Horizontal Pod Autoscaler

Also known as: HPA

On this page 7

The Horizontal Pod Autoscaler (HPA) is the built-in Kubernetes controller that automatically changes the number of Pod replicas in a Deployment, StatefulSet or other scalable workload so that observed load stays close to a target metric value. Instead of giving existing Pods more CPU or memory, it scales horizontally: it adds identical replicas when demand rises and removes them when demand falls.

In cloud-native infrastructure, the HPA is the first line of elastic capacity. It reacts to measured utilization within seconds to minutes, keeps response times stable during traffic peaks and lowers cost during quiet periods. It depends on resource requests, a working metrics pipeline and an application that can run as several interchangeable copies.

How the HPA control loop calculates replicas

The HPA works as a periodic control loop inside the kube-controller-manager, which by default evaluates every HorizontalPodAutoscaler object every 15 seconds. In each cycle it reads current metric values, compares them with the targets defined in the object and writes a new replica count to the scale subresource of the target workload. The Deployment or StatefulSet controller then creates or deletes Pods.

The core formula is desiredReplicas = ceil(currentReplicas × currentValue / targetValue). If four replicas run at 90% average CPU utilization against a 60% target, the result is ceil(4 × 90 / 60) = 6 replicas. A tolerance of 10% by default prevents changes when the ratio is close to 1.0, which avoids constant small adjustments. When several metrics are defined, the HPA computes a proposal for each and applies the highest replica count.

Metric values come from aggregated APIs. Resource metrics such as CPU and memory are served by the metrics.k8s.io API, usually provided by Metrics Server. Custom and external metrics come from custom.metrics.k8s.io and external.metrics.k8s.io, typically implemented by an adapter for Prometheus or a cloud monitoring service. The table below summarizes the moving parts.

ComponentRole in the loopTypical implementation
HPA controllerComputes desired replicas each cyclekube-controller-manager
HorizontalPodAutoscaler objectDeclares target, metrics and boundsautoscaling/v2 manifest
Resource metrics APISupplies CPU and memory usageMetrics Server
Custom metrics APISupplies per-Pod or per-object metricsPrometheus Adapter
External metrics APISupplies metrics from outside the clusterCloud monitoring adapter, KEDA
Scale subresourceReceives the new replica countDeployment, StatefulSet, ReplicaSet
Components involved in horizontal pod autoscaling

Metric types and target definitions

The autoscaling/v2 API supports four metric source types, and each one answers a different scaling question. Utilization targets are expressed as a percentage of the Pod resource request, not of node capacity, so a workload without CPU requests cannot be scaled on CPU utilization at all. The following table compares the options.

Metric typeWhat it measuresTypical targetBest suited for
ResourceCPU or memory of each PodUtilization 50-80%Stateless web and API services
ContainerResourceUsage of one named containerUtilization 60-75%Pods with heavy sidecars
PodsCustom per-Pod metricAverage value, such as 100 requests/sRequest-driven services
ObjectMetric of another Kubernetes objectValue, such as Ingress requests/sTraffic measured at the edge
ExternalMetric outside the clusterQueue length per replicaQueue consumers and workers
Metric source types in the autoscaling/v2 API

Each HPA also defines minReplicas and maxReplicas. The minimum protects availability and absorbs sudden spikes before new Pods become ready; the maximum caps cost and protects downstream systems such as databases. Choosing a target such as 60-70% CPU utilization leaves headroom for the time that new replicas need to start, pass readiness probes and receive traffic.

Scaling behavior, stabilization and policies

The behavior field controls how fast the HPA scales in each direction. A stabilization window makes the controller consider the recommendations of a recent period and choose the most conservative one. By default, scale-down uses a 300-second window, so a short dip in traffic does not remove replicas that will be needed again a minute later, while scale-up has no window and reacts immediately.

Scaling policies limit the size of each step, either as a number of Pods or a percentage per period. The default scale-up policy allows the larger of doubling the replica count or adding four Pods every 15 seconds. Tuning these settings is the main defense against flapping, the oscillation between scale-up and scale-down that wastes resources and disturbs caches and connection pools.

SettingDefaultEffect of increasing it
Scale-down stabilization window300 sFewer premature removals, slower cost savings
Scale-up stabilization window0 sLess reaction to brief spikes, slower response
Scale-up percent policy100% per 15 sFaster growth during sudden surges
Scale-down percent policy100% per 15 sFaster shrinking after load drops
Tolerance0.1Fewer small adjustments around the target
Common behavior settings and their effects

How the HPA compares with other autoscalers

The HPA is one layer in a multi-level autoscaling stack, and it rarely works alone. It changes how many Pods exist, while other components decide how large each Pod is and whether the cluster has enough nodes to schedule them. Confusing these layers is a frequent cause of failed scaling during incidents.

AutoscalerWhat it changesTriggerMain limitation
Horizontal Pod AutoscalerNumber of Pod replicasPod or external metricsNeeds horizontally scalable workloads
Vertical Pod AutoscalerCPU and memory requestsHistorical usageConflicts with HPA on the same resource
Cluster AutoscalerNumber of nodesPending unschedulable PodsNode startup takes minutes
KEDAReplicas, including to zeroEvent sources such as queuesAdditional operator to run
Autoscaling layers in a Kubernetes cluster

A typical production pattern combines the HPA for replicas with the Cluster Autoscaler or a node provisioner for capacity. Running the Vertical Pod Autoscaler on the same CPU or memory metric as the HPA creates a feedback conflict, because changing requests also changes the utilization percentage the HPA sees.

Common mistakes and limitations

Most HPA problems stem from missing prerequisites or unsuitable metrics, not from the controller itself. CPU utilization is a weak signal for I/O-bound or latency-sensitive services, and memory rarely falls after load drops because many runtimes keep allocated heap. The list below covers the issues practitioners meet most often.

  • Missing resource requests: the HPA reports an unknown metric and never scales.
  • Fixed replicas in the Deployment manifest: GitOps tools or kubectl apply reset the count the HPA set; omit the replicas field when an HPA manages the workload.
  • Slow startup: Pods that need 90 seconds to warm up make scale-up lag behind demand; use readiness probes and lower targets.
  • Memory-based scale-down: memory stays high after traffic falls, so replicas never decrease.
  • Downstream limits: scaling to 50 replicas can exhaust database connections; set maxReplicas from real capacity.

The HPA also cannot scale to zero with standard settings, because minReplicas must be at least one unless an alpha feature gate is enabled. Event-driven workloads that idle for hours are therefore often handled by KEDA, which manages an HPA internally and adds the zero-to-one activation step.

The simple explanation

Imagine a supermarket that opens more checkout lanes when queues grow and closes them when the store is quiet. The Horizontal Pod Autoscaler does the same for an application. It watches how busy the running copies are and adds or removes copies automatically, so customers wait less during rush hour and the company pays less at night.

It does not make any single copy stronger. It only changes how many copies are working, within a minimum and maximum the team sets in advance.

How it is used in practice

An online ticket seller runs its checkout API as a Deployment with CPU requests of 500 millicores per Pod. The team sets an HPA with minReplicas of 4, maxReplicas of 40 and a target of 65% average CPU utilization, plus a second metric of 120 requests per second per Pod from a Prometheus adapter. The scale-down stabilization window is raised to 600 seconds because ticket releases come in waves.

When a popular concert goes on sale, traffic jumps tenfold within two minutes. The HPA doubles replicas in successive cycles, reaching 32 Pods, while the Cluster Autoscaler adds six nodes for the pending Pods. Median checkout latency stays under 300 milliseconds throughout the sale. An hour later, as traffic falls, the HPA gradually returns to four replicas and the extra nodes are released, so the peak capacity is paid for only while it is needed.