Room 41
HORIZONTAL POD AUTOSCALER
The Elastic Engine
- HPA automatically scales pods based on CPU, memory, or custom metrics.
- Generate load to trigger auto-scaling and find the password.
📜 Mission Briefing
- Install metrics-server (required for HPA).
- Deploy
hpa-target.yaml- a deployment with resource requests. - Create an HPA: min 1, max 5 replicas, target 50% CPU.
- Generate load:
kubectl run loader --image=busybox -- sh -c 'while true; do wget -qO- http://hpa-svc; done' - Watch the HPA scale up:
kubectl get hpa --watch. - The HPA has an annotation with the password:
hpascale.
Key Commands
| Command | Purpose |
|---|---|
kubectl autoscale deployment NAME --min=N --max=N --cpu-percent=N |
Create HPA |
kubectl get hpa |
List HPAs |
kubectl describe hpa NAME |
HPA details and events |
kubectl top pods |
Pod resource usage |
How HPA Works
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
annotations:
escape-password: "hpascale"
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
- type: Resource
resource:
name: memory
target:
type: AverageValue
averageValue: 100Mi
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10
periodSeconds: 60
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 15
# HPA requires metrics-server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# For Kind, patch metrics-server to skip TLS verification
kubectl patch deployment metrics-server -n kube-system --type='json' \
-p='[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--kubelet-insecure-tls"}]'
# Create HPA imperatively
kubectl autoscale deployment web-app --min=1 --max=10 --cpu-percent=50
# Watch scaling
kubectl get hpa --watch
kubectl get pods --watch
# HPA algorithm: desiredReplicas = ceil[currentReplicas × (currentMetric / targetMetric)]
Hints
Hint 1
Install metrics-server first, then create the HPA.
Hint 3
The password is hpascale.