Inferensys

Glossary

Horizontal Pod Autoscaler (HPA)

A Kubernetes control loop that automatically scales the number of pod replicas in a deployment, replica set, or stateful set based on observed CPU utilization or custom metrics to maintain a target resource usage.
DevOps engineer deploying LLM to production on laptop, Kubernetes dashboards visible, late night deployment session.
KUBERNETES AUTOSCALING

What is Horizontal Pod Autoscaler (HPA)?

A Kubernetes control loop that automatically scales the number of pod replicas in a deployment based on observed CPU utilization or custom metrics to maintain a target resource usage.

The Horizontal Pod Autoscaler (HPA) is a Kubernetes control loop that automatically adjusts the number of pod replicas in a Deployment, ReplicaSet, or StatefulSet based on observed metrics. It periodically queries the metrics server to compare current CPU utilization or custom metrics against a defined target, then scales the workload horizontally to maintain that target, ensuring application performance and efficient resource utilization.

In disconnected or air-gapped environments, the HPA relies on a locally deployed metrics server and custom metrics adapters, as external monitoring services are unavailable. The algorithm calculates the desired replica count using the formula desiredReplicas = ceil[currentReplicas * (currentMetricValue / desiredMetricValue)], with a configurable stabilization window to prevent thrashing during metric fluctuations.

AUTOMATED SCALING MECHANICS

Key Features of the Horizontal Pod Autoscaler

The Horizontal Pod Autoscaler (HPA) is a Kubernetes control loop that dynamically adjusts the number of pod replicas in a Deployment, ReplicaSet, or StatefulSet based on observed resource utilization or custom metrics.

01

Metrics-Driven Scaling

The HPA's core logic relies on a continuous feedback loop that queries aggregated resource metrics to make scaling decisions. It fetches per-pod resource metrics from the metrics-server or custom metrics from an external provider via the custom.metrics.k8s.io API.

  • Resource Metrics: Default support for target average CPU utilization and average memory utilization.
  • Custom Metrics: Scale on any metric exposed by your pods, such as requests-per-second or queue depth.
  • External Metrics: Scale based on metrics from outside the cluster, like cloud provider message queue length.

The controller calculates the desired replica count using the formula: desiredReplicas = ceil[currentReplicas * (currentMetricValue / desiredMetricValue)].

< 15 sec
Default Metric Sync Period
02

Stabilization and Cooldown

To prevent thrashing—rapid, oscillating scale-up and scale-down events—the HPA implements configurable stabilization windows. These windows define a look-back period during which the controller considers past recommendations before acting.

  • Scale-Down Stabilization: Defaults to 5 minutes. The HPA uses the highest recommendation from this window, preventing premature termination of pods during transient load drops.
  • Scale-Up Policy: Can be configured with a specific policy (e.g., Pods or Percent) and a limit to control the rate of rapid expansion.
  • Customizable Behavior: The behavior field in the HPA v2 API allows fine-tuning of both scale-up and scale-down policies independently.
300 sec
Default Scale-Down Stabilization
03

Container Resource Metrics

Introduced in the autoscaling/v2 API, the HPA can target resource utilization of individual containers within a pod, rather than the aggregate pod total. This is critical for multi-container pods where a sidecar proxy or logging agent should not influence the scaling decision of the main application container.

  • Target Type: Set ContainerResource as the metric type.
  • Precision: Specify the exact container name to isolate its CPU or memory usage.
  • Use Case: Essential for service mesh architectures where an Istio sidecar or Cloud SQL Auth proxy runs alongside the primary workload.
04

HPA and VPA Coexistence

The Horizontal Pod Autoscaler can operate alongside the Vertical Pod Autoscaler (VPA) to achieve both horizontal elasticity and right-sized resource requests. However, they must not fight over the same resource dimensions.

  • Avoid Conflict: If VPA manages CPU or memory, the HPA must not use those same metrics for scaling. Instead, use custom or external metrics for the HPA.
  • Complementary Strategy: Use VPA in Off or Auto mode to set optimal base resource requests, while the HPA scales replica count based on throughput or latency.
  • Golden Signal: A common pattern is to use VPA for memory sizing and HPA for CPU or custom metrics like request latency.
05

API Version Evolution

The HPA has evolved through distinct API versions, each adding more granular control. Understanding the differences is crucial for writing portable and modern manifests.

  • autoscaling/v1: Supports only CPU utilization scaling. The simplest form, now considered legacy.
  • autoscaling/v2beta2: Introduced support for memory and custom/external metrics. This was the bridge to the modern API.
  • autoscaling/v2: The stable, current version. Supports all metric types (Resource, Pods, Object, External, ContainerResource) and the behavior field for fine-tuning scaling policies.
06

Idle and Unready Pod Handling

The HPA has specific logic for handling pods that are not in a Ready state or are missing metrics, ensuring that a single faulty pod does not skew the scaling algorithm.

  • Missing Metrics: Pods that fail to report metrics are ignored during the calculation of the current utilization ratio. They are treated as if they have 0% utilization for scale-down decisions and 100% for scale-up, erring on the side of caution.
  • Unready Pods: Pods in a non-Ready state with a deployment.kubernetes.io/revision label are ignored entirely during scaling calculations.
  • Startup Tolerance: The HPA includes a configurable tolerance flag (default 0.1) to ignore minor fluctuations around the target metric, preventing unnecessary scaling noise.
HORIZONTAL POD AUTOSCALER

Frequently Asked Questions

Clear answers to the most common questions about how the Horizontal Pod Autoscaler automatically scales Kubernetes workloads based on real-time resource metrics.

A Horizontal Pod Autoscaler (HPA) is a Kubernetes control loop that automatically adjusts the number of pod replicas in a Deployment, StatefulSet, or ReplicaSet based on observed resource utilization. The HPA controller queries the metrics server at regular intervals—defaulting to 15 seconds—to retrieve CPU or memory usage for each pod. It then calculates the ratio of current utilization to the target utilization defined in the manifest. For example, if a deployment targets 50% CPU and current usage averages 80%, the HPA scales up using the formula: desiredReplicas = ceil[currentReplicas * (currentMetricValue / desiredMetricValue)]. The controller respects configurable boundaries set by minReplicas and maxReplicas, and includes a stabilization window to prevent thrashing during traffic spikes. HPA can also scale on custom metrics from external sources like Prometheus or cloud monitoring APIs, enabling scaling decisions based on request latency, queue depth, or application-specific signals.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.