Add startup, readiness and liveness probes
In this task you'll let Kubernetes know when an app is ready, still starting, or stuck.
Probes are health checks the kubelet runs against each container. A readiness probe decides whether the pod receives traffic: failing removes it from the Service's endpoints but doesn't restart it. A liveness probe decides whether the container is stuck: failing restarts it. A startup probe holds the other two off until a slow app has started.
Most probe outages come from liveness probes that are too strict or that check other systems: if the database is slow, every pod fails liveness, restarts, and the outage gets worse. Keep liveness cheap and about the process itself, put dependency checks in readiness, and give slow starters a startup probe instead of a long initialDelaySeconds.
Example. Brindle Outdoor's catalogue service took 70 seconds to warm its cache, but its liveness probe started after 30. Pods were restarted forever during every release until a startup probe allowed up to 120 seconds.
Before you start
You need: Docker Desktop, kind and kubectl on your computer (all free), and a cluster from Run a practice cluster on your computer. Time: 30 minutes.
Where to look
- Terminal
- kubectl
Steps
- Deploy nginx with a readiness probe on path
/port 80, and check the pods become Ready. - Change the readiness path to
/missing; the pods stay Running but not Ready, and the Service has no endpoints.No restarts. - Put the path back, then add a liveness probe on
/missingand watch the restart count climb. - Remove the broken liveness probe. Add a startup probe with failureThreshold 12 and periodSeconds 10 (up to 2 minutes).
- Read each probe's results in the pod's events with
kubectl describe pod.
Commands
# In the container spec
startupProbe:
httpGet: {path: /healthz, port: 8080}
periodSeconds: 10
failureThreshold: 12
readinessProbe:
httpGet: {path: /ready, port: 8080}
periodSeconds: 5
livenessProbe:
httpGet: {path: /healthz, port: 8080}
periodSeconds: 10
failureThreshold: 3
kubectl describe pod <pod> | grep -A3 -i probe
Watch out for
- Cost
- Free.
- Security
- Health endpoints shouldn't leak versions, secrets or internal details.
- Common mistakes
- Liveness probes that call the database, the same strict check for liveness and readiness, and timeouts shorter than the app's normal response time.
Clean up
Delete the deployment.
Words to know
- Readiness probe
- Decides whether a pod gets traffic; failing removes it from the Service.
- Liveness probe
- Decides whether a container is stuck; failing restarts it.
- Startup probe
- Waits for a slow app to start before the other probes begin.
- failureThreshold
- How many failed checks in a row count as a failure.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On