DEV Community

Cover image for 2.Kubernetes Sidecar Containers
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

2.Kubernetes Sidecar Containers

Task Information

We have a web server container running the nginx image. The access and error logs generated 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 that 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 volume 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 the volume shared-logs on both containers at location /var/log/nginx, all containers should be up and running.
Enter fullscreen mode Exit fullscreen mode

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.

Task Solutions

Step 1: Create the Pod YAML configuration

Create a file named webserver-pod.yaml:

cat > webserver-pod.yaml << EOF
apiVersion: v1
kind: Pod
metadata:
  name: webserver
spec:
  containers:
  - name: nginx-container
    image: nginx:latest
    volumeMounts:
    - name: shared-logs
      mountPath: /var/log/nginx

  - name: sidecar-container
    image: ubuntu:latest
    command: ["sh", "-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

  volumes:
  - name: shared-logs
    emptyDir: {}
EOF
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the pod

Apply the configuration to create the pod:

kubectl apply -f webserver-pod.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the pod is running

Check if the pod is created and both containers are running:

kubectl get pod webserver
Enter fullscreen mode Exit fullscreen mode

Step 4: Check the detailed status of the pod

Verify both containers are properly configured:

kubectl describe pod webserver
Enter fullscreen mode Exit fullscreen mode

Step 5: Test the sidecar container logs

Check the logs of the sidecar container to see if it's reading the nginx logs:

kubectl logs webserver -c sidecar-container
Enter fullscreen mode Exit fullscreen mode

Step 6: Generate some web traffic to create logs

Let's generate some access to the nginx server to create log entries:

First, find the nginx container's IP

kubectl get pod webserver -o wide
Enter fullscreen mode Exit fullscreen mode

Then access the nginx server (you can use the pod IP or port-forward):

Using port-forward

In one terminal, set up port forwarding

kubectl port-forward webserver 8080:80
Enter fullscreen mode Exit fullscreen mode

Step 7: Verify the sidecar is shipping logs

After generating some traffic, check the sidecar container logs again:

kubectl logs webserver -c sidecar-container
Enter fullscreen mode Exit fullscreen mode

Step 8: Verify volume mounting

Check that both containers have the volume mounted correctly:

Check nginx container

kubectl exec -it webserver -c nginx-container -- ls -la /var/log/nginx/
Enter fullscreen mode Exit fullscreen mode

Check sidecar container

kubectl exec -it webserver -c sidecar-container -- ls -la /var/log/nginx/
Enter fullscreen mode Exit fullscreen mode


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
📖 More Deep Dives: Whispering Cloud Insights - Read other technical articles
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)