Learn Labs
Resources & Scaling

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.

apiVersion: autoscaling/v2kind: HorizontalPodAutoscalerspec:scaleTargetRef: { kind: Deployment, name: web }minReplicas: 3maxReplicas: 9metrics:- type: Resourceresource:name: cputarget: { type: Utilization, averageUtilization: 60 }% of the REQUEST (100m)behavior:scaleDown:stabilizationWindowSeconds: 60default 300: wait before removing pods

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 sstop266 sload12 clients burning CPUreplicas369 (max), CPU ~460% of request73seconds 0 … 300 (load stopped at 180 s)
  • 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. The KubeHpaMaxedOut alert 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.

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

  1. Run autoscaling.py. Then remove the CPU limit, run it again, and compare the utilization numbers.
  2. Lower the target to 30% and run it. How far does it scale?
  3. Set stabilizationWindowSeconds: 0 for scale-down and send load in bursts of 20 s on, 20 s off. Watch the replica count.

On this page