Init Containers and Native Sidecars
How does a Pod start its containers when some of them must run before others? Regular init containers run strictly in order, each exiting 0 before the next begins, and a native sidecar (an initContainer with restartPolicy=Always, GA in 1.33) declared after them starts next and runs for the whole Pod lifetime. The main container is held back until that sidecar reports Started, and on shutdown the termination order reverses. The sidecar still counts in the init list.
Play the animation 5 steps, animated, best on a wide screen
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 with code 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 its own or inherited restartPolicy 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. The same rule applies, it 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 that order runs backwards.
Play from step 5