Pod Restart Policy
A container inside a running Pod exits, and sometimes it comes back, while at other times it stays down for good. What decides which of the two happens next, and what policy fits a long-lived app or a Job?
Play the animation 5 steps, animated, plays on a computer or a tablet
Pod Restart Policy, step by step
-
The restartPolicy is a Pod-level field, immutable once the Pod is created, and covers every app and regular init container that does not set its own. Since 1.35 the ContainerRestartRules feature gate is beta and on by default, so a container may override it with its own restartPolicy and restartPolicyRules matched on exit codes. The Pod-level default is Always. A sidecar (an initContainer with restartPolicy Always, on by default since 1.29 and GA in 1.33) ignores the Pod value. Kubelet reads the field from the Pod spec and applies it whenever a container terminates.
Play from step 1 -
Scenario: a container exits 0, a clean success. Pod A (Always) restarts the container and stays Running. Pod B (OnFailure) does not restart it after a success, so once the container is done the Pod phase becomes Succeeded. Pod C (Never) does not restart anything either and likewise ends Succeeded.
Play from step 2 -
Scenario: a container fails with a non-zero exit code. Pod A (Always) restarts the container. Pod B (OnFailure) restarts it too, since OnFailure retries after any non-zero exit. Pod C (Never) restarts nothing, so a single failure drives the Pod phase to Failed.
Play from step 3 -
The first restart is immediate, and every restart after it, whether by Always or by OnFailure, waits out the same exponential backoff schedule, kept per container. By default the wait doubles from a 10s base to a 5 minute ceiling. While it lasts, the container sits in Waiting with reason=CrashLoopBackOff, and the timer resets after the container has run successfully for 10 minutes. A Never Pod whose containers set no policy of their own never restarts, so it never enters this loop.
Play from step 4 -
Controllers of long-running apps (Deployment, ReplicaSet, DaemonSet, StatefulSet) only allow restartPolicy=Always, so their containers restart unless one sets its own policy. A Job uses OnFailure or Never so its Pods reach a terminal Succeeded or Failed phase instead of looping forever.
Play from step 5