What is Kubernetes Horizontal Pod Autoscaler (HPA)

Ready to start learning? Individual Plans →Team Plans →

Traffic spikes expose two problems fast: overprovisioning wastes money, and underprovisioning hurts response time. Kubernetes Horizontal Pod Autoscaler (HPA) solves that by changing pod replica counts automatically based on live metrics, so you do not have to babysit scaling during every surge or slowdown.

Quick Answer

Kubernetes HPA is a built-in autoscaling control loop that increases or decreases pod replicas based on observed CPU, memory, or custom metrics. It is best for stateless, horizontally scalable workloads, and it works most reliably when metrics-server is installed, resource requests are defined, and thresholds are tested under real load.

Quick Procedure

  1. Verify metrics-server is installed and returning pod metrics.
  2. Choose a scalable target such as a Deployment.
  3. Set CPU and memory requests on the containers.
  4. Create an HPA with minimum and maximum replica limits.
  5. Start with a CPU utilization target and apply the manifest.
  6. Generate load and watch replicas increase and decrease.
  7. Tune thresholds after checking latency, errors, and pod startup time.
Primary UseAutomatic pod replica scaling as of October 2026
Target ResourcesDeployment, ReplicaSet, StatefulSet, or other scalable controllers as of October 2026
Common MetricsCPU, memory, custom metrics, and external metrics as of October 2026
Required Componentmetrics-server or another metrics source as of October 2026
Best Fit WorkloadsStateless APIs, web apps, workers, and event-driven services as of October 2026
Scaling StyleHorizontal scaling by changing replica count as of October 2026

What Kubernetes Horizontal Pod Autoscaler Is

Horizontal Pod Autoscaler is a Kubernetes feature that adjusts the number of running pods up or down based on observed demand. If traffic rises, HPA increases replicas. If traffic drops, it reduces replicas so you do not keep paying for idle capacity.

The “horizontal” part matters. Vertical Scaling means giving one pod more CPU or memory. HPA does the opposite: it adds more pods instead of making one pod bigger. That is why HPA is usually the better fit for stateless web services and APIs that can run in parallel.

Under the hood, HPA acts as a control loop. Kubernetes checks metrics, compares them to a target, and calculates a desired replica count. The result is responsive scaling without constant manual intervention.

Kubernetes HPA is not magic autoscaling. It is a decision engine that works well only when the workload, metrics, and resource requests are all configured correctly.

In real environments, HPA is commonly used for public APIs, frontend applications, background workers, and event-driven services. It also fits well into Kubernetes Autoscaling, where cluster autoscaling and workload autoscaling work together.

For official implementation details, Kubernetes documents HPA behavior in the upstream docs at Kubernetes Documentation. The key idea is simple: HPA is a built-in mechanism for changing pod replica counts based on measured load, not guessed load.

How Does Kubernetes HPA Work Under the Hood?

HPA works by continuously comparing live metrics against a target and then recalculating the desired replica count. The controller does not react to every tiny fluctuation. It evaluates at intervals, which makes scaling more stable and less noisy.

The workflow is straightforward. Metrics are collected, the controller compares them to a target, and Kubernetes updates the scale subresource for the workload. In practice, that means HPA changes replicas on a Deployment or similar scalable controller rather than touching individual containers directly.

  1. Collect metrics. Kubernetes reads current usage from metrics-server or another metrics source. If the data is stale, missing, or delayed, the scaling decision may be late or inaccurate.

  2. Compare to a target. The HPA compares current usage with the target defined in the manifest. For CPU-based scaling, that target is often an average utilization percentage across pods.

  3. Calculate desired replicas. If utilization is above the target, HPA increases replicas. If it is below the target, HPA lowers them, but only within the configured minimum and maximum bounds.

  4. Apply the change. Kubernetes updates the replica count for the target controller. The controller then creates or removes pods until the new desired state is reached.

  5. Recheck on the next loop. HPA keeps evaluating, which is why it can correct for rising or falling demand without human intervention.

That periodic behavior matters. If HPA reacted instantly to every short spike, you would get Oscillation: rapid scale-out and scale-in thrashing. The control loop smooths that behavior, which makes production systems more predictable.

Note

HPA scales the workload controller, not the pod itself. If your application cannot tolerate more replicas, the autoscaler will not fix that architectural limitation.

The official Kubernetes documentation explains the control loop model and supported metric types. For operators, the practical lesson is clear: HPA is only as good as the metric pipeline feeding it.

What Metrics Can HPA Use?

HPA can use CPU, memory, custom application metrics, and external metrics. The most common starting point is CPU because it is simple to observe and easy to reason about. But CPU is not always the best signal for demand.

CPU-based scaling works well when higher traffic usually means more compute work. That is why it fits request-heavy APIs and many web services. The main limitation is that CPU does not always reflect user experience. A service can be slow because of database waits, queue backlogs, or downstream timeouts while CPU remains low.

CPU utilization

CPU is the most common metric because it is supported in the basic HPA pattern and is easy to define as a percentage target. If your pods request 500m CPU and you set a target of 70 percent, HPA tries to keep average usage near that threshold.

Memory usage

A kubernetes hpa memory example is useful when workload growth tracks memory pressure more than CPU, such as caching services or applications with large in-memory sessions. Memory-based scaling can work, but it has a drawback: memory often grows slowly and does not always drop quickly after load decreases.

That makes memory a weaker signal for bursty services. A memory spike may also indicate a leak or bad caching behavior, not healthy demand. In those cases, scaling out can hide a problem instead of fixing it.

Custom and external metrics

Custom metrics are workload-specific signals such as request latency, queue depth, active sessions, or messages waiting in a broker. External metrics come from systems outside the cluster, such as cloud queues or third-party event sources.

  • Request rate works well for web APIs with predictable traffic per pod.
  • Queue depth is useful for background workers that should scale when jobs pile up.
  • Active sessions can help when one pod can only support a limited number of connected users.
  • Latency can be useful, but it should be paired with careful alerts because latency can rise for many reasons besides load.

For Kubernetes autoscaling guidance, the upstream docs remain the best starting point: Kubernetes Horizontal Pod Autoscaler. If you need application-specific signals, you also need a stable metrics pipeline that can export them consistently.

Prerequisites

HPA works reliably only when the cluster and workload are prepared for it. Before you create an autoscaler, check the basics first. Missing prerequisites are one of the most common reasons people think HPA is “broken.”

  • metrics-server or another metrics source must be installed and healthy.
  • Resource requests for CPU and memory should be defined on the container spec.
  • A scalable workload such as a Deployment should exist as the HPA target.
  • Application design should support horizontal scaling, ideally with stateless components.
  • Permissions must allow you to create and manage autoscaling resources.
  • Baseline observability should exist so you can compare replica changes with latency and error rates.

If you are using CPU utilization targets, resource requests matter a lot. Kubernetes calculates utilization relative to requested CPU, not node capacity. If requests are too low, HPA may scale too aggressively. If requests are too high, HPA may wait too long to react.

To confirm metrics availability, run commands such as kubectl top pods and kubectl top nodes. If those commands return errors, fix metrics collection before creating the HPA. The Kubernetes metrics pipeline is documented at Resource Metrics Pipeline.

When Should You Use HPA, and When Should You Not?

HPA is the right tool for workloads that can add replicas without breaking state or consistency. It is a strong choice for stateless APIs, frontend web services, queue consumers, and event processors that need to absorb changing traffic.

It is often a poor fit for workloads that depend on fixed node identity, local disk state, or tight inter-pod coordination. Databases, consensus systems, and some session-heavy services may need different scaling strategies or architectural changes before HPA makes sense.

  • Good fit: public APIs that see daytime peaks and overnight lulls.
  • Good fit: worker pools that drain queues faster during bursts.
  • Good fit: web apps that can serve requests from any replica.
  • Poor fit: tightly coupled services that require stable pod identities.
  • Poor fit: workloads where one instance holds critical in-memory state.
  • Poor fit: apps that cannot start quickly enough to catch sudden demand spikes.

HPA is also not a substitute for efficient code. If the app is slow because of bad database queries, excessive locking, or inefficient serialization, more replicas only postpone the problem. Good autoscaling starts with a workload that is designed to scale horizontally in the first place.

Autoscaling should match architecture. If the app cannot safely run in multiple copies, no HPA manifest will make it scalable.

For broader scaling context, Kubernetes docs on autoscaling are useful, but so are upstream application design patterns. When in doubt, ask whether the workload can tolerate one pod disappearing at any moment. If the answer is no, HPA is probably not the first solution to reach for.

How to Configure a Basic HPA

Configuring HPA starts with the workload, not the autoscaler. The safest path is to begin with a Deployment, confirm metrics are available, define CPU requests, and then apply a simple HPA manifest with conservative limits.

  1. Confirm metrics are available. Run kubectl top pods and verify the cluster returns live values. If metrics are missing, install or repair metrics-server before continuing.

  2. Create or update a Deployment. HPA needs a scalable target. A Deployment is the most common choice because it manages replicas cleanly and supports rolling updates.

  3. Set resource requests. Add CPU requests such as requests: { cpu: "250m" }. Without requests, CPU utilization targets may be meaningless or unavailable.

  4. Define replica bounds. Set a sensible minReplicas and maxReplicas. This prevents underscaling during load and runaway replica counts during a spike.

  5. Start with CPU utilization. Keep the first rule simple. CPU is easier to understand than custom metrics, and it gives you a baseline for tuning.

  6. Apply and observe. Use kubectl apply -f hpa.yaml, then watch how replicas change while you generate realistic traffic.

Start conservatively. A minimum of 2 replicas is common for availability, while the maximum depends on how much traffic your service can absorb and how much cluster capacity is available. The right range is not theoretical; it comes from load testing and observed production patterns.

The upstream Kubernetes autoscaling docs provide the canonical behavior of HPA resources at kubernetes.io. If your deployment also uses cluster autoscaling, make sure node capacity can grow when HPA asks for more pods.

What Does a Basic HPA Manifest Look Like?

An HPA manifest tells Kubernetes what to watch, what to scale, and how far to scale it. The most important fields are the target workload, the replica limits, and the metric target. Keep the first version simple enough that any operator can read it in a minute.

Here is the logic behind the fields, in plain English:

  • targetRef points to the Deployment or other scalable resource.
  • minReplicas sets the floor so the service does not scale to zero unless that is intentional and supported.
  • maxReplicas sets the ceiling so a traffic spike does not consume all cluster capacity.
  • metrics defines the signal HPA uses, such as CPU utilization.

For CPU-based autoscaling, the target is often expressed as an average utilization percentage. If the target is 70 percent, Kubernetes tries to keep average CPU use near that point across replicas. If the workload rises above target, HPA adds pods. If it falls below target, HPA removes pods within the configured limits.

Field Operational Meaning
minReplicas Lowest safe replica count during quiet periods
maxReplicas Highest allowed replica count during spikes
targetAverageUtilization Desired average usage level that triggers scaling
scaleTargetRef The workload HPA will control

Version-control the manifest and keep it readable. Autoscaling thresholds are operational policy, not temporary test settings. That makes change tracking just as important as the YAML itself.

How to Test HPA in a Real Environment

Testing HPA means checking behavior under load, not just confirming that the YAML applies cleanly. A good test includes a traffic ramp-up, a sustained peak, and a recovery period. You want to see replicas increase when demand rises and fall back when demand drops.

Use realistic traffic generators and watch more than replica counts. Check request latency, 5xx error rates, pod readiness, and container restarts. If the application becomes unhealthy before HPA reacts, the threshold is probably too high or the warm-up time is too slow.

  1. Start with a known baseline and note current replica count, CPU usage, and response times.

  2. Increase traffic gradually so you can see when HPA begins to react.

  3. Hold the load steady long enough for HPA to make multiple decisions.

  4. Drop the load and confirm replicas decrease without flapping.

  5. Review events with kubectl describe hpa and compare them to application metrics.

Pod startup time matters. If your server takes 60 seconds to become ready, HPA may technically work while users still feel the lag. In that case, the fix may involve image size, startup optimization, readiness probes, or a higher minimum replica count.

Pro Tip

Test both the scale-out path and the scale-in path. Many teams only verify that HPA adds pods, then discover later that it scales down too slowly or too aggressively.

What Are the Most Common HPA Problems?

Most HPA failures are caused by configuration gaps, not by the autoscaler itself. Missing resource requests, broken metrics pipelines, and narrow replica limits are the top causes of bad behavior. Start troubleshooting there before blaming Kubernetes.

If resource requests are missing, CPU utilization targets may not work as intended. If metrics-server is unhealthy, HPA can lose the signal it needs to make decisions. If min and max replicas are too close together, the autoscaler has no room to react meaningfully.

  • No resource requests: CPU utilization may be undefined or misleading.
  • Stale metrics: HPA decisions lag behind real demand.
  • Too-low maxReplicas: The service hits a ceiling before traffic peaks are handled.
  • Too-high minReplicas: Costs stay high even during quiet periods.
  • Slow pod startup: Users feel the delay before new replicas are ready.
  • Noisy workload: Replica counts bounce around because the signal is unstable.

Another common issue is metric mismatch. If you use CPU for a service that spends most of its time waiting on a queue or downstream API, the autoscaler may underreact. In that case, queue depth or request rate may be a better signal than raw CPU.

For cluster-level troubleshooting, Kubernetes events and kubectl describe hpa are your first tools. If the HPA status shows “unable to get metrics” or “failed to compute desired replica count,” fix the metrics path before tuning thresholds.

How Do You Tune HPA for Reliable Scaling?

Reliable HPA tuning is mostly about matching the metric to the workload and setting realistic limits. The best starting point is conservative: choose a stable metric, set a reasonable minimum, and expand the max only as far as the application and cluster can support.

Use metrics that reflect actual user demand or service saturation. CPU works for many services, but queue depth, request rate, or active sessions may tell the real story more accurately. That is especially true for worker pools and event-driven services.

  • Start simple: use one metric before mixing several.
  • Set a safe floor: keep enough replicas to survive small spikes.
  • Set a real ceiling: tie max replicas to cluster capacity and app limits.
  • Watch latency and errors: replica count alone does not show user impact.
  • Review after releases: new code can change CPU patterns and startup time.

HPA should be revisited whenever traffic patterns change, a new release alters performance, or infrastructure capacity changes. What worked at one traffic level may be too aggressive or too cautious later.

The best HPA configuration is the one that matches actual traffic, not the one that looks neat in YAML.

For operational discipline, keep the manifest in source control, review scaling events regularly, and treat HPA thresholds like any other production control. That makes tuning repeatable instead of ad hoc.

How Does HPA Fit Into a Broader Kubernetes Autoscaling Strategy?

HPA handles pod scaling, while cluster autoscaling handles node capacity. Those are different layers of the stack, and they solve different problems. HPA says, “This workload needs more replicas.” Cluster autoscaling says, “The cluster needs more room for those replicas.”

Many teams use both together. HPA adds pods when demand rises, and the cluster autoscaler adds nodes when the scheduler runs out of space. That combination gives you end-to-end elasticity, which is the real goal in production systems.

  • HPA: scales the number of pods.
  • Cluster autoscaling: scales node capacity.
  • Vertical scaling: increases CPU or memory per pod when replica scaling is not the best fit.

These strategies do not compete. They complement each other when used properly. For example, a stateless API might use HPA for traffic spikes, cluster autoscaling for capacity expansion, and vertical scaling only when a pod needs more memory to handle a larger working set.

The Kubernetes documentation on autoscaling is the best authoritative reference for the mechanics of the HPA layer: Kubernetes HPA docs. The important operational point is that autoscaling usually works best as a layered system, not as a single feature you turn on once and forget.

Key Takeaway

  • Kubernetes HPA automatically changes pod replicas based on live metrics, which makes it ideal for variable traffic.
  • CPU-based scaling is the simplest starting point, but memory, custom metrics, and external metrics often fit better for specific workloads.
  • metrics-server, resource requests, and a scalable workload such as a Deployment are required for reliable results.
  • HPA works best for stateless services that can add replicas without breaking application behavior.
  • Good tuning depends on real load testing, sane min/max limits, and ongoing review of latency and error rates.

Conclusion

Kubernetes Horizontal Pod Autoscaler is the standard way to scale pod replicas automatically based on demand. It helps you respond to traffic spikes, reduce idle waste, and cut down on manual scaling work. When HPA is configured correctly, it gives you predictable elasticity without constant operator intervention.

The core lesson is simple. HPA works best when the workload can scale horizontally, the metrics are accurate, and the resource requests are defined correctly. Start with CPU-based scaling, validate it under load, and then move to memory or custom metrics only if your application needs them.

If you want the most reliable result, treat HPA as part of a larger autoscaling strategy, not a standalone fix. Review your application architecture, check your metrics pipeline, and tune replica limits based on actual traffic. That is the difference between autoscaling that looks good on paper and autoscaling that works in production.

Next step: apply a basic HPA to one stateless service, test it under a realistic load spike, and adjust the thresholds based on what you observe in latency, CPU, and replica changes.

Kubernetes is a trademark of the Linux Foundation. Kubernetes Horizontal Pod Autoscaler is used here for descriptive purposes.

[ FAQ ]

Frequently Asked Questions.

What is the primary purpose of the Kubernetes Horizontal Pod Autoscaler (HPA)?

The primary purpose of the Kubernetes Horizontal Pod Autoscaler (HPA) is to automatically adjust the number of pod replicas in a deployment based on real-time metrics like CPU utilization, memory usage, or custom metrics.

This dynamic scaling helps ensure applications maintain optimal performance during traffic spikes and avoid overprovisioning during low demand, leading to better resource utilization and cost efficiency.

How does the Kubernetes Horizontal Pod Autoscaler (HPA) determine when to scale up or down?

The HPA continuously monitors specified metrics such as CPU utilization, memory consumption, or custom metrics through the Kubernetes Metrics Server or other monitoring solutions.

If the observed metrics cross predefined thresholds—like CPU usage exceeding 70%—the HPA increases the number of pod replicas. Conversely, if metrics fall below set thresholds, it scales down the number of pods to save resources.

What are the common metrics used by the Kubernetes Horizontal Pod Autoscaler (HPA)?

The most common metrics used by HPA include CPU utilization and memory usage, which are standard resource metrics available in Kubernetes.

Additionally, HPA can utilize custom metrics, such as request rate or application-specific indicators, through integrations with monitoring tools like Prometheus, enabling more granular control over autoscaling policies.

Are there any prerequisites or configurations needed to enable HPA in Kubernetes?

To enable HPA, your Kubernetes cluster must have the Metrics Server installed and properly configured, as it provides the necessary resource metrics for autoscaling decisions.

Furthermore, you need to define a HorizontalPodAutoscaler resource specifying the target deployment, metric thresholds, and minimum/maximum replica counts. Proper permissions and labels are also essential for smooth operation.

What are the benefits of using Kubernetes Horizontal Pod Autoscaler (HPA)?

Using HPA offers several benefits, including improved application performance during traffic surges, efficient resource utilization, and cost savings by avoiding overprovisioning.

It also provides operational flexibility, allowing applications to adapt automatically to changing workloads without manual intervention, thereby enhancing reliability and reducing management overhead.

Related Articles

Ready to start learning? Individual Plans →Team Plans →
Discover More, Learn More
What Is a Kubernetes Volume? Learn how Kubernetes Volumes enable persistent, shared storage for containers, helping you… What is Kubernetes StatefulSet? Discover how Kubernetes StatefulSets ensure reliable, persistent workloads with 100% pod stability,… What is Google Kubernetes Engine (GKE)? Discover how Google Kubernetes Engine simplifies deploying, managing, and scaling containerized applications… What Is Kubernetes Deployment? Discover how Kubernetes deployment strategies ensure zero downtime updates, improve application reliability,… What Is Horizontal Scaling? Discover how horizontal scaling enhances your application's performance and reliability by adding… Kubernetes vs. Docker: Understanding the Differences and Use Cases Discover the key differences and use cases of Kubernetes and Docker to…
FREE COURSE OFFERS