Init containers and sidecars
Give an app a file before it starts, then keep a helper running beside it.
What you will understand
- Explain when a regular init container finishes and when app containers can start.
- Recognize a native restartable sidecar in a Pod definition.
- Follow the same volume through three containers and debug a blocked start.
Builds on: The Pod lifecycle
What starts before the app
A web server needs an index.html when it starts. One way to arrange that is to run a small command that creates the file first, then launch the server. Another helper might continue reading or processing files while the server runs. Kubernetes gives these helpers different lifecycle roles.
A regular init container runs during Pod startup and has to finish successfully before Kubernetes goes on to the next init step. Init containers run in list order. They can prepare files or check dependencies so the application starts with what it needs. If a setup step repeatedly fails, later steps must wait. This makes a failure in the helper a valid explanation for why the web server has never started.
Prepare a file before the server starts
A regular init container writes the file to a shared volume, then exits successfully. That lets the main container start.
- KubeletStart the init containerInitializerThe application container waits for initialization.
- InitializerWrite index.html and exitShared mounted volumeThe prepared content is available to the next container.
- KubeletStart nginxApplication containernginx serves the file from the shared volume.
A regular init container writes the file to a shared volume, then exits successfully. That lets the main container start.
The Pod has one regular initializer; its default Pod restart policy retries failed initialization.
Explore another case
A sidecar is a helper that operates alongside the main application. A native restartable sidecar is declared within initContainers with its own restartPolicy: Always. Kubernetes waits for it to start, then advances to later init containers or application startup while the sidecar remains running. A startup probe, if configured, can determine when it counts as started. A regular second container in containers can also help an app, but it follows normal application-container startup rules.
When should a helper keep running?
A restartable init sidecar starts before the application and can stay running beside it. A regular init container finishes its work before startup continues.
Start the helper
- Kubelet lifecycleobserver: startedrestartable init
- Kubelet lifecycleweb: startingcontinue startup
observer is listed under initContainers with restartPolicy Always. After it starts, initialization can proceed rather than waiting for it to exit.
Run it beside the app
- Observer helperShared emptyDir pageread
- nginx applicationShared emptyDir pageread
The observer reads the shared page volume while nginx serves it. Both run in one Pod and share its resource and failure context.
Restart it independently
- nginx still running
- KubeletObserver restartedsidecar restart
If observer exits, kubelet can restart that sidecar without replacing the Pod or restarting nginx merely because the helper exited.
The observer has no startup probe; production ordering may require an explicit health gate.
Explore another case
Use an init container to prepare a file
In the manifest, the prepare init container writes a page to the volume page. The restartable observer reads it every ten seconds. nginx mounts the same volume at its document root and serves the page. The three mount paths belong to different containers. The underlying file is the same because all three use the volume named page.
apiVersion: v1
kind: Pod
metadata:
name: collaboration-demo
spec:
initContainers:
- name: prepare
image: busybox:1.36.1
command: ["sh", "-c", "printf 'prepared by init\n' > /work/index.html"]
volumeMounts:
- name: page
mountPath: /work
- name: observer
image: busybox:1.36.1
restartPolicy: Always # Restartable init sidecar: keep running.
command: ["sh", "-c", "while true; do cat /work/index.html; sleep 10; done"]
volumeMounts:
- name: page
mountPath: /work
readOnly: true
containers:
- name: web
image: nginx:1.27.5
volumeMounts:
- name: page
mountPath: /usr/share/nginx/html
readOnly: true
volumes:
- name: page
emptyDir: {} # Pod lifetime, not persistent storage.Save the file as collaboration-demo.yaml and try it in a disposable namespace. In describe, the prepare container should finish and the observer and web container should stay running. prepare writes its output to a file, so its logs may be empty. The observer’s logs should contain prepared by init. Start port forwarding if you want to fetch the page from your machine.
kubectl apply -f collaboration-demo.yaml
kubectl describe pod collaboration-demo
kubectl logs collaboration-demo -c prepare
kubectl logs collaboration-demo -c observer --tail=10
kubectl port-forward pod/collaboration-demo 8080:80Diagnose a blocked init container
For a separate copy of the Pod, change the preparation command so it exits with a nonzero code. Predict which containers never start and then inspect that Pod. Check the named init container’s logs and termination status first. Looking at web-server logs cannot explain a failure that happened before nginx ran. For this comparison use a different Pod name, since many fields in an existing Pod cannot be edited in place.
Check whether a helper must start first
Use an init step for work that needs to finish first. For dependencies such as another service, give any wait a timeout and a useful error so the next operator can tell why startup stopped. The shared volume here has the Pod’s lifetime. If Kubernetes replaces the Pod, preparation runs again. The module lab demonstrates regular init ordering and a second application container sharing a file. Its companion follows the regular container rules.
Check your understanding
The observer has started, but no nginx logs exist yet. Has the observer blocked nginx by continuing to run?
With the native sidecar configuration shown, Kubernetes can advance after the observer starts. It does not wait for that helper to exit. Check application container state and the startup of any other init steps to find what is still pending.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise