Sub topic 10
Sidecars in Jobs and the completion problem
This chapter assumes you can write a Job manifest, you know that init containers and sidecars share a Pod's volumes, and you have read the previous chapters on native sidecars (init containers with restartPolicy: Always).
Why Jobs are different
A Deployment runs forever, so a sidecar that never exits is fine. A Job is different. A Job is finished when its Pod reaches the Succeeded phase, and a Pod only reaches that phase once all of its containers have stopped.
That creates the completion problem. Suppose you add a log-shipping container to a Job Pod as an ordinary second entry in containers. The worker finishes and exits with code 0. The log shipper keeps running, because it tails a file forever. The Pod stays Running, and the Job never completes. It also holds on to its CPU and memory requests.
The native sidecar fix
A native sidecar is declared in initContainers with restartPolicy: Always. The kubelet knows it is a helper. When every regular container has finished, the kubelet terminates the sidecars and the Pod can finish. The sidecar does not block the Job.
apiVersion: batch/v1
kind: Job
metadata:
name: report-job
spec:
backoffLimit: 2
template:
spec:
restartPolicy: Never
initContainers:
- name: log-shipper
image: busybox:1.36
restartPolicy: Always
command: ['sh', '-c', 'touch /var/log/job/job.log; tail -F /var/log/job/job.log']
volumeMounts:
- name: logs
mountPath: /var/log/job
containers:
- name: worker
image: busybox:1.36
command: ['sh', '-c', 'for i in 1 2 3; do echo "step $i" >> /var/log/job/job.log; sleep 2; done']
volumeMounts:
- name: logs
mountPath: /var/log/job
volumes:
- name: logs
emptyDir: {}Apply it and wait for completion:
kubectl apply -f report-job.yaml
kubectl wait --for=condition=complete job/report-job --timeout=60s
kubectl logs job/report-job -c log-shipperThe wait command returns once the worker has finished and the kubelet has stopped log-shipper. If you move log-shipper into containers, the same wait times out.
How the Job treats the sidecar
- Startup: the sidecar starts before the worker, like any init container. The worker does not start until the sidecar is running and, if it has a startup probe, that probe has passed.
- Completion: the Job looks at the regular containers. Once they all exit successfully, the kubelet stops the sidecar and the Pod succeeds.
- Worker failure: if the worker fails and the Pod's
restartPolicyisNever, the Pod is markedFailed. The Job controller then creates a new Pod, up tobackoffLimit. The sidecar is stopped along with the failed Pod. - Shutdown: the sidecar receives SIGTERM and gets the termination grace period to flush its data. A shipper that buffers logs needs a handler that flushes on SIGTERM. Otherwise the last lines can be lost.
Workaround for clusters without native sidecars
If you cannot use native sidecars, keep the sidecar in containers and have the worker signal completion through the shared volume. The worker creates a marker file when it is done. The sidecar polls for that file and exits when it appears.
apiVersion: batch/v1
kind: Job
metadata:
name: report-job-legacy
spec:
template:
spec:
restartPolicy: Never
containers:
- name: worker
image: busybox:1.36
command: ['sh', '-c', 'echo "work" >> /pod/job.log; touch /pod/done']
volumeMounts:
- name: shared
mountPath: /pod
- name: log-shipper
image: busybox:1.36
command: ['sh', '-c', 'until [ -f /pod/done ]; do sleep 1; done; cat /pod/job.log']
volumeMounts:
- name: shared
mountPath: /pod
volumes:
- name: shared
emptyDir: {}This works, but every sidecar has to implement the same convention. It also does not help if the worker crashes before it creates the marker file. Prefer native sidecars when your cluster supports them.
Common mistakes
| Symptom | Likely cause |
|---|---|
| Job stays active, Pod shows `1/2` containers Running | The sidecar is in `containers`, not `initContainers` |
| Pod rejected with an unknown or invalid field error | The cluster is too old for `restartPolicy` on init containers |
| Last log lines are missing | The sidecar does not flush on SIGTERM |
Exercise
A Job Pod has a worker and a metrics-proxy container, both in containers. The worker exits with code 0, but the Job never completes. What is the smallest change that fixes this on a Kubernetes 1.33 cluster?
Show answer
Move the metrics-proxy entry into initContainers and add restartPolicy: Always to it. It becomes a native sidecar. After the worker exits, the kubelet stops the proxy, the Pod reaches Succeeded, and the Job completes.