0
0
Kubernetesdevops~5 mins

Liveness probe concept in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, applications inside containers can stop working but still appear running. A liveness probe helps Kubernetes check if an app is alive and restart it if it is stuck or crashed.
When your app might freeze or get stuck and needs a restart to recover.
When you want Kubernetes to automatically fix apps that stop responding.
When running long-lived services that must stay healthy without manual checks.
When you want to avoid manual intervention for crashed or deadlocked containers.
When you want to improve app reliability by automatic self-healing.
Config File - pod.yaml
pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: liveness-demo
spec:
  containers:
  - name: my-app
    image: nginx:1.23
    livenessProbe:
      httpGet:
        path: /healthz
        port: 80
      initialDelaySeconds: 5
      periodSeconds: 10
      timeoutSeconds: 1
      failureThreshold: 3

This file creates a pod named liveness-demo running an Nginx container.

The livenessProbe section tells Kubernetes to check the /healthz URL on port 80 every 10 seconds, starting 5 seconds after the container starts.

If the check fails, Kubernetes restarts the container automatically.

Commands
This command creates the pod with the liveness probe defined. Kubernetes will start the container and begin health checks.
Terminal
kubectl apply -f pod.yaml
Expected OutputExpected
pod/liveness-demo created
This command shows the status of all pods, including our new pod, to verify it is running.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE liveness-demo 1/1 Running 0 10s
This command shows detailed information about the pod, including events related to the liveness probe and any restarts.
Terminal
kubectl describe pod liveness-demo
Expected OutputExpected
Name: liveness-demo Namespace: default Containers: my-app: Container ID: docker://abcdef123456 Image: nginx:1.23 State: Running Ready: True Restart Count: 0 Liveness: http-get http://:80/healthz delay=5s timeout=1s period=10s #success=1 #failure=3 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 1m default-scheduler Successfully assigned default/liveness-demo to node-1
Key Concept

If you remember nothing else from this pattern, remember: a liveness probe lets Kubernetes detect and fix stuck or crashed containers automatically by restarting them.

Common Mistakes
Not setting initialDelaySeconds causing the probe to run before the app is ready
The probe fails immediately and Kubernetes restarts the container repeatedly, causing a crash loop.
Set initialDelaySeconds to a value that gives your app time to start before health checks begin.
Using a wrong path or port in the httpGet probe
The probe always fails because it cannot reach the correct endpoint, causing unnecessary restarts.
Verify the path and port match your app's health endpoint exactly.
Confusing livenessProbe with readinessProbe
Liveness restarts the container; readiness controls traffic routing. Using the wrong probe can cause unwanted restarts or traffic issues.
Use livenessProbe to check if the app is alive and restart if needed; use readinessProbe to control if the pod receives traffic.
Summary
Define a liveness probe in your pod spec to let Kubernetes check if your app is alive.
Use kubectl apply to create the pod and kubectl get pods to verify it is running.
Use kubectl describe pod to see detailed probe status and container restarts.