BlogKubernetes
Kubernetes autoscaling with the Horizontal Pod Autoscaler
Scale on CPU, memory, or custom Prometheus metrics, and tune stabilization windows so the HorizontalPodAutoscaler won't flap under spiky, bursty load.
The Horizontal Pod Autoscaler adds and removes replicas to match load. CPU-target autoscaling is one manifest; the parts people get wrong are needing metrics-server, scaling on the right signal, and stopping the replica count from flapping under spiky traffic.
The basic CPU autoscaler
hpa.yaml
apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata: { name: api }spec:scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: api }minReplicas: 3maxReplicas: 20metrics:- type: Resourceresource:name: cputarget: { type: Utilization, averageUtilization: 70 }
kubectl get hpa api -wNAME TARGETS REPLICASapi 42%/70% 3api 88%/70% 6 (scaled up under load)CPU is a proxy, not the goal
For a queue worker, scale on queue depth; for an API, on requests-per-second or latency. HPA v2 supports custom and external metrics — use the signal your users actually feel.
Stop the flapping
hpa.yaml
behavior:scaleDown:stabilizationWindowSeconds: 300 # wait 5m before scaling inpolicies: [{ type: Percent, value: 50, periodSeconds: 60 }]