Cấu hình Liveness, Readiness và Startup Probes
🎯 Mục tiêu: Triển khai app có đủ 3 loại probe; quan sát Kubernetes tự restart container khi liveness fail và giữ traffic khi readiness fail.
🧰 Công cụ / nền tảng: kubectl, kind/minikube, VS Code.
📦 Chuẩn bị: Cluster đang chạy; namespace probes-lab.
▶️ Các bước:
# 1. Tạo namespace
kubectl create namespace probes-lab
# 2. Deploy app với đủ 3 probe
cat > probes-demo.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: probes-demo
namespace: probes-lab
spec:
replicas: 2
selector:
matchLabels:
app: probes-demo
template:
metadata:
labels:
app: probes-demo
spec:
containers:
- name: web
image: nginx:1.27-alpine
ports:
- containerPort: 80
# Startup Probe: chờ tối đa 30s (6 * 5s) cho container khởi động
startupProbe:
httpGet:
path: /
port: 80
failureThreshold: 6
periodSeconds: 5
# Readiness Probe: kiểm tra mỗi 5s, fail 2 lần thì remove khỏi endpoints
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 3
periodSeconds: 5
failureThreshold: 2
successThreshold: 1
# Liveness Probe: kiểm tra mỗi 10s, fail 3 lần thì restart
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 10
periodSeconds: 10
failureThreshold: 3
timeoutSeconds: 2
resources:
requests:
cpu: "50m"
memory: "32Mi"
limits:
cpu: "200m"
memory: "64Mi"
EOF
kubectl apply -f probes-demo.yaml
kubectl rollout status deployment/probes-demo -n probes-lab
# 3. Quan sát probe status
kubectl get pods -n probes-lab -o wide
kubectl describe pod -n probes-lab -l app=probes-demo | grep -A 10 "Liveness\|Readiness\|Startup"
# 4. Tái hiện liveness failure: exec vào pod xóa file index
POD=$(kubectl get pod -n probes-lab -l app=probes-demo -o jsonpath='{.items[0].metadata.name}')
kubectl exec -n probes-lab $POD -- rm /usr/share/nginx/html/index.html
# Quan sát restart
kubectl get pod -n probes-lab -w
# 5. Xem restart count
kubectl get pod -n probes-lab $POD -o jsonpath='{.status.containerStatuses[0].restartCount}'
# 6. Kiểm tra readiness: block traffic khi pod không ready
kubectl get endpoints -n probes-lab
✅ Kết quả mong đợi: Sau khi xóa index.html, Pod RESTARTS tăng lên (liveness kill → restart); trong lúc readiness fail, kubectl get endpoints bỏ Pod đó ra khỏi danh sách (0 IP cho service). Sau restart nginx phục hồi.
🧹 Cleanup: kubectl delete namespace probes-lab.