Kubernetes Health Checks

Configure Kubernetes liveness, readiness, and startup probes with HTTP, TCP, gRPC, and exec handlers to build self-healing, reliable applications.

Kubernetes health checks, implemented as container probes, enable the kubelet to periodically assess the runtime health of application containers and automate self-healing actions. By combining startup, liveness, and readiness probes, a cluster can seamlessly protect traffic from initialization delays, isolate failing instances from service load balancers, and restart unresponsive workloads.

The Three Probe Types

Each probe type fulfills a distinct operational role in the container lifecycle:

Probe Type Failure Action Evaluation Window Primary Problem Solved
Startup Probe Kills and recreates the container Runs at container boot; disables liveness/readiness until success Slow initialization without triggering premature liveness restarts
Liveness Probe Kills and recreates the container Runs continuously after startup probe succeeds Deadlocks, thread pool exhaustion, unrecoverable internal freezes
Readiness Probe Removes Pod IP from Service Endpoints (stops receiving traffic) Runs continuously after startup probe succeeds Cold caches, warm-up phases, temporary saturation or dependency outages

Probe Handlers and Timing Controls

Kubernetes supports four probe mechanisms:

  1. httpGet: Performs an HTTP GET request against the container IP (200-399 status is considered success).
  2. tcpSocket: Opens a TCP socket connection against a designated container port.
  3. grpc: Executes a remote procedure call using the standard gRPC Health Checking Protocol.
  4. exec: Runs a command inside the container namespace; exit code 0 indicates success.

Every probe uses the following timing fields:

  • initialDelaySeconds: Seconds to wait before executing the first probe after container launch.
  • periodSeconds: Frequency in seconds between probe executions (default 10).
  • timeoutSeconds: Seconds after which the probe attempt times out (default 1).
  • failureThreshold: Number of consecutive failures required before taking action (default 3).
  • successThreshold: Number of consecutive successes required after a failure (default 1).

Production Deployment Manifest

The following manifest demonstrates a production configuration with all three probe types and appropriate thresholds:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: order-service
  namespace: production
spec:
  replicas: 3
  selector:
    matchLabels:
      app: order-service
  template:
    metadata:
      labels:
        app: order-service
    spec:
      containers:
        - name: order-service
          image: order-service:3.1.0
          ports:
            - containerPort: 8080
          startupProbe:
            httpGet:
              path: /actuator/health/liveness
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 5
            failureThreshold: 12   # Allows up to 60s (12 * 5s) for slow boot
            timeoutSeconds: 2
          livenessProbe:
            httpGet:
              path: /actuator/health/liveness
              port: 8080
            periodSeconds: 10
            failureThreshold: 3
            timeoutSeconds: 3
          readinessProbe:
            httpGet:
              path: /actuator/health/readiness
              port: 8080
            periodSeconds: 5
            failureThreshold: 2
            successThreshold: 1
            timeoutSeconds: 2
          resources:
            requests:
              cpu: 250m
              memory: 512Mi
            limits:
              cpu: 1000m
              memory: 1Gi

Diagnosing Probes with kubectl

# Check Pod restart counts and probe status
kubectl get pods -n production

# Inspect probe failure events and reason codes
kubectl describe pod order-service-7f6c99c89-w59kz -n production
# Events:
#   Warning  Unhealthy  10s  kubelet  Liveness probe failed: HTTP probe failed with statuscode: 500
#   Normal   Killing    10s  kubelet  Container order-service failed liveness probe, will be restarted

# Check whether Pod is included in Service endpoints
kubectl get endpoints order-service -n production

Common Mistakes

  • Checking downstream databases in Liveness Probes: If the shared database undergoes maintenance or fails, all dependent application Pods fail liveness checks simultaneously, entering an infinite restart loop (thundering herd problem).
  • Identical Liveness and Readiness configurations: Using the same endpoint without distinguishing internal runtime health from service readiness causes unnecessary container restarts during transient spikes.
  • Overly aggressive timeout thresholds: Setting timeoutSeconds: 1 on heavily loaded applications causes transient CPU latency to be misdiagnosed as crashes, creating cascading restarts under high load.
  • Using large initialDelaySeconds instead of Startup Probes: Large initial delays leave the application unmonitored if it boots quickly and fails shortly thereafter.

Best Practices

  • Keep Liveness Probes lightweight, checking only internal process health (memory exhaustion, deadlocked event loops) with zero remote network calls.
  • Use startupProbe with an appropriate failureThreshold to accommodate slow JVM or runtime initialization safely.
  • Keep Readiness Probes targeted at whether the container can currently process user requests (e.g., local cache initialized, ready to accept connections).
  • Configure successThreshold: 1 and failureThreshold: 3 for fast recovery and resilience against transient network blips.

Interview Questions

1. What is the operational difference between a Liveness Probe and a Readiness Probe?

A Liveness Probe tells Kubernetes whether the container process is alive and healthy; if it fails, the kubelet kills and restarts the container. A Readiness Probe tells Kubernetes whether the container is ready to accept incoming network traffic; if it fails, Kubernetes removes the Pod IP from the Service endpoints list so no traffic is routed to it, without restarting the container.

2. Why was the Startup Probe introduced in Kubernetes?

Before Startup Probes, slow-starting applications required setting a huge initialDelaySeconds on Liveness Probes. If the application took 2 minutes to start, the liveness probe had to wait 2 minutes before ever checking, even if the app crashed after 10 seconds. Startup Probes poll the application until it completes initialization; once successful, liveness and readiness take over immediately.

3. Why is it an anti-pattern to check external databases in a Liveness Probe?

If an external database becomes unreachable or slow, all backend Pods checking that database will fail their liveness probes simultaneously. The kubelet will kill and restart every Pod in the cluster. When they reboot, they will immediately hammer the struggling database during startup, preventing both the database and the services from ever recovering.

4. What are the four probe mechanisms supported in modern Kubernetes?

Kubernetes supports httpGet (HTTP request checking status code 200-399), tcpSocket (checks if a TCP port is open and accepting connections), exec (runs a shell command inside the container and checks for exit code 0), and grpc (queries the standard gRPC health check protocol).

5. What happens behind the scenes when a Readiness probe fails?

When a Readiness probe fails, the endpoint controller removes the Pod's IP address and port from the Endpoints (or EndpointSlice) object corresponding to any matching Service. Ingress controllers and kube-proxy stop routing new requests to that Pod until the readiness probe reports consecutive successes matching successThreshold.

6. How do you calculate the maximum startup time allowed by a Startup Probe?

The maximum allowed startup time is calculated as: $$\text{Max Startup Window} = \text{initialDelaySeconds} + (\text{periodSeconds} \times \text{failureThreshold})$$ For example, with initialDelaySeconds: 10, periodSeconds: 5, and failureThreshold: 30, the container has up to $10 + (5 \times 30) = 160$ seconds to boot before being killed.

Summary

  • Startup probes guard slow-starting containers without compromising liveness reaction time.
  • Liveness probes restart stuck or deadlocked processes.
  • Readiness probes control ingress and service traffic routing without terminating Pods.
  • Never test third-party or shared database dependencies inside liveness probes.

Continue Learning

Previous: Resource Limits | Next: Auto Scaling HPA

Was this guide helpful?

CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.