Kubernetes Basics - Liveness Probes

Working with liveness probes for container health checks

Liveness Probe Example

Download the YAML configuration file for the liveness probe example

Download pod.yaml

Deploy the Pod

kubectl apply -f pod.yaml

Applies the configuration from the YAML file to create the pod with a liveness probe.

Check Pod Events

kubectl describe pod liveness-example

Shows detailed information about the pod, including events and liveness probe status.

Cleanup

kubectl delete -f pod.yaml --force --grace-period=0

Deletes the pod immediately without waiting for graceful termination.

YAML Configuration File

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-example
spec:
  containers:
  - name: liveness
    image: busybox
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 250m
        memory: 256Mi    
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 15; rm -rf /tmp/healthy; sleep 3600
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5
      failureThreshold: 2

YAML Configuration Explanation:

Pod Structure:

  • apiVersion: v1 → Specifies the Kubernetes API version
  • kind: Pod → Defines this as a Pod resource
  • metadata.name: liveness-example → Names the pod "liveness-example"
  • metadata.labels.test: liveness → Adds a label for identification

Container Configuration:

  • name: liveness → Names the container "liveness"
  • image: busybox → Uses the lightweight BusyBox image
  • resources → Defines CPU and memory requests/limits
  • args → Command arguments that create a file, wait 15 seconds, then delete it

Liveness Probe Configuration:

  • exec.command → Runs the "cat /tmp/healthy" command to check container health
  • initialDelaySeconds: 5 → Waits 5 seconds before starting the first probe
  • periodSeconds: 5 → Runs the probe every 5 seconds
  • failureThreshold: 2 → Container will restart after 2 consecutive failures

How It Works:

The container starts by creating a file at /tmp/healthy. The liveness probe checks for this file every 5 seconds (after an initial 5-second delay). After 15 seconds, the container deletes the file. When the probe fails to find the file twice in a row (failureThreshold: 2), Kubernetes restarts the container. This demonstrates how liveness probes can detect and recover from application failures.

Expected Behavior:

When you run this pod, you'll see it restarting approximately 20-25 seconds after creation. Use "kubectl describe pod liveness-example" to see the restart events and liveness probe failures in the pod's events.