0
0
KubernetesHow-ToBeginner · 3 min read

How to Configure Liveness Probe in Kubernetes for Container Health

In Kubernetes, configure a livenessProbe in your pod's container spec to let Kubernetes check if the container is alive. You specify the probe type (HTTP, TCP, or command), the check frequency, and failure thresholds. Kubernetes restarts the container if the probe fails.
📐

Syntax

The livenessProbe is defined inside the container spec in a pod YAML file. It has three main types:

  • httpGet: Kubernetes sends an HTTP GET request to check container health.
  • tcpSocket: Kubernetes tries to open a TCP connection on a port.
  • exec: Kubernetes runs a command inside the container; success means healthy.

Common fields include initialDelaySeconds (wait before first check), periodSeconds (check interval), timeoutSeconds (how long to wait for response), and failureThreshold (how many failures before restart).

yaml
livenessProbe:
  httpGet:
    path: /healthz
    port: 8080
  initialDelaySeconds: 10
  periodSeconds: 5
  timeoutSeconds: 2
  failureThreshold: 3
💻

Example

This example shows a pod with a container that uses an HTTP GET liveness probe. Kubernetes will check the /healthz endpoint on port 8080 every 5 seconds, starting 10 seconds after the container starts. If the probe fails 3 times in a row, Kubernetes restarts the container.

yaml
apiVersion: v1
kind: Pod
metadata:
  name: liveness-example
spec:
  containers:
  - name: myapp
    image: k8s.gcr.io/liveness
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
      initialDelaySeconds: 10
      periodSeconds: 5
      timeoutSeconds: 2
      failureThreshold: 3
Output
Pod 'liveness-example' created. Kubernetes will restart the container if /healthz endpoint fails 3 times after initial delay.
⚠️

Common Pitfalls

Common mistakes when configuring liveness probes include:

  • Setting initialDelaySeconds too low, causing Kubernetes to kill containers before they are ready.
  • Using a probe that always fails or is misconfigured, causing constant restarts.
  • Not matching the probe path or port to the actual container service.
  • Setting timeoutSeconds too low, causing false failures.

Always test your probe manually before applying.

yaml
Wrong example:
livenessProbe:
  httpGet:
    path: /wrongpath
    port: 8080
  initialDelaySeconds: 0
  periodSeconds: 5
  failureThreshold: 1

Right example:
livenessProbe:
  httpGet:
    path: /healthz
    port: 8080
  initialDelaySeconds: 10
  periodSeconds: 5
  failureThreshold: 3
📊

Quick Reference

FieldDescription
httpGetHTTP GET request to check container health
tcpSocketTCP connection check on specified port
execRun command inside container to check health
initialDelaySecondsSeconds to wait before first probe
periodSecondsSeconds between probes
timeoutSecondsSeconds to wait for probe response
failureThresholdNumber of failures before restart

Key Takeaways

Configure livenessProbe in the container spec to let Kubernetes detect unhealthy containers.
Choose the right probe type: httpGet, tcpSocket, or exec based on your app.
Set initialDelaySeconds to avoid premature restarts during startup.
Test probe endpoints or commands manually before applying.
Use failureThreshold and timeoutSeconds to balance sensitivity and stability.