KubernetesKubernetes
Horizontal Pod Autoscaler: scale on CPU, memory and custom metrics
The HorizontalPodAutoscaler changes the replica count of a workload to match observed metrics. Utilization is measured against requests, and scale-down has a default stabilization window of 300 seconds.

How the controller decides
The HorizontalPodAutoscaler (HPA) is a control loop in the Kubernetes control plane. Its interval is a kube-controller-manager setting with a default of 15 seconds. The controller works on one ratio:
desiredReplicas = ceil( currentReplicas * currentMetricValue / desiredMetricValue )The documentation's example: with a current value of 200m and a desired value of 100m, the number of replicas doubles. With a current value of 50m, it halves. The controller skips any change when the ratio is within a tolerance of 1.0, which is 0.1 by default. With several metrics, it computes a replica count for each and takes the largest.
CPU and memory figures come from the metrics.k8s.io API. Metrics Server is an add-on that provides this API, and it needs to be launched separately. Test it with kubectl top pod: that command needs the Metrics Server too.
Utilization is relative to requests
Utilization is the ratio between current usage and the requested resources of the pod. A target of averageUtilization: 60 keeps the average across the pods at 60% of the request. Two things follow.
- Every container needs a request for the scaled resource. If some containers have none, CPU utilization for the pod is not defined and the autoscaler takes no action for that metric.
- The usage of all containers in a pod is summed. One container can run with high usage while the pod total stays within limits, and then the HPA does not scale out. The
ContainerResourcemetric type tracks one named container in place of the whole pod.
To scale on a raw amount, use a target of type AverageValue, such as averageValue: 500Mi for memory.
A manifest for autoscaling/v2
autoscaling/v2 is the current stable API version. The replica counts, the target and the scale-down policy below are values for you to choose:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 3
maxReplicas: 12
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10
periodSeconds: 60The documentation recommends removing spec.replicas from the Deployment manifest when an HPA is enabled. If the field stays, every kubectl apply of the manifest scales the pods back to that value. Removing it can cause a one-time drop to one replica, and the HPA page describes how to avoid that.
The behavior field
behavior sets separate rules under scaleUp and scaleDown. The values you specify are merged with the defaults:
- Scale-down: a stabilization window of 300 seconds. The controller considers every desired state computed in the window and uses the highest. One default policy allows 100% of the running replicas to be removed every 15 seconds, down to the minimum.
- Scale-up: no stabilization window. Two default policies allow 4 pods or 100% of the running replicas to be added every 15 seconds, and the policy that allows the larger change is selected.
The manifest above keeps the 300-second window and limits removal to 10% of the replicas per minute. selectPolicy: Disabled turns off scaling in one direction. periodSeconds can be 1800 at most.
Custom and external metrics
autoscaling/v2 has three more metric types for metrics that are not built into Kubernetes:
Pods: a metric that describes the pods and is averaged across them, such as packets per second.Object: a metric that describes another object in the same namespace, such as the requests per second of an Ingress.External: a metric with no obvious relationship to any object in the cluster, such as the outstanding tasks of a hosted queue service.
The HPA reads these from the custom.metrics.k8s.io and external.metrics.k8s.io APIs. Adapter API servers from metrics vendors provide the first, and the same adapters may provide the second.
KEDA, a CNCF-graduated project, is another route. You create a ScaledObject that links a workload to an event source. KEDA creates and manages the HPA, serves the external metrics to it, and scales between zero and one replica itself.
Where both would work, the Kubernetes documentation prefers custom metrics, because the external metrics API potentially gives access to any metric.
Conflict with the Vertical Pod Autoscaler
The Vertical Pod Autoscaler's documentation says it should not be used with the HPA on the same resource metric, CPU or memory. It names the combinations that work: the two on separate resource metrics, such as vertical scaling on memory and horizontal scaling on CPU, or the HPA on custom and external metrics.
First checks when scaling stalls
- Run
kubectl describe hpa my-appand read the conditions.ScalingActiveshows whether the HPA is enabled and able to calculate desired scales. - Confirm that every container in the target pods has a request for the scaled resource.
- Look at
ScalingLimited. It indicates that the desired scale was capped by the minimum or the maximum of the HPA. - Check the target's replica count. If it was set to 0 while the HPA's minimum is above 0, the HPA stops adjusting the target until you change one of the two.
- Check startup behavior. For pods with high CPU usage while they start, the documentation's good practice is a
startupProbethat does not pass until the high CPU usage has passed.
Sources
Want a second pair of eyes on yours? We do this work for a living.
More guides

Kubernetes requests and limits: set them from what you measure
Kubernetes · 4 min read
What requests and limits do, how QoS classes follow from them, and how to size both from real usage.

Readiness, liveness and startup probes in Kubernetes
Kubernetes · 2 min read
What each probe is for, and what the Kubernetes documentation warns about.

PodDisruptionBudgets: keep pods running through node drains
Kubernetes · 4 min read
What a disruption budget covers, how to write one, and the settings that leave a node drain stuck.