Workloads: Deployments, rollouts and jobs · 5 MIN READ

Readiness, liveness and startup probes: routing traffic and restarting stuck containers

Choose the right probe for each question, configure its timing, implement /readyz and /healthz handlers in a Go service, and avoid the probe settings that cause outages.

Kubernetes knows only that a container's process is running. Whether the application can serve traffic yet, and whether it is still making progress, it learns from probes that the kubelet runs on the node. Each probe answers a different question and triggers a different action, and confusing them is one of the most common causes of self-inflicted downtime.

kubernetes · A MAP OF THE IDEA

  1. Startup: allow a slow boot
  2. Readiness: gate traffic
  3. Liveness: restart if stuck
  4. Clear ready on SIGTERM

Useful when

  • Rollouts and Services only use Pods that are actually ready.
  • A hung process is restarted without anyone being paged.

Watch out

  • Liveness that checks dependencies causes restart storms.
  • Tight timeouts restart healthy Pods under load.

Implementation & operation cost: Two small HTTP handlers in every service and probe settings tuned to its real start-up and response times.

01

Three probes, three consequences

A readiness probe asks whether the container should receive traffic now. While it fails, the Pod is not ready and is removed from the EndpointSlices of every Service that selects it, but nothing is restarted; it runs for the container's whole life, so a Pod can leave and rejoin rotation. Readiness also drives rollouts, because a new Pod counts as available only once it is ready. A liveness probe asks whether the container is stuck beyond recovery: after failureThreshold consecutive failures the kubelet kills it and restarts it according to restartPolicy. A startup probe protects slow starters: until it succeeds, liveness and readiness probes are not run, and if it does not succeed within its budget the container is killed and restarted.

02

Probe mechanics and timing

A probe is an httpGet, where any status from 200 to 399 passes, a tcpSocket connection, an exec command that must exit 0, or a gRPC health check. The kubelet runs it from the node against the Pod IP. periodSeconds (default 10) sets the interval, timeoutSeconds (default 1) the deadline for one attempt, and failureThreshold (default 3) and successThreshold (default 1, always 1 for liveness and startup) how many consecutive results flip the state. Worst-case detection is roughly periodSeconds times failureThreshold, so the liveness probe below restarts a hung container after about 30 seconds, and the startup probe allows 5 x 24 = 120 seconds to start. The GopherForge lab terminal simulates this too: while a startupProbe is failing, liveness and readiness wait, and a startup probe that never passes gets the container restarted.

yaml
containers:
- name: web
  image: gopherforge/web:v2
  ports:
  - name: http
    containerPort: 8080
  startupProbe:
    httpGet:
      path: /healthz
      port: http
    periodSeconds: 5
    failureThreshold: 24     # up to 120 s to start
  readinessProbe:
    httpGet:
      path: /readyz
      port: http
    periodSeconds: 5
    failureThreshold: 2
  livenessProbe:
    httpGet:
      path: /healthz
      port: http
    periodSeconds: 10
    timeoutSeconds: 2
    failureThreshold: 3
Read the code

The startup probe gives the process up to two minutes to answer /healthz before liveness checks begin. Readiness uses its own endpoint and reacts within about ten seconds, taking the Pod out of rotation without a restart. Liveness tolerates two-second responses and three misses in a row before restarting. With a startup probe in place, initialDelaySeconds is rarely needed on the other two.

03

Designing /healthz and /readyz in Go

Liveness should answer one question only: can this process still make progress? Check state local to the process, such as a heartbeat that the main worker loop updates, and never call the database or another service from it. Readiness can be stricter: return 503 until caches are warm and configuration is loaded, and again as soon as shutdown begins, so the Pod leaves the endpoints while in-flight requests finish. Keep both handlers cheap and free of locks that request handlers hold, or a busy server fails its own probes.

go
var (
	ready    atomic.Bool  // set after warm-up, cleared on SIGTERM
	lastBeat atomic.Int64 // unix seconds, updated by the worker loop
)

func healthz(w http.ResponseWriter, r *http.Request) {
	if time.Since(time.Unix(lastBeat.Load(), 0)) > 30*time.Second {
		http.Error(w, "worker loop stalled", http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusOK)
}

func readyz(w http.ResponseWriter, r *http.Request) {
	if !ready.Load() {
		http.Error(w, "not ready", http.StatusServiceUnavailable)
		return
	}
	w.WriteHeader(http.StatusOK)
}
Read the code

healthz fails only when the worker loop has not reported for 30 seconds, a condition a restart can actually fix. readyz is under the application's control: call ready.Store(true) after warm-up, and ready.Store(false) first thing in the SIGTERM handler, before http.Server.Shutdown. The atomic values keep both handlers lock-free, so they still answer when request handlers are blocked.

04

Probe mistakes that cause outages

A liveness probe that checks a dependency turns a database outage into a restart storm: every replica restarts at once, caches go cold and recovery takes longer. A readiness probe that checks a shared dependency has a similar trap, since all Pods leave the Service together and clients get connection errors instead of a clear 503; check shared dependencies only when routing to another Pod could help. Aggressive thresholds, such as a one-second timeout with failureThreshold: 1, restart healthy containers during garbage-collection pauses or CPU throttling and push more load onto the survivors. A liveness probe on a slow starter without a startup probe kills it before it ever serves. And without any readiness probe, a container receives traffic the moment its process starts, before it listens.

05

Try it in the lab terminal

gopherforge/web:slow needs 25 seconds before it listens. First deploy it without probes and send traffic; then apply a manifest with a readiness probe, which replaces the Pods through a rolling update while traffic keeps flowing. Finally, gopherforge/web:deadlock stops answering 30 seconds after it starts, and a liveness probe with a 5-second period and a threshold of 3 restarts it. cluster tick advances virtual time so you do not have to wait.

bash
kind create cluster
kubectl create deployment web --image=gopherforge/web:slow --replicas=2 --port=8080
kubectl expose deploy web --port 80 --target-port 8080
kubectl rollout status deploy/web
traffic run svc/web --rps 20 --duration 25s
cat > web.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: web
        image: gopherforge/web:slow
        ports:
        - containerPort: 8080
        readinessProbe:
          httpGet:
            path: /readyz
            port: 8080
          periodSeconds: 5
EOF
kubectl apply -f web.yaml
traffic run svc/web --rps 20 --duration 60s
kubectl get pods
cat > stuck.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: stuck
spec:
  replicas: 1
  selector:
    matchLabels:
      app: stuck
  template:
    metadata:
      labels:
        app: stuck
    spec:
      containers:
      - name: web
        image: gopherforge/web:deadlock
        ports:
        - containerPort: 8080
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
          periodSeconds: 5
          failureThreshold: 3
EOF
kubectl apply -f stuck.yaml
cluster tick 60s
kubectl get pods -l app=stuck
kubectl describe pod -l app=stuck
Read the code

Without a probe, rollout status succeeds within seconds and both Pods count as ready, yet traffic run reports 13.0% ok with 435 requests refused: the Service sent traffic to Pods that were not listening yet. During the probed rollout, READY PODS reads 2/3 and the run ends 100.0% ok, because each new Pod joins the endpoints only once /readyz passes while the old Pods keep serving. The stuck Pod shows RESTARTS 1 and Last State: Terminated, and its events list Unhealthy warnings for the liveness probe followed by Killing: Container web failed liveness probe, will be restarted.

TAKE THIS WITH YOU

Readiness decides whether a Pod gets traffic, liveness whether its container is restarted, and startup how long it may take to begin. Give every server a readiness probe, keep liveness local and lenient, and never let a dependency outage restart your Pods.

Check your understanding

A Go service's readiness probe starts failing while its liveness probe keeps passing. What does Kubernetes do?

Go to the source

Progress stays in this browser. No account needed.