Horizontal Pod Autoscaler
The HPA is a controller that periodically (every 15s) reads metrics and adjusts a Deployment’s replicas.
apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata: { name: demo, namespace: demo }spec: scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: demo } minReplicas: 2 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: { type: Utilization, averageUtilization: 50 } behavior: scaleDown: { stabilizationWindowSeconds: 30 }The formula
Section titled “The formula”desiredReplicas = ceil( currentReplicas × currentMetric / targetMetric )Example: 3 pods averaging 150% of their CPU request with a target of 50% → ceil(3 × 150/50) = 9 pods.
“Utilization” is relative to requests: our pods request 100m, so 50% = 50 millicores average.
What it needs
Section titled “What it needs”- metrics-server installed (it collects CPU/memory from each kubelet). The lab installs it with
make metrics. - CPU requests on the containers. Without them you get
<unknown>targets.
Behaviour and tuning
Section titled “Behaviour and tuning”- Scale up is fast, scale down is slow (5-minute stabilization window by default) to avoid flapping. We shortened it to 30s so you can watch it in the lab.
- The HPA owns
replicas. If youkubectl scalewhile an HPA is active, it will put the number back. Don’t also hard-codereplicasthat fights it in GitOps. minReplicas ≥ 2keeps you available while one pod restarts.
Beyond CPU
Section titled “Beyond CPU”- Memory: works the same way, but many runtimes never give memory back, so it’s often a poor signal.
- Custom metrics (requests/sec, latency) via Prometheus Adapter.
- KEDA: event-driven autoscaling on queue length (Kafka, RabbitMQ, SQS), cron schedules, and more, including scale to zero.
- VPA (Vertical Pod Autoscaler): recommends or sets better requests/limits. Don’t let it and the HPA both act on CPU for the same Deployment.
Watching it
Section titled “Watching it”kubectl get hpa -n demo --watchkubectl describe hpa demo -n demo # events show each scaling decision and whykubectl top pods -n demo