Sub topic 2
Health checks and readiness: knowing when an instance can take traffic
This chapter assumes you can run a containerized web service behind a load balancer and read basic Kubernetes YAML. You should know what a Deployment and a Service are. Every rollout strategy in this tutorial depends on one question: is this instance ready to take traffic? Health checks answer it.
Why a running process is not a ready process
A container can be running and still be unable to serve requests. It may be loading configuration, warming a cache, or waiting for a database connection. If the load balancer sends traffic to it too early, users see errors.
During a deployment this matters more than usual. New instances start constantly, and the rollout machinery decides when to retire old ones. If it counts a new instance as healthy too soon, it removes old capacity before the replacement can carry the load.
A health check is a small test the platform runs against an instance to decide what to do with it. Kubernetes calls this test a probe.
Three kinds of probes
Kubernetes has three probes. They answer different questions, and mixing them up is a common source of outages.
| Probe | Question it answers | Action when it fails |
|---|---|---|
| Startup | Has the application finished starting? | Keeps other probes off. Restarts the container if it never passes. |
| Readiness | Can this instance take traffic right now? | Removes the pod from Service endpoints. Does not restart it. |
| Liveness | Is this process stuck beyond recovery? | Restarts the container. |
Readiness is the probe that rollouts depend on. A failing readiness probe only stops traffic. The instance keeps running, so it can recover, for example when a dependency comes back.
A readiness probe in a rolling update
The Deployment below replaces pods one at a time. Setting maxUnavailable: 0 means the rollout must never drop below four ready pods. Setting maxSurge: 1 allows one extra pod during the update.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 4
selector:
matchLabels:
app: web
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: example/web:2.0
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 5The readiness probe sends an HTTP GET to /healthz every 5 seconds. A pod counts as ready only after the probe succeeds. The rollout then proceeds like this:
- Kubernetes starts one new pod, making five in total.
- The new pod is not ready, so it receives no traffic and no old pod is removed.
- The probe passes. The pod joins the Service endpoints.
- Only now does Kubernetes terminate one old pod.
- The cycle repeats until all four pods run version 2.0.
If the new version never becomes ready, the rollout stalls. Old pods keep serving traffic, so users are unaffected. You can watch this with kubectl rollout status deployment/web.
What a good readiness endpoint checks
The endpoint should reflect whether this instance can do its job. Some guidelines:
- Return success only when the app has finished initialization and can accept requests.
- Check dependencies the instance cannot work without, such as its main database connection.
- Keep the check fast and cheap. It runs every few seconds on every pod.
- Do not make it depend on every downstream service. If a shared dependency fails, every pod turns unready at once and the whole service goes offline.
Common errors
- No readiness probe. Kubernetes treats a pod as ready as soon as its container starts. Traffic arrives before the app is listening, and the rollout can replace old pods too early.
- A probe that always returns 200. It passes while the app is broken, so a bad version spreads through the whole rollout.
- Slow startup with a short timeout. The probe fails repeatedly and liveness restarts the pod forever. Use a startup probe or a longer initial delay.
- Probe on the wrong port or path. The pod never becomes ready and the rollout hangs.
Readiness also protects blue-green and canary releases. Before you switch traffic to a new environment or send a share of requests to a canary, you want its instances to be passing readiness.
Exercise
A rolling update uses maxUnavailable: 0 and maxSurge: 1, but the Deployment has no readiness probe. What risk does this create, and what does adding a probe change?
Show answer
Without a probe, Kubernetes treats each new pod as ready as soon as its container starts, even if the app is still loading. It then terminates an old pod, so capacity can fall below what is needed and users may get errors. With a readiness probe, a new pod receives traffic and counts toward the ready total only after the probe passes, so an old pod is removed only when a working replacement exists.
Exercise
During an outage of a shared database, all pods fail a readiness check that tests the database. What happens to the Service, and why might this be a design problem?
Show answer
Every pod is removed from the Service endpoints, so the Service has no backends and all requests fail. This is a problem if some requests could be served without the database, such as cached or static content. A readiness check should cover only dependencies the instance truly cannot work without.