Start: 2023-01-13 21:00:51
Finished: 2023-01-13 21:18:09
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.
Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.
Create the manifest sidecar.yml for the resource definitions.
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:
[
"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
Apply.
kubectl apply -f sidecar.yml
Ensue the Pod is running.
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
webserver 2/2 Running 0 39s
To see if the container started successfully, we can check the Events that occured in the Pod.
$ kubectl describe pod webserver | grep Events -A 20
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 119s default-scheduler Successfully assigned default/webserver to kodekloud-control-plane
Normal Pulling 118s kubelet Pulling image "nginx:latest"
Normal Pulled 105s kubelet Successfully pulled image "nginx:latest" in 12.651602268s
Normal Created 105s kubelet Created container nginx-container
Normal Started 105s kubelet Started container nginx-container
Normal Pulling 105s kubelet Pulling image "ubuntu:latest"
Normal Pulled 96s kubelet Successfully pulled image "ubuntu:latest" in 8.98893506s
Normal Created 95s kubelet Created container sidecar-container
Normal Started 94s kubelet Started container sidecar-container
We can also verify that the two logs are created by running a shell command on the Pod.
~$ kubectl exec -it webserver -- bash
Defaulting container name to nginx-container.
Use 'kubectl describe pod/webserver -n default' to see all of the containers in this pod.
root@webserver:/#
root@webserver:/# ls -la /var/log/nginx
total 16
drwxrwxrwx 2 root root 4096 Jan 13 13:10 .
drwxr-xr-x 1 root root 4096 Jan 11 06:31 ..
-rw-r--r-- 1 root root 0 Jan 13 13:10 access.log
-rw-r--r-- 1 root root 2489 Jan 13 13:10 error.log