Running Applications
Probes
Startup, readiness, and liveness, measured
A process being up doesn't mean it can serve. The kubelet asks each container three different questions, with three different consequences.
The app has a 5-second warm-up (STARTUP_SECONDS) during which /readyz
returns 503, and endpoints to make it unready or "sick" on demand.
Measured
./scripts/lab.sh probes.py
endpoints: ['web-5865ccc48d-2g5q7', 'web-5865ccc48d-mfktj', 'web-5865ccc48d-x2gp6']
web-5865ccc48d-2g5q7 says not ready -> removed from endpoints after 2.2s
5 s of traffic: {'web-5865ccc48d-mfktj': 48, 'web-5865ccc48d-x2gp6': 41} (web-5865ccc48d-2g5q7 got 0)
pod still running, restarts: 0
ready again -> back in endpoints after 0.9s
web-5865ccc48d-2g5q7 says not alive -> container restarted after 16.4s (restarts: 2)
ready again 5.7s after the liveness failure (startup 5 s warm-up included)
traffic during it: 392 requests, 392 ok, errors: none
event: Unhealthy x6 Liveness probe failed: HTTP probe failed with statuscode: 500
event: Killing x2 Container web failed liveness probe, will be restartedReadiness takes a pod out; liveness restarts it
- Readiness removed the pod from the endpoints in 2.2 s (2 failures × 2 s). It received zero of the next 89 requests, and it was not restarted: once it reported ready again, it was back in under a second.
- Liveness restarted the container after 16 to 20 s across runs: 3 failures × 5 s, plus the 5-second preStop sleep that also runs before a liveness kill. With startup and readiness gating, no client saw an error.
Designing the endpoints
Common mistake
Never check dependencies in liveness. If /healthz checked Redis, a
Redis outage would fail liveness in every pod at once, and the kubelet
would restart all of them, repeatedly, turning a dependency outage into
your own outage, with a cold start on top. Liveness should only detect
states a restart fixes: a deadlock, a corrupted in-process state.
- Readiness can reflect the pod's own ability to serve (warming up,
shutting down, overloaded). Be careful with shared dependencies there
too: if every pod goes unready when Redis is down, the Service has no
endpoints and clients get errors from the proxy instead of a degraded
response from the app. This app's
/api/visitsreturns 503 on its own when Redis is down, and stays ready. - Startup probes exist for slow starters (JVMs, large caches). Without one, a liveness probe with a short timeout kills the container before it finishes starting, forever: a crash loop that looks like a bug in the app.
Lab
- Run
probes.py. Then change the liveness probe to/readyzand make a pod unready. What happens now, and why is that worse? - Set
STARTUP_SECONDS: "30"and roll the Deployment. How long does the rollout take now, and which probe decides when each new pod gets traffic? - Stop Redis (
kubectl -n shop scale sts redis --replicas=0) and request/api/whoamiand/api/visits. Which still work?