← Về danh sách bài học
Bài 15/25
📈 Bài 15: HPA - Horizontal Pod Autoscaler
🎯 Sau bài học này, bạn sẽ:
- Hiểu HPA hoạt động như thế nào
- Tạo HPA theo CPU/Memory
- Cấu hình scaling behavior
1. HPA Là Gì?
Horizontal Pod Autoscaler tự động tăng/giảm số pods dựa trên metrics (CPU, memory, custom).
⚠️ Yêu cầu: Cần cài Metrics Server!
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
2. Tạo HPA Command Line
# Tạo HPA cho deployment
kubectl autoscale deployment nginx-deployment \
--min=2 --max=10 --cpu-percent=50
# Xem HPA
kubectl get hpa
kubectl describe hpa nginx-deployment
3. HPA YAML
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 70
💡 Deployment phải có resources.requests:
resources:
requests:
cpu: "100m"
memory: "128Mi"
4. Scaling Behavior
behavior:
scaleDown:
stabilizationWindowSeconds: 300 # Đợi 5 phút trước khi scale down
policies:
- type: Percent
value: 50
periodSeconds: 60
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 15
- type: Pods
value: 4
periodSeconds: 15
selectPolicy: Max
📝 Tóm Tắt
- HPA: Tự động scale pods
- Scale dựa trên CPU, Memory, Custom metrics
- Cần Metrics Server
- minReplicas, maxReplicas