0
0
KubernetesConceptBeginner · 3 min read

Liveness Probe in Kubernetes: What It Is and How It Works

A 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.

yaml
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: 10
Output
Kubernetes will start the pod and every 10 seconds after an initial 5-second delay, it will send an HTTP GET request to http://<pod-ip>:8080/health. If the response is not HTTP 200, the container is restarted.
🎯

When 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.

Key Takeaways

Liveness probes help Kubernetes detect and fix stuck containers by restarting them.
They improve app reliability by automatically recovering from failures.
You can configure liveness probes using HTTP, command, or TCP checks.
Set appropriate delays and intervals to avoid false restarts.
Use liveness probes for apps that may hang or freeze without crashing.