K8s by Example: Liveness Probes

Liveness probes detect broken containers. If the probe fails consecutively (failureThreshold times), Kubernetes kills and restarts the container. Use for: deadlocks, memory leaks, unrecoverable errors.

pod-liveness.yaml

HTTP probe is the most common choice. Use it when your app has an HTTP endpoint. Healthy if status is 200-399. initialDelaySeconds waits before first probe. periodSeconds sets interval. failureThreshold is consecutive failures before restart.

spec:
  containers:
    - name: app
      image: my-app:v1
      livenessProbe:
        httpGet:
          path: /health
          port: 8080
        initialDelaySeconds: 30
        periodSeconds: 10
        timeoutSeconds: 5
        failureThreshold: 3
pod-liveness-headers.yaml

HTTP probes can include custom headers for authentication or routing. Use scheme: HTTPS for TLS endpoints.

livenessProbe:
  httpGet:
    path: /healthz
    port: 8080
    scheme: HTTP
    httpHeaders:
      - name: X-Custom-Header
        value: probe
      - name: Authorization
        value: Bearer token
  initialDelaySeconds: 45
  periodSeconds: 30
pod-liveness-tcp.yaml

TCP probe checks if the port accepts connections. Use for non-HTTP services (databases, Redis, gRPC without health checks). Limitation: it only confirms the port is open, not that the service is healthy. Prefer HTTP or exec when possible.

livenessProbe:
  tcpSocket:
    port: 5432
  initialDelaySeconds: 15
  periodSeconds: 20
pod-liveness-exec.yaml

Exec probe runs a command inside the container. Healthy if exit code is 0. Use for custom health logic (checking files, running CLI tools like pg_isready). More flexible but slower and uses more resources than HTTP/TCP.

livenessProbe:
  exec:
    command:
      - sh
      - -c
      - pg_isready -U postgres
  initialDelaySeconds: 30
  periodSeconds: 10
pod-startup-probe.yaml

Startup probes replace liveness during container startup. Use for slow-starting apps instead of long initialDelaySeconds. Liveness probe only starts after startup probe succeeds. This example allows up to 5 minutes (30 × 10s) for startup.

spec:
  containers:
    - name: app
      startupProbe:
        httpGet:
          path: /health
          port: 8080
        failureThreshold: 30
        periodSeconds: 10
      livenessProbe:
        httpGet:
          path: /health
          port: 8080
        periodSeconds: 10
        failureThreshold: 3
pod-probe-params.yaml

Probe parameters: failureThreshold is consecutive failures before action. successThreshold is consecutive successes to recover (only for readiness). timeoutSeconds must be less than periodSeconds.

livenessProbe:
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 45
  periodSeconds: 30
  timeoutSeconds: 10
  failureThreshold: 3
  successThreshold: 1
pod-liveness-best.yaml

Common mistakes: (1) Too aggressive settings cause restart loops. Increase timeoutSeconds for slow endpoints. (2) Checking dependencies like databases or external APIs. If the DB is down, restarting won’t help and causes cascading failures. (3) Using liveness for apps that crash cleanly. Kubernetes already restarts crashed containers. Only use liveness for silent failures (deadlocks, hangs).

livenessProbe:
  httpGet:
    path: /health
    port: 8080
  periodSeconds: 15
  timeoutSeconds: 5
  failureThreshold: 3
terminal

Debug probe failures with events and logs. Container restarts increment the RESTARTS counter. Check probe endpoint directly with kubectl exec or port-forward.

$ kubectl get pods
NAME     READY   STATUS    RESTARTS   AGE
my-app   1/1     Running   3          10m

$ kubectl describe pod my-app | grep -A5 "Liveness probe failed"
Warning  Unhealthy  2m  Liveness probe failed: HTTP probe failed
Warning  Unhealthy  1m  Liveness probe failed: HTTP probe failed

$ kubectl exec my-app -- curl -v localhost:8080/health
< HTTP/1.1 500 Internal Server Error

$ kubectl port-forward pod/my-app 8080:8080
$ curl localhost:8080/health
{"status": "unhealthy", "reason": "database connection failed"}

Index | GitHub | Use arrow keys to navigate |