Sub topic 4
Restart policy and CrashLoopBackOff
This chapter assumes you can read a Pod manifest, know the Pod phases and container states from the previous chapter, and can run basic kubectl commands.
What restartPolicy controls
restartPolicy is a field in the Pod spec. It tells the kubelet (the agent on each node) what to do when a container exits. It applies to all regular containers in the Pod, and it is not a per-container setting.
| Value | Restarts on exit code 0 | Restarts on non-zero exit | Typical use |
|---|---|---|---|
| `Always` (default) | Yes | Yes | Long-running services |
| `OnFailure` | No | Yes | Jobs that should retry |
| `Never` | No | No | One-shot tasks you want to inspect |
The kubelet restarts the container inside the same Pod, on the same node. The Pod is not recreated and keeps its name and IP. Only the container is replaced, so the RESTARTS column in kubectl get pod counts container restarts.
Restart backoff
If a container keeps exiting, the kubelet does not restart it in a tight loop. It waits between attempts. The delay starts at 10 seconds and doubles each time (20s, 40s, and so on), up to a cap of 5 minutes. The delay resets after the container has run for 10 minutes without a problem.
What CrashLoopBackOff means
CrashLoopBackOff is not an error in itself. It is a waiting state: the container crashed, and the kubelet is waiting out the backoff delay before trying again. It tells you that something makes the container exit repeatedly.
Here is a Pod that crashes on purpose:
apiVersion: v1
kind: Pod
metadata:
name: crasher
spec:
restartPolicy: Always
containers:
- name: app
image: busybox:1.36
command:
- sh
- -c
- echo "starting"; sleep 2; exit 1Apply it and watch:
kubectl apply -f crasher.yaml
kubectl get pod crasher -wThe status moves through Running, Error, and then CrashLoopBackOff, while RESTARTS increases.
Finding the cause
Common causes are a bad command or arguments, a missing config file or Secret, an application error at startup, a failing liveness probe, and an out-of-memory kill. Check these in order:
- Run
kubectl describe pod crasher. Look atLast State,Reason, andExit Codeunder the container.OOMKilledwith exit code 137 points to a memory limit. - Run
kubectl logs crasher --previous. The current container may have no logs yet, so--previousshows the output of the last crashed instance. - Read the
Eventssection at the bottom ofdescribefor probe failures and back-off messages.
Phase with Never and OnFailure
With restartPolicy: Never, a container that exits with code 0 moves the Pod to Succeeded, and a non-zero exit moves it to Failed. Nothing restarts. With OnFailure, a successful exit gives Succeeded, and failures are retried with the same backoff. With Always, the Pod is not expected to finish, so its phase stays Running while it loops.
Exercise
A Pod has restartPolicy Always and its container exits with code 0 every few seconds. Is the Pod in CrashLoopBackOff, and which command shows the output of the last exited container?
Show answer
Yes. With Always, the kubelet restarts the container even after a successful exit, and repeated quick exits lead to backoff delays and the CrashLoopBackOff status. Use kubectl logs <pod> --previous (add -c <container> if the Pod has several containers).