The Horizontal Pod Autoscaler
How the HPA turns a metric into a replica count: the ratio formula, the 10% tolerance, scale-up limits, the five-minute scale-down window, and behavior.
The Horizontal Pod Autoscaler
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The HPA is a controller in kube-controller-manager. Every 15 seconds it reads metrics, computes desired = ceil(current × currentValue / target), and writes the result to the target's scale subresource.
- CPU and memory utilization are percentages of the Pod's requests, so an HPA on a workload without requests can't work. Resource metrics come from the metrics.k8s.io API, usually served by metrics-server.
- A ratio within 10% of 1.0 is ignored, so small wobbles don't cause scaling. The tolerance can be set per HPA and per direction in behavior, which is GA in 1.37.
- Scaling down waits for a stabilization window, five minutes by default: the HPA uses the highest recommendation it made in that window, so a brief dip doesn't remove Pods.
- Without a behavior section, scale-up is capped at max(2 × current, 4) per sync. With one, policies such as 4 Pods or 100% per 15 seconds set the rate in each direction.
- Custom, object and external metrics let the HPA follow request rate or queue depth instead of CPU, through adapters that serve custom.metrics.k8s.io or external.metrics.k8s.io.
Sizing for the peak wastes the trough
Traffic to most services follows a curve: busy afternoons, quiet nights, a spike when a newsletter goes out. A fixed replica count has to cover the peak, so for most of the day you pay for Pods that sit idle. Set it lower and the spike overwhelms you. Someone could watch a dashboard and run kubectl scale, but people sleep.
The Horizontal Pod Autoscaler (HPA) does that job: it watches a metric and adjusts a workload’s replica count to keep the metric near a target. It is horizontal because it changes how many Pods run, not how big each one is. The next lesson covers that other direction.
The pieces
An HPA is an API object, and the logic lives in a controller inside kube-controller-manager. Every 15 seconds (--horizontal-pod-autoscaler-sync-period) the controller, for each HPA:
- reads the target’s scale subresource to get the current replica count and Pod selector;
- fetches metrics for those Pods;
- computes a desired replica count;
- writes it back to the scale subresource.
The Deployment controller then does the actual work of adding or removing Pods. Anything with a scale subresource, including StatefulSets and many custom resources, can be autoscaled this way.
For CPU and memory, metrics come from the resource metrics API, metrics.k8s.io, which the API server proxies to an add-on, usually metrics-server, which in turn collects from each kubelet. Without it, the HPA can’t see anything. On a lab API server with no metrics add-on, the HPA said exactly that:
Conditions:
AbleToScale True SucceededGetScale the HPA controller was able to get the target's current scale
ScalingActive False FailedGetResourceMetric the HPA was unable to compute the replica count: failed to get cpu utilization:
unable to get metrics for resource cpu: unable to fetch metrics from resource metrics API:
the server could not find the requested resource (get pods.metrics.k8s.io)
The formula
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
averageUtilization: 60 means 60% of the Pods’ CPU requests, averaged across Pods. No requests, no percentage, so set them, as covered in requests, limits and QoS classes. The controller then computes:
desiredReplicas = ceil( currentReplicas × currentMetricValue / targetValue )
To watch it work, I served a stand-in metrics API that spread a fixed 600 millicores of CPU across however many web Pods were running (each requests 200m), registered it with the lab API server, and let the real HPA controller act:
web Deployment/web cpu: 150%/60% 2 10 2 15s
web Deployment/web cpu: 75%/60% 2 10 4 31s
web Deployment/web cpu: 60%/60% 2 10 5 46s
Two Pods at 300m each is 150% of request. 150/60 = 2.5, so desired = ceil(2 × 2.5) = 5. It went to 4 first. With no behavior set, the controller caps each step at max(2 × current, 4), and from 2 that’s 4. At 4 Pods, 150m each is 75%, ceil(4 × 1.25) = 5, and at 5 Pods the load sits on target.
Tolerance: don’t chase noise
Raising the load to 650m put five Pods at 65%. The ratio 65/60 is 1.08, and nothing happened: the HPA ignores ratios within 10% of 1.0 (--horizontal-pod-autoscaler-tolerance). That dead band stops replicas flickering on ordinary noise. Kubernetes 1.37 made a per-HPA tolerance GA, set separately for each direction in behavior.
Scale down slowly
Then the load dropped to 300m, 30% on five Pods, so the formula wanted 3. At 17:57:08 the load dropped. The HPA scaled to 3 at 18:01:57. That pause is the downscale stabilization window, five minutes by default. When scaling down, the HPA uses the highest recommendation it made during the window. Load that dips for two minutes and comes back never removes a Pod, and you aren’t paying start-up costs twice.
Scale-up has no window by default. Reacting late to more load is worse than reacting late to less.
Shaping it with behavior
behavior replaces the built-in limits with explicit policies:
behavior:
scaleUp:
tolerance: 0.05
scaleDown:
stabilizationWindowSeconds: 600
policies:
- type: Percent
value: 20
periodSeconds: 60
Sending that to the API server with --dry-run=server shows the defaults it fills in for the direction you only partly set: scale-up gets stabilizationWindowSeconds: 0 and two policies, 4 Pods or 100% per 15 seconds, with selectPolicy: Max, so whichever allows more wins. The tolerance comes back as the quantity "50m". Scale-down here removes at most 20% of Pods a minute after a ten-minute window.
Beyond CPU
CPU is a poor proxy for many services. An I/O-bound API can be saturated at 20% CPU. autoscaling/v2 accepts four metric types:
- Resource: CPU or memory of the target’s Pods.
- Pods: a per-Pod custom metric such as requests per second, averaged.
- Object: one value from another object, such as an Ingress’s request rate.
- External: something outside the cluster, such as a cloud queue’s depth.
The last three come from custom.metrics.k8s.io and external.metrics.k8s.io, served by an adapter such as prometheus-adapter. Several metrics can be listed, and the HPA picks the largest desired count. minReplicas: 0 is accepted only with an Object or External metric, since a workload at zero Pods has no Pod metrics. The lab API server rejected it for this CPU-only HPA. For event sources, KEDA wraps all of this up.
About the video
The embed is a clip of Cloud With VarJosh’s “Day 20: MASTER Kubernetes Autoscaling | HPA & VPA Explained with DEMO”, part of a 2025 CKA course, from “What is HPA?” (11:07) to the end of the HPA demo (29:18), about 18 minutes. Going by its chapters and description, it explains what the HPA does, how it uses metrics-server and requests, and demos scaling a Deployment under load. I judged it from chapters and description only, since transcripts couldn’t be fetched. It predates the per-HPA tolerance GA. English was judged from its “English (US) original” audio track. networknutsdotnet’s video in deeper spends 12 minutes on behavior policies and stabilization.
What was run: every HPA output above came from the real HPA controller in a local Kubernetes v1.37.3 control plane whose nodes run no containers. Pods were marked Running by hand. CPU figures came from a small stand-in for metrics-server, registered through the aggregation layer, that divided a fixed total among running Pods. The controller’s arithmetic, limits and timing were its own. The 2×/4 cap and the defaults were confirmed in pkg/controller/podautoscaler and pkg/apis/autoscaling/v2/defaults.go. Metric types and adapters are from the HPA docs, checked 2026-11-28.
Where this fits
More Pods is one answer. Bigger Pods is the other: vertical scaling and in-place resize.
Check yourself
Three Pods each request 200m CPU and use 300m. The target is 60% utilization. How many replicas does the HPA want?
Utilization is 300/200 = 150%. The ratio to target is 150/60 = 2.5, so desired = ceil(3 × 2.5) = 8, subject to maxReplicas and the scale-up rate limits.
Load drops sharply at 12:00. Why are the extra Pods still there at 12:03?
Scale-down uses a stabilization window, five minutes by default. The HPA takes the highest recommendation of the last five minutes, which still includes the busy period, so it won't scale down until about 12:05.
Your HPA shows cpu: <unknown>/60% and ScalingActive False. What are the two most common causes?
No resource metrics API is installed (metrics-server is missing or unhealthy, so get pods.metrics.k8s.io fails), or the target Pods have no CPU requests, so a utilization percentage can't be computed.
Why is the HPA's target a Deployment's scale subresource rather than its Pods?
The HPA only changes replicas. The Deployment controller then creates or removes Pods, and anything with a scale subresource (StatefulSets, ReplicaSets, custom resources) can be autoscaled the same way.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.