Sub topic 8
Liveness probes: restarting stuck containers
This chapter assumes you know Pods, Deployments, and Services, can read YAML, and can use kubectl apply and kubectl describe. It also assumes you know the basic probe mechanisms and timing parameters from the earlier chapters.
What a liveness probe does
A liveness probe asks one question: is this container still working, or is it stuck in a state it cannot recover from? Examples are a deadlock, an infinite loop, or a runtime that is up but no longer serves requests.
The kubelet runs the probe. If it fails failureThreshold times in a row, the kubelet kills the container. The Pod's restartPolicy then decides what happens next. With the default Always (and with OnFailure), the container is restarted in the same Pod.
A liveness failure does not remove the Pod from Service endpoints directly. Traffic is controlled by the readiness probe. The restart does take the container down, so the Pod becomes not ready while it restarts.
Configuring a liveness probe
Here is a Deployment with a liveness probe on an HTTP endpoint.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: registry.example.com/web:1.0.0
ports:
- containerPort: 8080
livenessProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 10
timeoutSeconds: 2
failureThreshold: 3With these values, the kubelet probes every 10 seconds. Each probe must answer within 2 seconds. After 3 consecutive failures, the container is killed. A truly stuck container is therefore restarted roughly 30 seconds after it stops responding.
You can use the other mechanisms too. An exec probe succeeds when the command exits with status 0:
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
periodSeconds: 5Choosing the timing
Detection time is about periodSeconds × failureThreshold, plus up to one timeoutSeconds. Tune it by asking how long a stall can last before a restart is the better option.
- Too aggressive (short period, threshold of 1): a garbage collection pause or a brief CPU spike causes a restart. Restarts then add load, which causes more failures.
- Too relaxed: a deadlocked container serves errors for minutes.
Liveness probes only start after the startup probe succeeds, if one is configured. For slow-starting applications, use a startup probe rather than a large initialDelaySeconds. Otherwise the liveness probe can kill the container before it finishes starting.
What the probe should check
Keep the liveness check narrow. It should test whether the process itself can respond, not whether its dependencies are healthy.
A good /healthz handler returns 200 as long as the main loop can run. It does not need to do any real work.
Seeing restarts
When a liveness probe fails, the kubelet records events. Use these commands to inspect them:
kubectl describe pod -l app=web
kubectl logs deploy/web --previousIn the describe output, look for events like Liveness probe failed: HTTP probe failed with statuscode: 500 followed by Container web failed liveness probe, will be restarted. The RESTARTS column in kubectl get pods also increases. --previous shows the logs of the container instance that was killed, which usually explains why it hung.
If the container keeps failing after restart, the kubelet waits longer between restarts. The Pod shows CrashLoopBackOff, with the delay growing up to five minutes.
Common mistakes
- Using the same endpoint and thresholds for liveness and readiness. A temporary overload should stop traffic, not restart the container.
- Setting
timeoutSecondslower than the endpoint's normal latency under load. - Adding a liveness probe "just in case". If the application already exits when it fails, a probe adds risk and little benefit.
Exercise
A container with a liveness probe has periodSeconds: 5, timeoutSeconds: 1, and failureThreshold: 4. The application deadlocks and stops responding at time 0. Roughly how long until the kubelet kills the container, and what happens to traffic during that time?
Show answer
About 20 seconds (4 failures × 5 seconds), plus up to a second of timeout. During that period the Pod still receives traffic unless its readiness probe also fails. The liveness probe only restarts the container. Readiness is what removes the Pod from Service endpoints.