Sub topic 8
Native sidecars: init containers with restartPolicy Always
This chapter assumes you can write Pod manifests, use kubectl apply, kubectl get, and kubectl logs, and that you know what an init container is and how an emptyDir volume is shared between containers. It covers the native sidecar, which is an init container that keeps running. Startup probes, shutdown order, and Jobs are covered in later chapters.
The problem with ordinary sidecars
In the previous chapter, a sidecar was just a second entry in spec.containers. That works, but it has two weaknesses:
- Regular containers start without a guaranteed order. A log shipper or proxy may not be ready when the app starts writing or sending traffic.
- Init containers do guarantee order, but they must exit before the next one starts. A helper that has to keep running cannot be an init container in the traditional sense.
Native sidecars close this gap. They were introduced as alpha in Kubernetes 1.28 and became stable in 1.33. On older clusters the feature gate SidecarContainers must be enabled, and the field is rejected where it is unsupported.
The one-line change
You make an init container a native sidecar by setting restartPolicy: Always on that container. The field sits on the container, not on the Pod.
apiVersion: v1
kind: Pod
metadata:
name: app-with-log-sidecar
spec:
initContainers:
- name: log-shipper
image: busybox:1.36
restartPolicy: Always
command: ['sh', '-c', 'touch /var/log/app/app.log; tail -F /var/log/app/app.log']
volumeMounts:
- name: logs
mountPath: /var/log/app
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
volumes:
- name: logs
emptyDir: {}Here log-shipper creates the log file and follows it. The app container writes a timestamp every five seconds into the same emptyDir volume. Because log-shipper is listed under initContainers, kubelet starts it first.
How the lifecycle differs
A regular init container runs to completion and must exit with code 0. A native sidecar is different in three ways:
- Kubelet considers it started once its container has been started (and its startup probe, if any, has passed). It does not wait for it to exit.
- It keeps running for the life of the Pod, alongside the app containers.
- If it exits, kubelet restarts it, regardless of whether the exit code was 0 or not.
Regular init containers listed after a sidecar still run in order. They wait only for the sidecar to start, not to finish.
Startup order
The diagram shows a Pod with one regular init container followed by a sidecar and the app container.
Because the sidecar is up before the app begins, the app can rely on the helper being present. A typical use is a proxy or log agent that must be listening before the first request or log line.
Observing the Pod status
After applying the manifest, check the Pod:
kubectl apply -f sidecar.yaml
kubectl get pod app-with-log-sidecar
kubectl logs app-with-log-sidecar -c log-shipperThe READY column counts the sidecar. A Pod with one sidecar and one app container shows 2/2 when both are running. The status is Running rather than Init:... once the sidecar has started and the app container is up. The logs command works on the sidecar by name, just as it does for any container, and you should see the timestamps the app writes.
Comparing the two ways to run a sidecar
| Aspect | Sidecar in `containers` | Native sidecar in `initContainers` |
|---|---|---|
| Start order | Not guaranteed relative to the app | Starts before the app containers |
| Listed under | `spec.containers` | `spec.initContainers` with `restartPolicy: Always` |
| Restarts on exit | Follows the Pod `restartPolicy` | Always restarted |
| Can use a startup probe to gate the app | No | Yes |
Common errors
- Forgetting the field. Without
restartPolicy: Always, the container is a normal init container. If its command never exits, the Pod stays inInit:0/1forever and the app never starts. - Putting the field in the wrong place. Setting it under
spec.containersis rejected. It is valid only for entries ininitContainers. - Using it on an old cluster. On a cluster that does not support the feature, the API server rejects the manifest. Check the version with
kubectl version. - A sidecar that exits right away. Kubelet restarts it repeatedly, and it shows
CrashLoopBackOffbehavior. Read its logs withkubectl logs <pod> -c <sidecar>.
Exercise
A Pod defines an init container named proxy with restartPolicy: Always, followed by a regular init container named migrate, and then the app container. Does migrate wait for proxy to exit before it starts, and why?
Show answer
No. proxy is a native sidecar, so kubelet treats it as started once its container has started (and its startup probe, if any, has passed). It does not wait for it to exit. migrate then runs to completion, and only after that does the app container start, with proxy still running.
Exercise
You converted a log-shipping container from spec.containers to spec.initContainers but the Pod now stays in Init:0/1 and the app never starts. What did you most likely forget?
Show answer
You forgot to set restartPolicy: Always on the container. Without it, kubelet treats it as a regular init container and waits for it to exit. Because tail -F never exits, the Pod never moves past initialization.