Container Termination Order
A Pod with several app and sidecar containers is shutting down, and the sidecars have to outlive the app. In what order are they stopped, who goes last, and what happens if the grace period runs out first?
Play the animation 6 steps, animated, plays on a computer or a tablet
Container Termination Order, step by step
-
The Pod is marked for deletion and the Kubelet begins the local shutdown on this Node. Nothing has been signalled yet. Five containers are running here, three declared in spec.initContainers with restartPolicy Always and two in spec.containers, and one terminationGracePeriodSeconds covers all of them.
Play from step 1 -
Every container in spec.containers goes first. The Kubelet runs any preStop hook they declare and has the runtime send the stop signal to web and to redis, at different times and in an arbitrary order that nothing should rely on. No sidecar above has been signalled yet.
Play from step 2 -
While web and redis are still winding down, the gate holds so that mesh-proxy keeps carrying their outbound calls, log-agent keeps tailing the file they write and otel-agent keeps shipping what they emit. A sidecar stopped alongside the app would cut a path the app still needs.
Play from step 3 -
The last container in spec.containers has fully terminated, so the gate opens and the sidecars follow, in the reverse of the order they are declared in. The Kubelet walks the row from its end, and initContainers[2] is otel-agent, so otel-agent is the first sidecar to be signalled.
Play from step 4 -
Next is initContainers[1], which is log-agent, while mesh-proxy at initContainers[0] is still running. The Kubelet walks the row backwards so that log-agent can flush its last lines out through mesh-proxy on the way down.
Play from step 5 -
The mesh-proxy container is declared first, so it is stopped last, which is what kept a route open for the four that went before it. All five stops came out of the one budget the Pod carries, so if it runs out first, every sidecar not yet signalled is stopped at once with a short grace period.
Play from step 6