How to Set Up Liveness and Readiness Probes in Kubernetes

Liveness and readiness probes let Kubernetes make informed decisions about container health and traffic routing — correctly configured, they significantly improve application reliability. This guide covers setting them up properly.

Liveness vs Readiness: The Key Distinction

Probe TypeQuestion It AnswersAction on Failure
LivenessIs the container in a state requiring restart?Kubernetes restarts the container
ReadinessIs the container ready to receive traffic?Pod removed from service endpoints (not restarted)

Why Both Matter Separately

A container might be genuinely alive (no need to restart) but temporarily not ready for traffic (still warming up, or temporarily overloaded) — conflating these into a single probe type leads to either unnecessary restarts or serving traffic to genuinely unready pods.

Basic HTTP Liveness Probe

livenessProbe:
  httpGet:
    path: /healthz
    port: 8080
  initialDelaySeconds: 15
  periodSeconds: 20

initialDelaySeconds gives the application time to start before the first check — a common source of the crash-debugging scenario in How to Debug a Crashing Pod in Kubernetes when set too aggressively short.

Basic HTTP Readiness Probe

readinessProbe:
  httpGet:
    path: /ready
    port: 8080
  initialDelaySeconds: 5
  periodSeconds: 10

Implementing a Meaningful Health Check Endpoint

app.get('/healthz', (req, res) => res.status(200).send('OK'));

app.get('/ready', async (req, res) => {
  try {
    await db.query('SELECT 1');
    res.status(200).send('Ready');
  } catch {
    res.status(503).send('Not Ready');
  }
});

A liveness check should be simple/fast (just confirming the process isn't deadlocked); a readiness check can be more thorough, verifying genuine dependencies (database connectivity, for example) are actually available.

Avoiding a Common Mistake: Liveness Checking Dependencies

A liveness probe that checks database connectivity is problematic — if the database is temporarily down, Kubernetes would restart the application repeatedly (which won't fix a database outage), potentially making the situation worse; reserve dependency checks for readiness, not liveness.

TCP and Command-Based Probes (Alternatives to HTTP)

livenessProbe:
  tcpSocket:
    port: 5432
livenessProbe:
  exec:
    command: ["cat", "/tmp/healthy"]

For applications without an HTTP endpoint, TCP socket checks or command execution provide alternative probe mechanisms.

Tuning Probe Timing Appropriately

livenessProbe:
  initialDelaySeconds: 30
  periodSeconds: 10
  failureThreshold: 3
  timeoutSeconds: 5

failureThreshold requires multiple consecutive failures before action is taken — avoids overreacting to a single transient blip; tune based on your application's genuine startup time and normal response characteristics.

Using Startup Probes for Slow-Starting Applications

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

For applications with genuinely slow startup, a dedicated startup probe (with more lenient timing) runs before liveness/readiness probes take over, avoiding the need to set an overly generous initialDelaySeconds on your regular probes.

Common Errors

Pod repeatedly restarts despite the application seeming to work fine — almost always indicates an overly aggressive liveness probe configuration; increase initialDelaySeconds and failureThreshold, and verify your liveness endpoint genuinely reflects "needs restart" rather than transient conditions.

Continue Reading

Browse more articles in Kubernetes & Container Orchestration.

  • kubernetes liveness readiness probes, kubernetes health check, startup probe kubernetes, k8s probe configuration
  • 0 Benutzer fanden dies hilfreich
War diese Antwort hilfreich?

Verwandte Artikel

What Is Kubernetes and When Do You Need It on a VPS?

Kubernetes is a container orchestration platform — it automates deploying, scaling, and...

How to Install a Single-Node Kubernetes Cluster with k3s

k3s is a lightweight, certified Kubernetes distribution designed to run efficiently on modest...

How to Install kubeadm and Set Up a Multi-Node Kubernetes Cluster

kubeadm is the official tool for bootstrapping a standard, full-featured Kubernetes cluster. This...

Kubernetes Pods, Deployments & Services Explained

Understanding these three core Kubernetes objects — Pods, Deployments, and Services —...

How to Expose Applications with a Kubernetes Ingress Controller (Nginx Ingress)

An Ingress lets you route external HTTP/HTTPS traffic to multiple services within your cluster...