Script Valley
Kubernetes: From Containers to Clusters
Core Workload Resources: Pods, Deployments, and ReplicaSetsLesson 2.5

Kubernetes liveness and readiness probes: how to handle unhealthy pods

liveness probe definition, readiness probe definition, startup probe, HTTP probe, TCP probe, exec probe, probe parameters, initialDelaySeconds, periodSeconds, failureThreshold, probe failure behavior

Two Different Questions About Health

Kubernetes liveness vs readiness probe comparison

Kubernetes uses two probes to monitor containers. Liveness: is this container still working, or is it deadlocked? If the liveness probe fails, Kubernetes restarts the container. Readiness: is this container ready to accept traffic? If the readiness probe fails, Kubernetes removes the Pod from the Service's endpoints — no traffic is sent to it — but it is not restarted.

Configuring HTTP Probes

containers:
- name: api
  image: my-api:2.0
  livenessProbe:
    httpGet:
      path: /healthz
      port: 8080
    initialDelaySeconds: 10   # wait before first check
    periodSeconds: 10          # check every 10s
    failureThreshold: 3        # restart after 3 consecutive failures
  readinessProbe:
    httpGet:
      path: /ready
      port: 8080
    initialDelaySeconds: 5
    periodSeconds: 5
    failureThreshold: 2

Exec and TCP Probes

# Exec probe: run a command inside the container
livenessProbe:
  exec:
    command: ["sh", "-c", "pg_isready -U postgres"]
  periodSeconds: 15

# TCP probe: check if a port is open
readinessProbe:
  tcpSocket:
    port: 6379
  periodSeconds: 10

Use a startup probe for slow-starting containers (JVM apps, legacy monoliths) to give them time to boot before liveness checks begin. Set startupProbe.failureThreshold high enough to cover your worst-case startup time.