Kubernetes Probes: Liveness Restarts, Readiness Reroutes, Startup Buys Time

Two pods go unhealthy at 2 a.m. The first has a deadlocked request handler: the process is alive, the port accepts connections, and nothing ever completes. The second is overloaded: still serving every request correctly, just 800 ms slower than usual. Kubernetes has an answer for both — and the fastest way to turn one incident into an outage is to answer both with the same probe. Liveness restarts containers; readiness reroutes traffic. Confusing the two is how you get a pod that is being killed and removed from the Service at the same moment, under load, at scale.

This post walks through what each probe actually controls, the defaults that quietly set your failure timing, the cascading-failure trap that a badly tuned liveness probe guarantees, and the configuration patterns that hold up in production.

Three probes, two different jobs

The kubelet runs each probe periodically against the container and acts on the result. The three probe types share mechanisms but have completely different consequences:

  • Liveness answers “is this container recoverable only by restarting it?” When it fails enough times in a row, the kubelet kills the container and the pod’s restartPolicy takes over. Nothing about traffic changes directly — the container dies and comes back.
  • Readiness answers “should this pod receive traffic right now?” When it fails, the pod’s IP is removed from the EndpointSlices of every matching Service — connections stop being routed to it — but the container keeps running and keeps being probed. It can come back to full traffic without a restart.
  • Startup answers “has the application finished booting?” While a startup probe is configured and has not yet succeeded, liveness and readiness are suspended entirely. Once it succeeds once, it never runs again for that container’s lifetime.

That division of labor implies a rule of thumb: restart is a drastic remedy reserved for unrecoverable states like deadlocks or corrupted in-memory state, while most other trouble — slowness, dependency hiccups, cache warm-up — belongs in readiness, where the cost of being wrong is one pod briefly out of rotation instead of one pod destroyed.

What actually happens on failure

The sequencing matters when reasoning about an incident. A failing readiness probe just flips the pod’s Ready condition to false; the EndpointSlice controller withdraws the pod IP, in-flight connections finish, and new requests go elsewhere. The kubelet keeps probing, so recovery is automatic once checks pass again. If there is no readiness probe at all, the pod is always considered ready — and before initialDelaySeconds elapses, a defined readiness probe counts as failing, so a pod starts out-of-rotation and joins it only after first success.

A failing liveness probe (or startup probe) is a kill. After failureThreshold consecutive failures, the kubelet stops the container — giving it a grace period to shut down — and restarts it per policy. That grace period defaults to the pod-level terminationGracePeriodSeconds (30 seconds), but a liveness or startup probe can carry its own terminationGracePeriodSeconds that overrides the pod-level value. Probe-level override has been stable since v1.28, and the API server rejects it on readiness probes, which never kill anything.

The defaults set your failure timing

Every probe inherits these if you do not set them:

  • periodSeconds: 10 — one check every 10 seconds
  • timeoutSeconds: 1 — the check fails if no answer within 1 second
  • failureThreshold: 3 — three consecutive failures to act
  • successThreshold: 1 — one success to recover (must stay 1 for liveness and startup)
  • initialDelaySeconds: 0

Read those together and a picture emerges: with defaults, a liveness restart fires roughly 30 seconds after checks start failing (3 failures × 10 s period, each judged within 1 second), and a readiness withdrawal happens on the same schedule. Two of these defaults deserve individual suspicion.

The 1-second timeout is the classic foot-gun. It applies to the whole HTTP round trip — connect, request, response. An endpoint that answers in 150 ms normally can easily take two seconds when the pod is under load, the runtime is in GC, or the database is slow. That is precisely when you least want probes failing, and precisely when they do: liveness failures start killing pods, shrinking capacity, pushing more load onto the survivors, which makes their endpoints slower too. This is the cascading failure the Kubernetes docs explicitly warn about, and it is why liveness probes must be configured with care. Set timeoutSeconds to something your endpoint can meet on its worst day — 5 is a common choice — and keep failureThreshold generous for liveness.

exec probes cost real CPU. Each exec check forks processes inside the container. On high-density nodes with short periodSeconds, that overhead is measurable and shows up as node CPU burn. Prefer httpGet or gRPC for anything you check frequently; reserve exec for checks that genuinely need shell access.

Health endpoints: what to check, and what not to

Separate the two endpoints and make each answer its own question. The liveness endpoint (/healthz) should check only that the process can respond — no database ping, no dependency calls, no locks on external state. A liveness check that fails because PostgreSQL is down does not fix PostgreSQL; it just restarts every pod in the deployment while the database is already having a bad day. The readiness endpoint (/readyz) is where dependency checks belong, alongside application-level state: cache warm, migrations applied, consumer connected. A pod whose upstream dependency is gone can still answer its own health check, but it cannot do its job, so it should leave rotation — temporarily.

A minimal Go handler shows the separation:

var ready atomic.Bool // flipped true after migrations + warm-up

func registerHealth(mux *http.ServeMux, db *sql.DB) {
	// Liveness: process can answer. Never touches dependencies.
	mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK) // 2xx/3xx = pass
	})

	// Readiness: can this pod do its job right now?
	mux.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) {
		if !ready.Load() {
			http.Error(w, "warming up", http.StatusServiceUnavailable)
			return
		}
		ctx, cancel := context.WithTimeout(r.Context(), 300*time.Millisecond)
		defer cancel()
		if err := db.PingContext(ctx); err != nil {
			http.Error(w, "db unreachable", http.StatusServiceUnavailable)
			return
		}
		w.WriteHeader(http.StatusOK)
	})
}

One subtlety: readiness withdrawal happens at the endpoint layer, but load balancers and service meshes that route independently of Kubernetes endpoints — or anything doing direct pod-to-pod traffic — will not honor it. If your architecture routes around Services, probe semantics stop at the Service boundary.

A configuration that holds up

The standard pattern for an application with a slow boot — JVM warm-up, model loading, cache priming — is a startup probe that protects boot time while keeping the steady-state checks tight:

spec:
  terminationGracePeriodSeconds: 45
  containers:
  - name: api
    image: registry.example.com/api:1.7.2
    ports:
    - name: http
      containerPort: 8080
    startupProbe:
      httpGet: { path: /healthz, port: http }
      periodSeconds: 5
      failureThreshold: 60        # up to 300s to boot
    livenessProbe:
      httpGet: { path: /healthz, port: http }
      periodSeconds: 10
      timeoutSeconds: 5
      failureThreshold: 6         # ~60s of failures before restart
      terminationGracePeriodSeconds: 45  # override pod-level for probe kills
    readinessProbe:
      httpGet: { path: /readyz, port: http }
      periodSeconds: 5
      timeoutSeconds: 3
      failureThreshold: 3

Once the startup probe succeeds, liveness takes over with its own aggressive-but-fair budget, and boot slowness can never trigger a restart loop. If the container usually starts in more than initialDelaySeconds + failureThreshold × periodSeconds of the liveness probe, a startup probe is mandatory — otherwise you end up patching the problem with a huge liveness initialDelaySeconds, which blinds you to a genuinely wedged container during the entire delay.

Choosing the mechanism

Four mechanisms exist, and they answer different questions. httpGet passes on any 2xx–3xx status and is the default choice; keep the response body tiny, because the kubelet closes connections after 10 KiB and the resulting “connection reset by peer” log lines look exactly like real failures. tcpSocket only proves the port accepts connections — a wedged application whose listener still accepts sockets passes, which makes it useful mostly as a last resort for protocols that are not HTTP or gRPC. grpc speaks the gRPC health checking protocol and is stable since v1.27; if your services already expose grpc.health.v1.Health, it avoids inventing HTTP endpoints, with the caveat that the health port cannot be referenced by name. exec runs a command and checks for exit code 0 — flexible, but pays the fork cost on every check.

Wrapping up

The decision procedure is short. If your process crashes when something goes wrong, you may need no liveness probe at all — the restart policy already handles it. Give every pod a readiness probe tied to what it needs in order to serve. Add a startup probe whenever boot exceeds the liveness budget. Add liveness last, with a timeout it can meet under load and a failure threshold that tolerates a bad minute, and point it at an endpoint that knows nothing about your dependencies. Probes are levers for recovery; the goal is that a probe failure costs one pod one problem — never every pod their lives.

Leave a Reply

Your email address will not be published. Required fields are marked *