CKAD — Probes & Health Checks Lab

3 minute read

Lab 1: Liveness Probe in Action

Create a Pod that simulates a health failure — watch Kubernetes automatically restart it.

# liveness-demo.yaml
apiVersion: v1
kind: Pod
metadata:
  name: liveness-demo
spec:
  containers:
  - name: app
    image: busybox:1.36
    command: ["/bin/sh", "-c"]
    args:
    - |
      echo "App starting..."
      touch /tmp/healthy
      echo "App healthy"
      sleep 20
      echo "Simulating failure..."
      rm /tmp/healthy
      sleep 600
    livenessProbe:
      exec:
        command: ["test", "-f", "/tmp/healthy"]
      initialDelaySeconds: 3
      periodSeconds: 5
      failureThreshold: 2
kubectl apply -f liveness-demo.yaml

# Watch — after ~25 seconds the container will restart
kubectl get pod liveness-demo -w

# Check restart count
kubectl describe pod liveness-demo | grep "Restart Count"
# Expected: Restart Count: 1 (and increasing)

Lab 2: Readiness Probe Controlling Traffic

See how a readiness probe removes a Pod from Service endpoints when the health check fails.

# Create a Deployment with a readiness probe
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx:1.27
        ports:
        - containerPort: 80
        readinessProbe:
          httpGet:
            path: /
            port: 80
          periodSeconds: 5
EOF

# Create a Service
kubectl expose deployment web --port=80

# Check endpoints — all 3 Pods should be listed
kubectl get endpoints web

# Now, exec into one Pod and break the readiness check
POD=$(kubectl get pods -l app=web -o name | head -1)
kubectl exec $POD -- rm /usr/share/nginx/html/index.html

# Check endpoints again — one Pod should be removed
kubectl get endpoints web
# Only 2 IPs now

Practice Questions

Q1: A Pod’s liveness probe is failing. What does Kubernetes do?

Show Answer Kubernetes **restarts the container**. After `failureThreshold` consecutive failures, the kubelet kills the container and creates a new one in the same Pod. The restart count increments each time.

Q2: Create a Pod named health-check with image nginx:1.27 that has a liveness probe on path / port 80 with an initial delay of 10 seconds and a period of 15 seconds.

Show Answer ```yaml apiVersion: v1 kind: Pod metadata: name: health-check spec: containers: - name: nginx image: nginx:1.27 ports: - containerPort: 80 livenessProbe: httpGet: path: / port: 80 initialDelaySeconds: 10 periodSeconds: 15 ```

Q3: What is the difference between a liveness probe and a readiness probe?

Show Answer - **Liveness probe** — Checks if the container is still running properly. If it fails, Kubernetes **restarts** the container. Use for deadlock detection. - **Readiness probe** — Checks if the container is ready to accept traffic. If it fails, Kubernetes **removes the Pod from Service endpoints** (stops sending traffic to it). The container is NOT restarted. Use for warm-up time or temporary dependency issues.

Q4: An application takes 2 minutes to load data on startup. The liveness probe times out and keeps restarting the container before the app is ready. How do you fix this?

Show Answer Add a **startup probe** that gives the app enough time to start: ```yaml startupProbe: httpGet: path: /healthz port: 8080 failureThreshold: 24 periodSeconds: 5 # 24 × 5 = 120 seconds (2 minutes to start) livenessProbe: httpGet: path: /healthz port: 8080 periodSeconds: 10 failureThreshold: 3 ``` The startup probe runs first and allows up to 120 seconds. Once it succeeds, the liveness probe takes over with a shorter timeout.

Q5: Write a TCP socket liveness probe for a database container on port 5432 that checks every 20 seconds after an initial 30 second delay.

Show Answer ```yaml livenessProbe: tcpSocket: port: 5432 initialDelaySeconds: 30 periodSeconds: 20 failureThreshold: 3 timeoutSeconds: 5 ``` TCP probes are ideal for databases and other services that do not have HTTP health endpoints.

← Previous: Helm & Kustomize Next: Logging & Debugging →

Updated: