Sub topic 7
The sidecar pattern: a helper that runs alongside your app
This chapter assumes you can write Pod manifests, use kubectl to apply resources and read logs, and that you know what an emptyDir volume is from the earlier chapters.
What a sidecar is
A sidecar is a second container in the same Pod that supports the main application container. The app does its own job. The sidecar handles a side concern such as shipping logs, refreshing configuration, or proxying network traffic.
Putting the helper in the same Pod, instead of a separate one, gives you three things:
- Shared lifecycle. Both containers are scheduled together on one node, and they are created and deleted together.
- Shared network. Both containers use the same IP address and can reach each other on
localhost. - Shared volumes. Both can mount the same volume, so one can write files that the other reads.
The main benefit is separation of concerns. The app image does not need log-shipping code, and the helper can be reused with many different apps.
A log-shipping sidecar
In this example the app writes a log file, and the sidecar reads it and prints it to its own stdout. In a real system the sidecar would forward the lines to a log backend.
apiVersion: v1
kind: Pod
metadata:
name: app-with-sidecar
spec:
containers:
- name: app
image: busybox:1.36
command: ['sh', '-c', 'while true; do date >> /var/log/app/app.log; sleep 5; done']
volumeMounts:
- name: logs
mountPath: /var/log/app
- name: log-shipper
image: busybox:1.36
command: ['sh', '-c', 'mkdir -p /var/log/app; touch /var/log/app/app.log; tail -F /var/log/app/app.log']
volumeMounts:
- name: logs
mountPath: /var/log/app
volumes:
- name: logs
emptyDir: {}Both containers mount the logs volume at the same path. The app container appends a timestamp every five seconds. The log-shipper container follows the file with tail -F.
Apply the manifest and read the sidecar's output:
kubectl apply -f app-with-sidecar.yaml
kubectl get pod app-with-sidecar
kubectl logs app-with-sidecar -c log-shipperREADY should show 2/2. Because the Pod has two containers, you must pass -c to choose which one's logs you want.
What to know before you use it
Startup order is not guaranteed
Containers in the containers list start in the order written, but Kubernetes does not wait for one to be ready before starting the next. The sidecar might start before the app creates its log file. That is why the example runs touch first and uses tail -F, which keeps trying if the file is missing. Write sidecars so they tolerate the app not being ready yet.
Each container is restarted on its own
If the sidecar crashes, the kubelet restarts only that container. The app keeps running. This is usually what you want, but a crash-looping sidecar can silently stop your logs while the Pod still looks alive. Check RESTARTS and per-container state.
Resources add up
The scheduler places the Pod using the sum of the requests of all its containers. A sidecar that requests 200m CPU raises the Pod's cost by 200m, and with 50 replicas that is 10 CPUs. Set requests and limits on the sidecar deliberately.
Data in emptyDir is temporary
An emptyDir volume lives as long as the Pod. It survives container restarts, but it is deleted when the Pod is removed. If the sidecar has not shipped the data by then, the data is lost.
Ordinary sidecars have lifecycle gaps
A sidecar in containers is an equal of the app container. Kubernetes does not know it is a helper. Two problems follow:
- It is not guaranteed to be running before the app starts, so an app that needs a proxy at startup may fail.
- In a Job, the Pod will not finish while the sidecar is still running, even after the main work is done.
The next chapters cover native sidecars, which are init containers with restartPolicy: Always, and which address both problems.
Exercise
In the example Pod, the app container is changed to write its log to /var/log/app/app.log, but the log-shipper container mounts the logs volume at /logs instead of /var/log/app. What does the sidecar see, and why?
Show answer
The sidecar sees the same app.log file, but at /logs/app.log. A volume is shared by name, and each container chooses its own mount path. The sidecar's command must then tail /logs/app.log, or it will follow a path that does not exist.