7 min readRishi

Kubernetes Liveness Probes That Restart a Process That Was Fine

Kubernetes Liveness Probes That Restart a Process That Was Fine

A probe is a yes or no question, and Kubernetes does a different thing with each answer. Mixing the questions up is how a database blip restarts every pod, or how a slow boot gets killed for not answering /healthz in the first second. The configure probes task is the reference. The failure mode is almost always the same: liveness was asked a readiness question.

Three probes, three outcomes

ProbeIf it failsWhat it is for
LivenessThe kubelet restarts the containerThe process is wedged, and a restart can help
ReadinessThe pod is removed from Service endpoints. The process keeps runningThe process should not receive traffic right now
StartupLiveness and readiness do not run until this succeeds. Enough failures and the container is restartedSlow boot that would otherwise look dead

Restart is a violent answer. The process loses in-flight work, dumps its caches, and then stampedes whatever it talks to. That is justified when the process cannot make progress on its own: a deadlock, a stuck event loop, a handler that will never return. It is not justified when Postgres is failing over. A restart does not create a healthy database. It creates N reconnect storms from the N pods you just killed.

Readiness is the lever for dependencies. Fail readiness, traffic stops, the process stays up, and it can pass again when the dependency returns. The pod looks idle in kubectl get pods (0/1 Ready) without entering CrashLoopBackOff. That is the state you want during a dependent outage.

Startup exists because boot is not deadlock. A JVM that needs forty seconds to load is not wedged. It is slow. If liveness is already ticking during those forty seconds, the kubelet kills a process that was about to be fine, and the next replica does the same thing.

Liveness that checks the database

The handler mapped to /healthz that SELECT 1s the database, pings Redis, and calls a downstream HTTP API is a readiness handler with the wrong name. Wire it to liveness and you have coupled the process lifetime to every dependency:

  1. The database pauses.
  2. Every pod's liveness fails within periodSeconds * failureThreshold.
  3. The kubelet restarts every container.
  4. The database comes back into a reconnect storm from the fleet you just bounced.

The liveness path should answer "can this process do work at all?" with no network call. A local /live that returns 200 if the HTTP server is accepting connections is enough for most services. If you have a more precise deadlock check — a watchdog the event loop must pet — keep that local too.

Readiness is allowed to touch a dependency, and it is allowed to flap. Tune failureThreshold and periodSeconds so a single slow query does not eject the pod, and so a real outage ejects it before users pile up on a handler that will 500.

One handler that does both jobs will be configured to the stricter of the two. You are then back to restarting on a dependency. Use two paths.

Slow starts need a startup probe, not a long initial delay

initialDelaySeconds waits a fixed time and then liveness is live. If boot sometimes takes longer than that delay, the kubelet kills a process that was still starting. You then raise the delay until the worst boot succeeds, and now a process that deadlocks at second two sits unexamined until the delay expires.

A startup probe disables liveness and readiness until it succeeds once. Kubernetes' own example is failureThreshold: 30 and periodSeconds: 10, which is five minutes of budget, after which liveness takes over and can still react quickly. Fast boots succeed on the first ticks. Slow boots get the remaining budget. Deadlocks after startup still hit liveness on a short period.

startupProbe:
  httpGet:
    path: /healthz
    port: 8080
  failureThreshold: 30
  periodSeconds: 10
livenessProbe:
  httpGet:
    path: /healthz
    port: 8080
  periodSeconds: 10
  failureThreshold: 3
readinessProbe:
  httpGet:
    path: /ready
    port: 8080
  periodSeconds: 5
  failureThreshold: 2

Startup and liveness can share a path when that path is the cheap local check. Readiness should not share it if /ready includes dependencies.

timeoutSeconds defaults to 1. A health handler that queries three services will exceed that and look dead, even if the process is fine. If readiness must check a dependency, give the handler a timeout shorter than timeoutSeconds, and set timeoutSeconds high enough that a slow-but-alive check is a failed readiness, not a timed-out probe. A timed-out probe is a failed probe.

successThreshold must be 1 for liveness and startup. You cannot require two consecutive successes before the container is considered alive. Readiness can use a higher successThreshold if you want a pod to prove itself twice before taking traffic after a flap.

httpGet, tcpSocket, exec

httpGet against a local port is the usual check. Point it at the container port the process actually listens on. The kubelet talks to the container network namespace — do not send the probe through an ingress.

tcpSocket only asks whether something accepts a connection. It will pass for a process that is listening and wedged on every request. exec forks a process in the container on every tick — a real fork/exec. If the binary is a shell script that curls localhost, you now have exec plus HTTP with extra failure modes. Use httpGet.

When not to add a liveness probe

If the process exits on its own when it is wedged, Kubernetes already restarts it. A liveness probe that can false-fail is worse than none. Batch jobs, one-shot migrations, and sidecars that do not serve traffic often should not have liveness at all. A sidecar with a failing liveness restarts that container and can take down the whole pod for a log-shipper blip.

If you cannot write a liveness check that is local, cheap, and true only when a restart would help, omit liveness and keep readiness. Do not probe a cache warmer or "has this replica received a message" as liveness — those are startup or readiness conditions. Do not copy probe timings from another service. Pick the budget from the blast radius of a restart.

What the restart loop looks like in the alert

CrashLoopBackOff with probe-failed events in kubectl describe pod is liveness or startup. The container is dying because you asked the kubelet to kill it. Ready 0/1 with a running container is readiness. Those two pages must not be the same page.

A pod that is not ready should page only if it stays that way, or if too many pods in the Service are unready. Crash-looping is a different page; the rest is in alerts that don't cry wolf. If the liveness path contains a dependency, fix the probe before you tune the alert.

  • Liveness hits a local handler. No database, no Redis, no downstream HTTP.
  • Readiness may check dependencies. It has its own path.
  • Startup probe covers the worst realistic boot. initialDelaySeconds is not a substitute.
  • timeoutSeconds is greater than the handler's own timeout, and the default of 1 has been considered.
  • Probes are httpGet (or grpc) to a container port, not exec curl.
  • A failed liveness is a restart you are willing to take in the middle of a deploy and in the middle of the night.
  • Alerts distinguish CrashLoopBackOff from a fleet that is unready together.

The kubelet is obedient. It will restart a healthy process as many times as the probe tells it to. Ask the process a question whose "no" really means "kill this."

Keep reading

Newsletter

New posts, straight to your inbox

One email when a new post goes up. Double opt-in, unsubscribe anytime. Not a daily digest.

Comments

  • No comments yet. Be the first.