Autoscaling
The HorizontalPodAutoscaler under real load, up and down
The HorizontalPodAutoscaler (HPA) changes a Deployment's replica count to keep a metric near a target. For CPU it reads metrics-server, which collects usage from every kubelet.
The calculation is simple: desired = ceil(current × currentUtilization / target), clamped to min and max, re-evaluated every 15 seconds.
Under load
autoscaling.py runs 12 clients calling a CPU-burning endpoint for three
minutes, then stops:
./scripts/lab.sh autoscaling.py
time replicas desired cpu (% of request) ready pods
0s 3 3 1% 3
25s 3 6 114% 3
30s 3 6 114% 6
40s 6 9 451% 6
45s 6 9 451% 9
55s 9 9 487% 9
...
180s -- load stopped (12264 requests, 0 errors) --
205s 9 9 350% 9
220s 9 9 42% 9
266s 9 7 1% 7
281s 7 3 2% 3- 25 s to the first decision: metrics-server's collection interval plus the HPA's 15 s loop. Then 5 s for new pods to become ready (the app's warm-up). Real apps with slow starts react much more slowly.
- Utilization of 451% is relative to the 100m request. Each pod was capped by its 500m limit (500%), so the pods were saturated and throttled.
- At the maximum of 9, still saturated: the HPA can't help beyond
maxReplicas, and in a real cluster, beyond the nodes' capacity. TheKubeHpaMaxedOutalert exists for this. - Scale-down took about 85 seconds after the load stopped: utilization had to drop in the metrics, then stay low for the 60-second stabilization window. The default window is 5 minutes, which prevents flapping on bursty traffic.
An HPA owns replicas. If your manifests also set replicas: 3 and you
re-apply them during a traffic peak, you scale the Deployment back to 3
until the HPA notices. Leave replicas out of Deployments that have an
HPA: this module's Deployment starts at 1 and the HPA raises it to
minReplicas.
Removing the field has its own trap, hit while writing this lesson.
kubectl apply of the manifest without replicas set the live
Deployment to 1: client-side apply saw the field disappear from the
last-applied configuration and reset it to the default. Two of three pods
terminated at once, and the HPA restored 3 about 15 seconds later
("Current number of replicas below Spec.MinReplicas"). Remove the field
in a quiet moment, or with server-side apply and a field manager that
hands ownership to the HPA.
Beyond CPU
- Memory works the same way, but rarely makes a good scaling signal: most apps don't release memory under lower load.
- Custom and external metrics (requests per second, queue length, Kafka consumer lag) via the Prometheus Adapter or KEDA, which can also scale to zero.
- Vertical autoscaling (VPA) adjusts requests instead of replicas.
- Cluster autoscaling (Cluster Autoscaler, Karpenter) adds nodes when pods are Pending because nothing fits. Pod autoscaling without node autoscaling only works until the nodes are full.
Lab
- Run
autoscaling.py. Then remove the CPU limit, run it again, and compare the utilization numbers. - Lower the target to 30% and run it. How far does it scale?
- Set
stabilizationWindowSeconds: 0for scale-down and send load in bursts of 20 s on, 20 s off. Watch the replica count.