https://drive.google.com/file/d/1ovRvqFddKp74eItuM_PUL61xBU4vIzsV/view?usp=sharing
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.htmlis missing or unreadable.
| 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:
t=10s: First probe- If fails โ
t=30s: Second probe- If fails โ
t=50s: Third probe- If fails โ container restarted at
t=50s
๐ก Probe Types:
exec: Run command (your example)httpGet: HTTP request (e.g.,GET /health)tcpSocket: Open TCP connection (for non-HTTP apps)
# Apply Pod
kubectl apply -f pod-with-health-check.yml
# Verify it's running
kubectl get pods nnwebserver
๐ก Delete the critical file that the probe checks:
# Remove index.html (breaks the app)
kubectl exec nnwebserver -- rm /var/www/html/index.html
# 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?
- Probe failed 3 times (
failureThreshold: 3)- Kubernetes killed and restarted the container
- New container recreates
/var/www/html/index.html(assuming image does this on startup)