Browse topics

Liveness, readiness and startup probes

Version
1.37
Review
Draft

Summary#

The kubelet runs probes against a container. A failing readiness probe marks the Pod unready. A startup probe holds back the other two until it passes. When liveness or startup failures reach the threshold, the kubelet kills the container and applies its restart policy.

Syntax#

yaml
startupProbe:
  httpGet:
    path: /healthz
    port: 8080
  periodSeconds: 5
  failureThreshold: 30
livenessProbe:
  tcpSocket:
    port: 8080
readinessProbe:
  exec:
    command: ["test", "-f", "/tmp/ready"]

A probe uses one of exec, httpGet, tcpSocket or grpc.

Example#

Files stand in for application health: creating ready makes the container ready, and removing healthy makes the liveness probe fail.

sh
(
  set -eu
  ns="ref-probes-$$"
  kubectl create namespace "$ns" >/dev/null
  trap 'kubectl delete namespace "$ns" >/dev/null' EXIT
  kubectl wait -n "$ns" --for=create serviceaccount/default --timeout=60s >/dev/null

  kubectl apply -n "$ns" -f - >/dev/null <<'EOF'
apiVersion: v1
kind: Pod
metadata:
  name: app
spec:
  terminationGracePeriodSeconds: 1
  containers:
    - name: app
      image: busybox:1.37
      command: ["sh", "-c", "touch /tmp/healthy; exec sleep 3600"]
      startupProbe:
        exec:
          command: ["test", "-f", "/tmp/healthy"]
        periodSeconds: 1
        failureThreshold: 30
      livenessProbe:
        exec:
          command: ["test", "-f", "/tmp/healthy"]
        periodSeconds: 1
        failureThreshold: 2
      readinessProbe:
        exec:
          command: ["test", "-f", "/tmp/ready"]
        periodSeconds: 1
EOF
  kubectl wait -n "$ns" --for=jsonpath='{.status.phase}'=Running pod/app --timeout=120s >/dev/null
  kubectl get -n "$ns" pod app -o jsonpath='ready={.status.containerStatuses[0].ready}{"\n"}'

  kubectl exec -n "$ns" app -- touch /tmp/ready
  kubectl wait -n "$ns" --for=condition=Ready pod/app --timeout=60s >/dev/null
  kubectl get -n "$ns" pod app \
    -o jsonpath='ready={.status.containerStatuses[0].ready} restarts={.status.containerStatuses[0].restartCount}{"\n"}'

  kubectl exec -n "$ns" app -- rm /tmp/healthy
  kubectl wait -n "$ns" --for=jsonpath='{.status.containerStatuses[0].restartCount}'=1 pod/app --timeout=60s >/dev/null
  kubectl wait -n "$ns" --for=condition=Ready=false pod/app --timeout=60s >/dev/null
  kubectl get -n "$ns" pod app \
    -o jsonpath='ready={.status.containerStatuses[0].ready} restarts={.status.containerStatuses[0].restartCount}{"\n"}'
)

Expected output#

Text
ready=false
ready=true restarts=0
ready=false restarts=1

After the restart the container has a fresh filesystem, so /tmp/ready is gone.

Common mistakes#

Do not make a liveness probe depend on a database or another service. An outage there would restart healthy containers and can spread the failure. Check only for a state that a restart can fix, and use readiness for temporary inability to serve.

Version applicability#

The example targets Kubernetes 1.37. Built-in grpc probes are stable since 1.27. Probe defaults include periodSeconds: 10, timeoutSeconds: 1 and failureThreshold: 3.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0