Probes & Lifecycle
Configure health checks and graceful shutdown for production-ready pods.
Time: ~15 minutes Difficulty: Intermediate
What You Will Learn
Section titled “What You Will Learn”- Startup probes: protect slow-starting containers from being killed
- Liveness probes: detect deadlocked or crashed containers
- Readiness probes: control when a pod receives traffic
- Pre-stop hooks: run cleanup before pod termination
- Graceful shutdown with SIGTERM and terminationGracePeriodSeconds
- The #1 production incident cause: misconfigured probes
Deploy
Section titled “Deploy”Navigate to the demo directory:
cd demos/probes-lifecyclekubectl apply -f manifests/namespace.yamlkubectl apply -f manifests/healthy-app.yamlkubectl apply -f manifests/slow-start-app.yamlkubectl apply -f manifests/failing-liveness.yamlkubectl apply -f manifests/graceful-shutdown.yamlScenario 1: Healthy App (all three probes)
Section titled “Scenario 1: Healthy App (all three probes)”kubectl get pods -l app=healthy-app -n probes-demokubectl describe pod -l app=healthy-app -n probes-demo | grep -A 3 "Liveness\|Readiness\|Startup"All probes pass. The startup probe runs first, then liveness and readiness kick in.
Scenario 2: Slow-Starting App
Section titled “Scenario 2: Slow-Starting App”The slow-start app takes 15 seconds to initialize:
kubectl get pods -l app=slow-start -n probes-demo -wWatch the pod go through these phases:
Runningbut0/1 Ready(startup probe still checking)- After ~15s, startup probe passes
- Readiness probe passes, pod becomes
1/1 Ready
Without the startup probe, the liveness probe would kill the container before it finished starting.
Scenario 3: Failing Liveness Probe
Section titled “Scenario 3: Failing Liveness Probe”The liveness-fail pod removes its health file after 30 seconds:
kubectl get pods liveness-fail -n probes-demo -wWatch it:
- Starts healthy (RESTARTS: 0)
- After ~45s, liveness probe fails 3 times
- Kubernetes restarts the container (RESTARTS: 1)
- Cycle repeats
Check the events:
kubectl describe pod liveness-fail -n probes-demo | grep -A 5 "Events:"Scenario 4: Graceful Shutdown
Section titled “Scenario 4: Graceful Shutdown”Delete a pod and watch the graceful shutdown sequence:
# Watch logs in one terminalkubectl logs -f deploy/graceful-app -n probes-demo &
# Delete a podkubectl delete pod -l app=graceful-app -n probes-demo --wait=false
# Watch the shutdown sequence in the logs:# 1. preStop hook runs (removes from LB)# 2. SIGTERM delivered to the process# 3. App drains connections and saves state# 4. Clean exitWhat is Happening
Section titled “What is Happening”manifests/ namespace.yaml # probes-demo namespace healthy-app.yaml # All 3 probes configured correctly slow-start-app.yaml # Startup probe protects 15s init time failing-liveness.yaml # Liveness failure triggers restart graceful-shutdown.yaml # preStop hook + SIGTERM handlerProbe execution order:
Pod starts ──> Startup probe (repeats until success) | v (startup passes) Liveness probe ──> fails 3x ──> container restart Readiness probe ──> fails ──> removed from Service endpointsShutdown sequence:
kubectl delete pod | v1. preStop hook runs2. SIGTERM sent to PID 13. App handles SIGTERM (drain, save state)4. terminationGracePeriodSeconds countdown5. SIGKILL if still runningProbe types:
| Probe | Method | Best For |
|---|---|---|
httpGet | HTTP GET to a path/port | Web apps with health endpoints |
exec | Run a command, check exit code | File checks, CLI tools |
tcpSocket | TCP connection to a port | Databases, non-HTTP services |
Experiment
Section titled “Experiment”-
Remove the startup probe from
slow-start-app.yamland redeploy. Watch the liveness probe kill the container before it finishes starting. -
Make the readiness probe fail by changing the exec command:
Terminal window kubectl exec -it deploy/healthy-app -n probes-demo -- rm /usr/share/nginx/html/index.htmlThe pod becomes
0/1 Readyand is removed from the Service. Restore it:Terminal window kubectl exec -it deploy/healthy-app -n probes-demo -- sh -c 'echo ok > /usr/share/nginx/html/index.html' -
Check endpoints during readiness failure:
Terminal window kubectl get endpoints healthy-app -n probes-demo
Cleanup
Section titled “Cleanup”kubectl delete namespace probes-demoFurther Reading
Section titled “Further Reading”See docs/deep-dive.md for a detailed explanation of probe internals, gRPC probes, the kubelet probe execution model, common misconfiguration patterns, and production probe tuning.
Next Step
Section titled “Next Step”Move on to Network Policies to learn how to control pod-to-pod traffic.