Kubernetes Sidecar Containers Kodekloud

 


Question: We have a web server container running the nginx image. The access and error logs produced by the web server are not critical enough to be placed on a persistent volume. However, Nautilus developers need access to the last 24 hours of logs so they can trace issues and bugs. Therefore, we need to ship the access and error logs for the web server to a log-aggregation service. Following the separation of concerns principle, we implement the Sidecar pattern by deploying a second container that ships the error and access logs from nginx. Nginx does one thing, and it does it well—serving web pages. The second container also specializes in its task—shipping logs. Since containers are running on the same Pod, we can use a shared emptyDir volume to read and write logs.

  • Create a pod named webserver.
  • Create an emptyDir volumemount name: shared-logs.
  • Create two containers from nginx and ubuntu images with latest tag only and remember to mention tag i.e nginx:latest, nginx container name should be nginx-container and ubuntu container name should be sidecar-container on webserver pod.
  • Add command on sidecar-container "sh","-c","while true; do cat /var/log/nginx/access.log /var/log/nginx/error.log; sleep 30; done"
  • Mount volume /var/log/nginx on both containers, all containers should be up and running.

 

Solution:

Simply create a pod.yaml file

apiVersion: v1
kind: Pod
metadata:
name: webserver
labels:
name: webserver
spec:
volumes:
- name: shared-logs
emptyDir: {}
containers:
- name: nginx-container
image: nginx:latest
volumeMounts:
- name: shared-logs
mountPath: /var/log/nginx
- name: sidecar-container
image: ubuntu:latest
command:
[
"/bin/bash",
"-c",
"while true; do cat /var/log/nginx/access.log /var/log/nginx/error.log; sleep 30; done",
]
volumeMounts:
- name: shared-logs
mountPath: /var/log/nginx

 

Apply pod.yaml

kubectl apply -f pod.yaml 

Check the pod 

kubectl get po

 

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment