Liveness Probe in Kubernetes: What It Is and How It Works
liveness probe in Kubernetes is a health check that tells the system if a container is alive or stuck. If the probe fails, Kubernetes restarts the container to fix the problem automatically.How It Works
Imagine you have a friend who sometimes freezes while playing a video game. You want to check if they are still playing or stuck. A liveness probe is like a gentle tap on your friend’s shoulder to see if they respond. If they don’t, you help them restart the game.
In Kubernetes, the liveness probe regularly checks a container’s health by running a command, sending an HTTP request, or opening a TCP connection. If the container does not respond correctly, Kubernetes assumes it is stuck or broken and restarts it to keep your app running smoothly.
Example
This example shows a liveness probe that checks if a web server inside a container responds on port 8080 with a HTTP GET request to the path /health. If the server does not respond with status 200, Kubernetes restarts the container.
apiVersion: v1
kind: Pod
metadata:
name: liveness-example
spec:
containers:
- name: web-server
image: nginx:1.23
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 5
periodSeconds: 10When to Use
Use a liveness probe when you want Kubernetes to automatically fix containers that are stuck or not working properly. This is helpful for apps that might freeze, deadlock, or get into a bad state without crashing.
For example, if your web server sometimes hangs and stops responding, a liveness probe can detect this and restart it without manual intervention. This keeps your service reliable and reduces downtime.
Key Points
- A liveness probe checks if a container is alive and working.
- If the probe fails, Kubernetes restarts the container automatically.
- It can use HTTP requests, commands, or TCP checks.
- Helps keep applications running smoothly by fixing stuck containers.