App
Autoscaling
HPA and metrics-server
CPU climbs, the Horizontal Pod Autoscaler does the math, and new Pods appear. A sale doubles traffic on Service web. Before the HPA can act, someone has to know how much CPU each Pod is using - so the lesson starts there: cAdvisor, inside every kubelet, feeding a pipeline that ends in one divide.
- Trigger
- CPU usage per Pod: 600m of 500m requested
- Target
- 60% of requests
- Formula
- desired = ceil(3 × 120% / 60%) = 6
- Source
- metrics.k8s.io · scraped every 15 s
Read the whole lesson as text
1. Where the numbers come from
No component can see Pod CPU directly - it's scattered across /sys/fs/cgroup on every node. cAdvisor, embedded in each kubelet, reads those cgroups, and the kubelet serves the numbers at /metrics/resource. metrics-server, one small cluster add-on, scrapes that endpoint on every node every 15 seconds and republishes the result as an extra API group, metrics.k8s.io, through the API server's aggregation layer.
- metrics-server pulls each node's Pod metrics from its kubelet.
- Node-2 answers the same pull - two web Pods live here.
- The aggregate is served through the API server like any other resource.
2. The math
The HPA controller reads its own object - target 60% CPU - and the fresh metrics, and does arithmetic you could do on paper. Usage 600m against request 500m is 120% utilization. Desired replicas = current × currentUtilization ÷ target, rounded up: 3 × 120 / 60 = 6. One detail does the heavy lifting: utilization is usage divided by REQUESTS. A Pod with no requests has no defined utilization and can't be autoscaled sensibly at all.
- The HPA reads the metrics and its target, and computes six.
3. Act
The HPA's only lever is a number: it patches Deployment web from 3 to 6 replicas. Everything after that is earlier lessons replayed - the Deployment controller scales the ReplicaSet, the ReplicaSet creates three Pods, the scheduler places them two on node-1, one on node-2, the kubelets run them. The HPA doesn't know Pods exist; it knows one integer.
- The HPA writes its one number: six replicas.
- The familiar cascade creates three Pods and the scheduler spreads them.
4. Settle
The three newcomers are Running but count for nothing yet - no metrics for 15-45 seconds, and usage near zero while they boot. When their numbers arrive the average falls: six Pods sharing the sale's load sit at ~58% each. That's below target but inside the tolerance band, 54-66%, and inside the band the HPA deliberately does nothing. Oscillation is worse than imprecision.
- With all six Pods reporting, utilization lands just under target.
5. Scale down, slowly
At 2 a.m. the sale ends; utilization slides to 30%. The math says ceil(6 × 30/60) = 3, but scaling down is deliberately slower: the HPA waits out a stabilization window - 300 s by default - using the highest desired value seen in that window, then removes replicas, newest Pods first. Fast up, slow down: a load spike shouldn't be answered by a later cliff.
- Load fell; the math says three - but the decision waits five minutes.
- The window holds, so three replicas it is - newest Pods retired first.
Summary
Autoscaling is the reconcile pattern aimed at a number nobody stores: per-Pod CPU. A scrape pipeline turns kernel cgroup counters into an API, the HPA divides by requests and rounds up, and its entire output is a single integer on the Deployment. Everything else - Pods, scheduling, endpoints - happens because of machinery this series already covered.
Next lesson: Scale to zero with KEDA