Review the need for container liveness probes

Use liveness probes for unhealthy states that a restart can resolve.

Description

A running process can stop responding because of a deadlock. Use a liveness_probe when restarting can restore the application. If the process exits, restart_policy governs what happens next.

Potential impact

Without a check, a hung application may need a manual restart. A check that fails whenever a shared external service goes down can repeatedly restart healthy containers.

Remediation

Check the application itself using HTTP, TCP or exec. Set the period and failure threshold for its recovery time, and use a startup probe for slow initialization. Use readiness to decide when to send traffic.

Examples

Configure a maintained nginx image to serve /health on port 80. The default image does not provide that endpoint.

Before

hcl
resource "kubernetes_pod" "app" {
  metadata {
    name = "app"
  }

  spec {
    container {
      name  = "app"
      image = "nginx:1.27"

      port {
        container_port = 80
      }
    }
  }
}

After

hcl
resource "kubernetes_pod" "app" {
  metadata {
    name = "app"
  }

  spec {
    container {
      name  = "app"
      image = "nginx:1.27"

      port {
        container_port = 80
      }

      liveness_probe {
        http_get {
          path = "/health"
          port = 80
        }

        initial_delay_seconds = 10
        period_seconds        = 10
      }
    }
  }
}

The second Pod starts checking /health after 10 seconds and repeats every 10 seconds. When consecutive failures reach the threshold, the kubelet restarts this Pod’s container.

References