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: 3Example
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: 3Output
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
initialDelaySecondstoo 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
timeoutSecondstoo 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: 3Quick Reference
| Field | Description |
|---|---|
| httpGet | HTTP GET request to check container health |
| tcpSocket | TCP connection check on specified port |
| exec | Run command inside container to check health |
| initialDelaySeconds | Seconds to wait before first probe |
| periodSeconds | Seconds between probes |
| timeoutSeconds | Seconds to wait for probe response |
| failureThreshold | Number 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.