https://drive.google.com/file/d/1ovRvqFddKp74eItuM_PUL61xBU4vIzsV/view?usp=sharing

๐Ÿ” YAML Breakdown

apiVersion: v1
kind: Pod
metadata:
  name: nnwebserver
spec:
  containers:
    - name: nnwebserver
      image: lovelearnlinux/webserver:v1
      livenessProbe:
        exec:
          command: ["cat", "/var/www/html/index.html"]  # โ† Check if file exists
        initialDelaySeconds: 10   # โ† Wait 10s before first check
        timeoutSeconds: 3         # โ† Fail if check takes >3s
        periodSeconds: 20         # โ† Repeat every 20s
        failureThreshold: 3       # โ† Fail 3 times โ†’ restart container
      resources:
        requests:
          cpu: "500m"
          memory: "128Mi"
        limits:
          cpu: "1000m"
          memory: "256Mi"
      ports:
        - containerPort: 80

๐Ÿ”‘ Key Insight:

This probe restarts the container if /var/www/html/index.html is missing or unreadable.


๐Ÿ“Œ How Liveness Probes Work

Parameter Purpose Your Value
exec.command Run command inside container cat /var/www/html/index.html
initialDelaySeconds Wait before first probe 10s (lets app start)
periodSeconds How often to probe 20s
failureThreshold Consecutive failures before restart 3
timeoutSeconds Max time for probe to complete 3s

๐ŸŽฏ Failure Timeline:

๐Ÿ’ก Probe Types:


๐Ÿงช k3s Lab: Break the App & Watch Auto-Restart

๐Ÿ”ง Step 1: Deploy the Pod

# Apply Pod
kubectl apply -f pod-with-health-check.yml

# Verify it's running
kubectl get pods nnwebserver

๐Ÿ”ง Step 2: Simulate App Failure

๐Ÿ’ก Delete the critical file that the probe checks:

# Remove index.html (breaks the app)
kubectl exec nnwebserver -- rm /var/www/html/index.html

๐Ÿ”ง Step 3: Watch Kubernetes Heal Itself

# Watch Pod restarts
kubectl get pods nnwebserver -w

# โœ… Expected:
# NAME          READY   STATUS    RESTARTS   AGE
# nnwebserver   1/1     Running   0          10s
# nnwebserver   1/1     Running   1          45s  โ† Restarted!

# Check logs to confirm
kubectl describe pod nnwebserver | grep -A 5 "Liveness"
# Events:
#   Warning  Unhealthy  50s  kubelet  Liveness probe failed: cat: /var/www/html/index.html: No such file or directory
#   Normal   Killing    50s  kubelet  Container nnwebserver failed liveness probe, will be restarted

๐Ÿ” Why did it restart?

๐Ÿ”ง Step 4: Clean Up