Init Containers and Native Sidecars
Regular init containers finish one by one before the app starts, while a sidecar runs beside it as long as the Pod lives. In what order do they start, who waits for whom, and who restarts the sidecar when it crashes?
Play the animation 5 steps, animated, plays on a computer or a tablet
Init Containers and Native Sidecars, step by step
-
Kubelet asks the runtime to Create and Start wait-for-db via CRI. Init containers run strictly sequentially: a regular one must exit 0 before the next can start. A non-zero exit shows Init:Error and is retried with backoff (Init:CrashLoopBackOff), or fails the Pod if the container restartPolicy, own or inherited, is Never.
Play from step 1 -
The wait-for-db container exits 0. Kubelet observes the exit via PLEG (Pod Lifecycle Event Generator) and then creates migrate-schema. It follows the same rule and must exit 0 before any later container can start. Each init container image is pulled lazily, just before that container is created, per its imagePullPolicy.
Play from step 2 -
Both regular init containers exited 0. The sidecar (declared as an initContainer with restartPolicy=Always, beta in 1.29 and GA in 1.33) is started next, allowed to run for the full lifetime of the Pod. Once it reports Started (its startupProbe succeeded, or a running process where no probe is set), Kubelet moves on, and with no init container left, the main container starts next.
Play from step 3 -
As soon as the sidecar Started flag flips true, Kubelet creates and starts the main container. From here both run in parallel. Pod phase flips from Pending to Running once the main container is running, even before its own startupProbe passes.
Play from step 4 -
Pod is Running. The sidecar handles cross-cutting concerns (proxy, log shipping, credential rotation) alongside main. Kubelet restarts the sidecar independently if it crashes (because restartPolicy=Always on the init slot). On Pod termination the start order runs backwards.
Play from step 5