Home KubernetesKubernetes Liveness and Readiness Probes: A Practical Guide

Kubernetes Liveness and Readiness Probes: A Practical Guide

by Knowledge Base Linux
Kubernetes Liveness and Readiness Probes — Kubernetes Liveness and Readiness Probes: A Practical Guide

If you have ever watched a Kubernetes pod report Running while the application inside was completely frozen, you already understand why health checks matter. Kubernetes can see that a container process exists, but it cannot tell whether your web server is actually answering requests, whether a deadlock has locked up your application threads, or whether your API is returning 500 errors to every caller. That gap is exactly what kubernetes probes close: small, periodic health checks the kubelet runs against your containers, driving the two most important automated decisions Kubernetes makes — when to restart a container and when to send it traffic.

Diagram of kubernetes probes liveness and readiness checks flowing into pod containers

In this practical guide you will learn what liveness, readiness, and startup probes do, how to configure the three common probe handlers (httpGet, exec, and tcpSocket), how to tune timing fields like initialDelaySeconds and failureThreshold, and how misconfigured probes become one of the most common causes of CrashLoopBackOff. Every example reflects current Kubernetes behavior and is ready to paste into your own manifests.

Prerequisites – Kubernetes Liveness and Readiness Probes

  • A Kubernetes cluster (v1.25 or newer) you can deploy to — kind, minikube, or a managed cluster all work
  • kubectl installed and configured against that cluster
  • Basic familiarity with pods and Deployments — if you need a refresher, see our Kubernetes Deployment and ReplicaSet explained guide
  • An application image exposing HTTP health endpoints (the examples use /healthz and /ready)

What Are Kubernetes Probes? – Kubernetes Liveness and Readiness Probes

A kubernetes probe is a diagnostic check the kubelet performs against a container on a schedule you define. Each probe has two parts: a handler that performs the check (an HTTP request, a TCP connection, or a command executed inside the container) and a set of timing fields that control how often the check runs and how many failures trigger an action.

Kubernetes defines three probe types, each answering a different question:

  • Liveness probe — “Is the application still alive?” If the liveness probe fails, the kubelet kills the container and restarts it according to the pod’s restartPolicy.
  • Readiness probe — “Is the application ready to receive traffic?” If the readiness probe fails, the kubelet marks the pod NotReady and removes it from the endpoints of any Service. The container keeps running; it simply stops receiving requests until it recovers.
  • Startup probe — “Has the application finished starting?” When a startup probe is defined, liveness and readiness probes stay disabled until it succeeds. This gives slow-starting applications — large JVMs, services warming caches — a long grace period without forcing an enormous initialDelaySeconds on the other probes.

The distinction between liveness and readiness is the heart of kubernetes probes: liveness controls the container lifecycle (restart or not), while readiness controls traffic flow (send requests or not).

Kubernetes Probe Handlers: httpGet, exec, and tcpSocket

Every probe needs a handler that defines how the check is performed. Kubernetes supports four handlers; the three you will use most are httpGet, exec, and tcpSocket (a fourth, grpc, targets gRPC services implementing the health-checking protocol).

httpGet: Probe an HTTP Endpoint

The most popular handler. The kubelet sends an HTTP GET request and considers the probe successful for any status code between 200 and 399. Anything else — including connection refused — counts as a failure.

livenessProbe:
  httpGet:
    path: /healthz
    port: 8080
    httpHeaders:
      - name: X-Health-Check
        value: kubelet
  initialDelaySeconds: 15
  periodSeconds: 10
  timeoutSeconds: 3
  failureThreshold: 3

Best practice: expose a dedicated, lightweight endpoint such as /healthz that does the minimum work needed to prove the process is functional. Do not point liveness at an endpoint that queries a database or a downstream API — an outage in that dependency would restart all of your pods.

exec: Run a Command Inside the Container

The exec handler runs a command inside the container; exit code 0 means success and anything else means failure. It suits checks with no network endpoint, such as verifying a marker file or running a database client ping.

livenessProbe:
  exec:
    command:
      - cat
      - /tmp/healthy
  initialDelaySeconds: 5
  periodSeconds: 5

Keep in mind that exec probes spawn a new process inside the container on every check. On clusters with many pods and a short periodSeconds, that overhead adds up — prefer httpGet or grpc when you can.

tcpSocket: Check That a Port Accepts Connections

The tcpSocket handler opens a TCP connection to the given port. It fits non-HTTP workloads such as databases and message brokers, where a successful connection is a reasonable health signal.

readinessProbe:
  tcpSocket:
    port: 5432
  initialDelaySeconds: 5
  periodSeconds: 10
  timeoutSeconds: 2

startupProbe: Protect Slow-Starting Applications

A startup probe uses the same handlers but with a generous failure budget, so a slow application can take minutes to boot without being killed:

startupProbe:
  httpGet:
    path: /healthz
    port: 8080
  periodSeconds: 10
  failureThreshold: 30

This configuration gives the container up to 300 seconds (failureThreshold × periodSeconds) to start. Once the startup probe succeeds, it steps aside permanently and the liveness and readiness probes take over.

Tuning Kubernetes Probe Timing and Thresholds

Five timing fields are shared across all kubernetes probes:

  • initialDelaySeconds — seconds to wait after the container starts before the first check. Defaults to 0.
  • periodSeconds — how often to run the probe. Defaults to 10; minimum is 1.
  • timeoutSeconds — how long one check may take before it counts as a failure. Defaults to 1; minimum is 1.
  • successThreshold — consecutive successes required before a failed probe is considered passing again. Defaults to 1, and must be 1 for liveness and startup probes; only readiness probes may raise it.
  • failureThreshold — consecutive failures before Kubernetes acts. Defaults to 3.

Two formulas help you size these values:

  • Time to detect a failure is approximately periodSeconds × failureThreshold. With the defaults, a genuinely dead container is detected in about 30 seconds.
  • Maximum startup allowance is approximately initialDelaySeconds + (failureThreshold × periodSeconds). Measure your application’s real startup time under load first, then set the budget above the worst case — not below the average.

The default timeoutSeconds: 1 is one of the most common sources of phantom restarts: on a busy node or during a garbage-collection pause, a healthy endpoint can take longer than a second to answer. A value of 2–5 seconds is a safer default for most services.

How Bad Kubernetes Probes Cause CrashLoopBackOff

Some of the nastiest production incidents start with an innocent-looking probe. The pattern is always the same: the kubelet kills a container it believes is unhealthy, the replacement fails the same check, and after a few cycles the pod lands in CrashLoopBackOff with an exponentially growing delay between restarts. If you are seeing that state right now, our CrashLoopBackOff troubleshooting guide walks through the full diagnosis — but check these probe mistakes first:

  • Probing the wrong port or path. The app listens on 8080 but the probe targets 80, or the /healthz endpoint was renamed in a new release. The pod restarts forever while the app itself is fine.
  • initialDelaySeconds shorter than real startup time. The first probes fail while the app is still booting, and three quick failures trigger a restart before the app ever had a chance. Use a startup probe instead of guessing a delay.
  • Liveness probe depends on an external service. A liveness endpoint that queries a database turns a database slowdown into a pod restart storm. Liveness should test only the process itself; put dependency checks in readiness.
  • timeoutSeconds left at the 1-second default. Under CPU pressure, healthy responses arrive late and the kubelet restarts containers that were never broken.
  • Copy-pasted probes from a different application. Timing that was perfect for a Go microservice can be fatal for a JVM monolith that needs two minutes to warm up.

Debugging Failing Probes with kubectl

When a probe misbehaves, kubectl describe pod shows you exactly what the kubelet saw:

kubectl describe pod web-7d9c4f6b5-x2kzq

Look in the Events section for lines like this:

Liveness probe failed: Get "http://10.244.1.5:8080/healthz": dial tcp 10.244.1.5:8080: connect: connection refused

That message tells you whether the failure was a connection problem, a bad status code, or a timeout — pointing at networking, the application, or the timing values respectively. A few more commands round out the toolkit:

# Watch the pod cycle in real time
kubectl get pod web-7d9c4f6b5-x2kzq -w

# Check recent events across the namespace
kubectl get events --sort-by=.lastTimestamp | tail -20

# Confirm the endpoint answers from inside the cluster
kubectl exec web-7d9c4f6b5-x2kzq -- curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/healthz

Kubernetes Probes Best Practices

  • Separate your endpoints. Give liveness a cheap /healthz and readiness a deeper /ready that may include dependency checks.
  • Prefer startup probes over huge delays. A startup probe adapts to actual startup time; a giant initialDelaySeconds just delays every restart by a fixed amount.
  • Set timeoutSeconds deliberately. 2–5 seconds suits most HTTP services; keep it at 1 only if you have measured that your endpoint always answers faster.
  • Keep liveness self-contained. Never let a liveness check depend on a database, cache, or downstream API.
  • Prefer httpGet over exec. Exec probes cost a process spawn per check per pod; HTTP checks are cheaper and easier to observe.
  • Remember successThreshold must be 1 for liveness and startup probes — the API server rejects anything else.
  • Test probes under load. A probe that passes in staging can time out in production when the node is saturated; load-test the endpoints before finalizing your tuning.

Conclusion

Kubernetes probes turn the kubelet from a process babysitter into an application-aware supervisor. Liveness probes restart containers that are truly stuck, readiness probes keep half-started pods out of your Service endpoints, and startup probes give slow applications the room they need to boot — all driven by a handful of well-understood timing fields. Get the handlers and thresholds right, and most CrashLoopBackOff mysteries disappear before they start. For the authoritative reference, see the official Kubernetes documentation on configuring liveness, readiness, and startup probes.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.