Sub topic 7
Restart policy and CrashLoopBackOff
This chapter assumes you know Pods, Deployments, and kubectl describe. It also assumes you know that the kubelet is the agent on each node that runs containers. Here you will see how the kubelet decides whether to restart a container, and why a failing liveness probe can end in CrashLoopBackOff.
What restartPolicy controls
Every Pod has a restartPolicy field in its spec. It tells the kubelet what to do when a container in the Pod stops. The policy applies to all containers in the Pod, and the kubelet does the restarting on the same node. The Pod is not rescheduled.
| Value | Restarts when container exits 0 | Restarts when container exits non-zero |
|---|---|---|
| `Always` (default) | Yes | Yes |
| `OnFailure` | No | Yes |
| `Never` | No | No |
Deployments only allow Always. This matters for probes. A liveness probe failure makes the kubelet kill the container, and the restart policy then decides what happens next. With Always, the container is started again.
Backoff between restarts
If a container keeps stopping, the kubelet does not restart it in a tight loop. It waits between attempts, and the wait grows: 10 seconds, 20 seconds, 40 seconds, and so on, up to a cap of 5 minutes. The kubelet resets the timer after the container has run for 10 minutes without problems.
While the kubelet waits, the Pod shows the status CrashLoopBackOff. This is not an error in itself. It means "the container keeps stopping, and I am waiting before the next attempt."
How probes feed into this
Only some probe results lead to restarts.
- A failed readiness probe never restarts anything. The Pod is removed from Service endpoints and the container keeps running.
- A failed liveness probe, after
failureThresholdconsecutive failures, makes the kubelet kill the container. The restart policy then applies. - A failed startup probe also kills the container once its threshold is reached. Liveness and readiness probes do not run until the startup probe succeeds.
When a container is killed by a probe, the kubelet sends SIGTERM and waits for the grace period. It then sends SIGKILL. Such a container usually shows exit code 137 (SIGKILL) or 143 (SIGTERM).
A restart loop caused by a probe
Consider an application that needs 40 seconds to start, with this liveness probe and no startup probe:
livenessProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 5
failureThreshold: 3The first probe fires about 0 to 5 seconds after the container starts. By 15 seconds, three probes have failed and the kubelet kills the container. The application never finishes starting, so the cycle repeats with growing delays. The application is healthy. The probe is too strict for its startup time. The fix is a startup probe, covered in a later chapter, or a longer probe window.
Reading the symptoms
Use these commands to find the cause.
kubectl get pods -l app=web
kubectl describe pod -l app=web
kubectl logs deploy/web --previousIn kubectl describe, check these fields under the container:
- Last State: Terminated shows the reason and exit code.
- Restart Count shows how many restarts have happened.
- Events show lines like
Liveness probe failedfollowed byContainer web failed liveness probe, will be restarted.
The reason tells you which case you are in:
| Signal | Likely cause |
|---|---|
| `Liveness probe failed` events, exit code 137 | The probe killed the container |
| Reason `Error`, exit code 1, no probe events | The application crashes on its own |
| Reason `OOMKilled` | The container exceeded its memory limit |
Use --previous because the current container has just started and its logs are often empty. The failure is in the previous instance.
Exercise
A Pod shows CrashLoopBackOff. kubectl describe shows many Liveness probe failed: Get "http://10.0.1.5:8080/healthz": context deadline exceeded events, and the last state is Terminated with exit code 137. The application logs from --previous show normal startup messages that were cut off partway. What is the most likely cause, and what would you check first?
Show answer
The liveness probe is killing a healthy but slow container. The probe times out (default timeoutSeconds is 1) or starts too early, and after failureThreshold failures the kubelet kills the container (exit 137). Check how long the application takes to start and respond. Then add a startup probe or raise timeoutSeconds and failureThreshold.