What Is the Kubernetes HPA? Automatically Adjust Pod Replicas from Metrics
The Kubernetes HorizontalPodAutoscaler (HPA) watches CPU, memory, or application metrics and automatically changes the replica count of a workload such as a Deployment. It can add replicas as load rises and remove them as load falls, but it does not add node capacity or fix an application bottleneck. Reliable scaling depends on a working metrics API, realistic resource requests, and suitable replica bounds.
How does HPA calculate replicas?
The basic calculation applies the ratio of the observed metric to its target to the current replica count, then rounds up:
desired replicas = ceil(current replicas × current metric ÷ target metric)For example, if three Pods average 120% CPU utilization against a 60% target, the result is ceil(3 × 120 ÷ 60) = 6. The controller can adjust this result for minimum and maximum replicas, Pods that are not ready, missing metrics, and stabilization rules. The numbers are illustrative, not a test result.
The HPA controller periodically reads metrics and changes the target's scale subresource. CPU and memory resource metrics usually come from the Metrics API provided by metrics-server. Application metrics such as request rate or queue length require an adapter that serves the Custom Metrics or External Metrics API. Creating an HPA object does not install this metrics pipeline.
How should you choose metrics and resource requests?
For CPU or memory, averageUtilization is a percentage comparing actual usage with the container's resources.requests. CPU utilization scaling therefore needs a realistic CPU request for the target containers. Without it, utilization may be undefined; if it is far from actual capacity, the HPA uses a misleading reference point.
averageValue can target an absolute average per Pod. For workloads whose demand is poorly represented by CPU, a business metric such as requests per second or queue depth may be more useful, but it requires collection, an adapter, and a clear mapping to Pods or an external object. With multiple metrics, HPA chooses the largest replica recommendation. If a metric cannot be read, scale-up may still happen, while scale-down can be held back for safety.
Apply and verify a CPU-based HPA
This example targets 60% average CPU utilization for a Deployment named web in namespace app, with between 2 and 10 replicas. The YAML is explanatory and was not run against a cluster.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
namespace: app
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60Set a realistic CPU request on the application containers in the Deployment and confirm that the Metrics API is available. Depending on cluster setup, kubectl top pods -n app shows resource measurements. Then apply and inspect the HPA and workload:
kubectl top pods -n app
kubectl apply -f web-hpa.yaml
kubectl get hpa -n app
kubectl describe hpa web -n app
kubectl get deployment web -n app
kubectl get pods -n appWhen metrics are available, TARGETS shows current and target values, and REPLICAS stays within the configured range. Before load testing, check expected replica growth and spare node capacity. HPA adds Pods, not nodes; if the scheduler has nowhere to place new Pods, they can remain Pending. Node autoscaling or capacity planning is separate.
Reduce abrupt changes and manifest conflicts
Short metric spikes can make replica counts fluctuate. behavior.scaleUp and behavior.scaleDown let you limit change rates and set stabilization windows. A scale-down stabilization window can retain the highest recent recommendation for a period, avoiding a sharp reduction after a temporary drop in load. This trades faster response for spare capacity, so tune it using observed latency and demand patterns.
If the Deployment manifest keeps a fixed spec.replicas, applying it can conflict with the HPA's changes. Configure your deployment tool not to own that field, or define ownership clearly in your declarative workflow. A manual kubectl scale change can also be overwritten by the HPA's next calculation.
What should you check when scaling does not work?
If `TARGETS` is `unknown`, check the Metrics API and metrics-server. If `kubectl top pods -n app` also fails, start with the metrics pipeline.
If the CPU target is unavailable, confirm that target containers have CPU requests and that the Metrics API returns recent Pod measurements.
If replicas do not go below the minimum, check `minReplicas`. If load remains at the maximum, review `maxReplicas`, node resources, and application concurrency limits.
If more replicas do not improve response time, the bottleneck may be a database, network, lock, or external API. Scaling on a metric that does not describe that bottleneck can make it worse.
Inspect events and conditions with `kubectl describe hpa web -n app`. Do not sharply lower the minimum or raise the maximum without first understanding a metrics error.
HPA is a feedback controller that adjusts workload replicas toward configured metric targets. For CPU-based scaling, resource requests and the Metrics API are the foundation; choose metrics that describe real demand and account for node capacity and scaling speed. Replica autoscaling does not replace node scaling, application tuning, or incident response.
References
[Horizontal Pod Autoscaling](https://kubernetes.io/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/) — algorithm, metrics, and scaling behavior
[Resource metrics pipeline](https://kubernetes.io/docs/tasks/debug/debug-cluster/resource-metrics-pipeline/) — kubelet, metrics-server, Metrics API, and `kubectl top`
[HorizontalPodAutoscaler API reference](https://kubernetes.io/docs/reference/kubernetes-api/autoscaling/horizontal-pod-autoscaler-v2/) — `autoscaling/v2` fields and constraints
[Resource management for Pods and Containers](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/) — CPU and memory requests and limits
Scope: This article explains a CPU-based Deployment example with Kubernetes autoscaling/v2, the metrics path, scaling stabilization, and basic diagnosis. Official Kubernetes documentation was checked on 2026-11-23. YAML and commands are explanatory and were not run against a cluster. Validate adapters, request values, node capacity, and scaling policy for your cluster and application.