Skip to content

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

  1. Install metrics-server (required for HPA).
  2. Deploy hpa-target.yaml - a deployment with resource requests.
  3. Create an HPA: min 1, max 5 replicas, target 50% CPU.
  4. Generate load: kubectl run loader --image=busybox -- sh -c 'while true; do wget -qO- http://hpa-svc; done'
  5. Watch the HPA scale up: kubectl get hpa --watch.
  6. 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 2

kubectl get hpa web-hpa -o jsonpath='{.metadata.annotations.escape-password}'

Hint 3

The password is hpascale.


🔓 Unlock Room 42

openssl enc -aes-256-cbc -d -a -pbkdf2 \
        -in ../room_42/README -out ../room_42/README.txt -pass pass:PASSWORD
cat ../room_42/README.txt