Sub topic 16
Liveness and Startup Probes That Kill Healthy Apps
This chapter assumes you can read pod YAML, know what a container restart is, and have seen CrashLoopBackOff in kubectl get pods. A pod can enter that state even when the application is fine. A misconfigured liveness probe can cause it.
How probes cause restarts
The kubelet runs three kinds of probes. Only two of them can restart a container.
| Probe | Question it answers | Effect on failure |
|---|---|---|
| Startup | Has the app finished starting? | Kills the container after `failureThreshold` failures |
| Liveness | Is the app still working? | Kills the container after `failureThreshold` failures |
| Readiness | Can the app take traffic? | Removes the pod from Service endpoints. It does not restart. |
Liveness checks begin as soon as the container starts, unless a startup probe is defined. If the app needs 60 seconds to warm up and the liveness probe gives up after 30, the kubelet kills the container mid-start. The app restarts, takes 60 seconds again, and is killed again. Back-off delays make the loop slower each cycle.
Recognizing the pattern
The signs differ from a real application crash:
kubectl describe pod webshows events such asLiveness probe failed: Get "http://10.0.0.5:8080/healthz": connection refusedfollowed byContainer app failed liveness probe, will be restarted.- The last terminated state usually shows exit code
137(SIGKILL) or143(SIGTERM), not a code the app chose. kubectl logs web --previousshows a normal startup that was cut off, with no error.
An exit code of 137 also appears with OOMKilled. Check the reason field in the terminated state to tell them apart. A probe kill has reason Error, not OOMKilled.
Fixing it with a startup probe
A startup probe holds off the liveness probe until it succeeds once. This pod allows up to 150 seconds to start (periodSeconds: 5 times failureThreshold: 30). After that, liveness runs with tight settings.
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: app
image: registry.example.com/team/web:1.4.2
startupProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 5
failureThreshold: 30
livenessProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 10
failureThreshold: 3Prefer this over a large initialDelaySeconds on the liveness probe. A fixed delay wastes time on fast starts and still fails on slow ones.
Other causes to check
timeoutSecondsis too short (the default is 1 second), so a briefly busy app fails the probe.- The probe uses the wrong port or path, so it fails every time.
- The app is CPU-throttled and responds slowly under load.
Exercise
A pod restarts every 40 seconds. kubectl logs --previous shows a clean startup with no errors, and the app needs about 60 seconds to initialize. The liveness probe has periodSeconds: 10 and failureThreshold: 3. What is the likely cause, and what change fixes it?
Show answer
The liveness probe fails three times in a row (about 30 seconds plus the first check) before the app is ready, so the kubelet kills the container. Add a startupProbe with enough time to cover initialization, for example periodSeconds: 5 and failureThreshold: 30. Liveness checks then begin only after startup succeeds.