Sub topic 8
Multi-container Pods and the sidecar pattern
This chapter assumes you can read a Pod manifest, know that a Pod has one IP address, and have seen emptyDir volumes. Most Pods run one container. A Pod can run several when they form one tightly coupled unit.
What containers in a Pod share
Containers in the same Pod share:
- The network namespace. They have one IP address and reach each other on
localhost, so two containers cannot listen on the same port. - Any volumes you mount into them.
They do not share a filesystem. Each container has its own image and root filesystem. Each container also has its own resources settings and its own probes. The Pod is scheduled as a whole, so all its containers land on the same node.
The sidecar pattern
A sidecar is a helper container that runs next to the main application container and extends it without changing its image. Common roles:
| Role | What the helper does | Example |
|---|---|---|
| Sidecar | Adds a capability | Ships logs, refreshes certificates |
| Adapter | Converts output to a standard format | Turns app metrics into Prometheus format |
| Ambassador | Proxies outbound connections | Connects to a database through `localhost` |
The next manifest shows a log sidecar. The app container appends a timestamp to a file every five seconds. The log-reader container follows that file. They exchange data only through the shared emptyDir volume.
apiVersion: v1
kind: Pod
metadata:
name: log-demo
spec:
volumes:
- name: logs
emptyDir: {}
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-reader
image: busybox:1.36
command:
- sh
- -c
- touch /var/log/app/app.log; tail -F /var/log/app/app.log
volumeMounts:
- name: logs
mountPath: /var/log/appThe touch in the reader avoids a race. Both containers start at about the same time, and the reader must not fail because the file does not exist yet.
Because the Pod has two containers, kubectl logs needs -c to pick one:
kubectl apply -f log-demo.yaml
kubectl logs log-demo -c log-readerSidecar or init container?
Use an init container for work that must finish before the app starts, such as preparing files. Use a sidecar for work that must run for the whole life of the app. Init containers run one at a time and must exit successfully. Regular containers start together and keep running.
Recent Kubernetes versions also support native sidecars. These are init containers with restartPolicy: Always. They start before the app container and keep running, and the kubelet stops them after the app containers. Check that your cluster version supports this feature before you rely on it.
Gotchas
- The Pod is Ready only when all of its containers are ready. A failing sidecar probe removes the whole Pod from Service endpoints.
- The Pod's
restartPolicyapplies to every container. A crashing sidecar can enter CrashLoopBackOff while the app keeps running. - Pod resource requests are the sum of all container requests, so a sidecar makes the Pod harder to schedule.
- Do not add a container just because it is convenient. If the two parts scale or fail independently, use separate Pods.
Exercise
In the log-demo Pod, why does the log-reader command run touch /var/log/app/app.log before tail -F?
Show answer
The two containers start at about the same time, and the app may not have created app.log yet. The touch creates the file so the reader does not fail on a missing file. Both containers share the file through the logs emptyDir volume.
Exercise
A Pod has an app container and a sidecar. The sidecar's readiness probe keeps failing, but the app is healthy. What happens to traffic sent to this Pod through a Service?
Show answer
The Pod is not Ready, because every container must be ready. The Service stops sending traffic to it, even though the app itself is healthy.