Sub topic 11
Restart policy and the back-off delay
This chapter assumes you can read a pod manifest and know the difference between a container's state and a pod's phase, covered earlier. Here you learn which component restarts containers, when it does so, and why the delay between restarts grows.
Who restarts a container
The kubelet on the node restarts failed containers. The scheduler and the Deployment controller are not involved. The restart happens in place, on the same node, inside the same pod. The pod is not recreated, and its IP address and volumes stay the same. Only the restartCount in the container status goes up.
The pod-level field spec.restartPolicy controls this, and it applies to every container in the pod.
| Value | Restarts when | Typical use |
|---|---|---|
| `Always` (default) | The container exits for any reason, including exit code 0 | Deployments, StatefulSets, DaemonSets |
| `OnFailure` | The container exits with a non-zero code | Jobs |
| `Never` | Never | Jobs, one-shot debugging pods |
Deployments only accept Always. A web server that exits cleanly with code 0 is still restarted.
The back-off delay
The kubelet does not restart a failing container immediately. It waits, and the wait doubles after each failure: 10 seconds, 20, 40, 80, 160, then it stays at the 5-minute cap. If the container runs for 10 minutes without failing, the kubelet resets the delay.
While the kubelet waits, the container is in the Waiting state with the reason CrashLoopBackOff. This is why the status text appears and disappears. Between waits the container briefly runs, then goes to Terminated.
Watching it happen
This pod exits with an error after one second, so you can watch the back-off.
apiVersion: v1
kind: Pod
metadata:
name: crasher
spec:
restartPolicy: Always
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "echo starting; sleep 1; exit 1"]Apply it and watch the status column:
kubectl apply -f crasher.yaml
kubectl get pod crasher --watchThe output shows Error, then CrashLoopBackOff, and the RESTARTS count climbs more slowly over time. If you change restartPolicy to Never, the pod ends in Error with one run and no restarts.
Common mistakes
- Reading
CrashLoopBackOffas the error itself. It only says the container keeps exiting and the kubelet is waiting. The cause is in the exit code and logs, covered in later chapters. - Deleting the pod to skip the delay. A Deployment creates a new pod with a fresh back-off, but the underlying fault remains.
- Using
Alwaysfor a batch task that is meant to finish. The task reruns forever. Use a Job withOnFailureorNever.
Exercise
A Deployment's pod shows 6 restarts and the status CrashLoopBackOff. About how long will the kubelet wait before the next restart, and what resets that delay?
Show answer
After 6 failures the delay has doubled from 10s to 320s, which is capped at 5 minutes, so the wait is about 300 seconds. The delay resets only if the container runs for 10 minutes without failing.