Sub topic 4
How init containers run: order, failures, and restarts
This chapter assumes you can write a Pod manifest, apply it with kubectl, and read basic Pod status. It also assumes you know what an init container is from the previous chapter. Here you will learn the rules the kubelet follows when it runs init containers, and what you see when one of them fails.
Strict sequential order
Init containers run one at a time, in the order they appear in spec.initContainers. The kubelet starts the next one only after the previous one exits with code 0. App containers start only after every init container has succeeded.
This differs from app containers, which the kubelet starts together. Because of the sequence, you can build steps that depend on each other. For example, one init container fetches a file and the next one validates it.
apiVersion: v1
kind: Pod
metadata:
name: ordered-init
spec:
initContainers:
- name: step-one
image: busybox:1.36
command: ['sh', '-c', 'echo step one done; sleep 5']
- name: step-two
image: busybox:1.36
command: ['sh', '-c', 'echo step two done; sleep 5']
containers:
- name: app
image: nginx:1.27Apply it and run kubectl get pod ordered-init -w. The STATUS column changes from Init:0/2 to Init:1/2, then to PodInitializing, then to Running. The first number counts the init containers that have finished, and the second is the total.
What happens when an init container fails
A nonzero exit code counts as a failure. What the kubelet does next depends on the Pod's restartPolicy:
Always(the default) andOnFailure: the kubelet restarts only the failed init container. Kubernetes treats an init container asOnFailurewhen the Pod policy isAlways.Never: the kubelet does not retry. The Pod is marked as failed.
Retries use exponential backoff. The delay grows (10 seconds, 20 seconds, 40 seconds, and so on) up to a cap of five minutes. While the delay is running, kubectl get pod shows Init:CrashLoopBackOff.
Until all init containers succeed, the Pod's Ready condition stays false. A Service does not send traffic to the Pod, and the app containers never start.
| STATUS | Meaning |
|---|---|
| `Init:0/2` | Two init containers, none finished yet |
| `Init:1/2` | One finished, the second is running or waiting |
| `Init:Error` | An init container exited with an error |
| `Init:CrashLoopBackOff` | An init container keeps failing and is in backoff |
| `PodInitializing` | All init containers succeeded, app containers are starting |
When init containers run again
An init container that has succeeded does not run again just because an app container crashes. The kubelet restarts the crashed app container only.
Init containers can run again in these cases:
- The Pod sandbox is recreated, for example after a node or container runtime restart. All init containers then run again, in order.
- You change an init container's
image. Kubernetes treats that as a restart of the Pod's init sequence. - The kubelet restarts a failed init container, as described above.
For this reason, write init containers to be idempotent. Running one twice must give the same result as running it once. For example, use mkdir -p instead of mkdir, and overwrite files instead of appending to them.
Other rules to know
- Init container names must be unique within the Pod, and they cannot repeat an app container's name.
- Regular init containers do not support
livenessProbe,readinessProbe, orstartupProbe. Success is defined only by the exit code. (Native sidecars, covered later, are the exception.) activeDeadlineSecondson the Pod covers the time spent in init containers too. A slow init sequence can cause the whole Pod to fail.- An init container that never exits blocks the Pod forever. A wait loop with no timeout, like the one in the earlier DNS example, is a deliberate choice. Make sure you want it.
Observing a failure
To see the behavior, run a Pod whose init container always fails, then inspect it.
kubectl get pod web
kubectl describe pod web
kubectl logs web -c wait-for-dbdescribe shows each init container's state, last exit code, and restart count. logs -c <name> prints that container's output. You must pass -c because the Pod has several containers.
Exercise
A Pod has restartPolicy: Never and two init containers. The first exits with code 1. What is the state of the Pod, and does the second init container run?
Show answer
The kubelet does not retry the first init container because the policy is Never, so the Pod is marked as failed. The second init container never runs, and the app containers never start.
Exercise
Your app container crashes and restarts three times. Do the init containers run again each time? Why does idempotency still matter?
Show answer
No. Init containers do not rerun when only an app container restarts. Idempotency still matters because init containers can run again after a Pod sandbox recreation, an image change, or a retry after failure, and a repeat run must be safe.