Kubernetes Horizontal Pod Autoscaling: How It Works
Horizontal Pod Autoscaling adds or removes pods as load changes, so a Kubernetes workload scales without manual work. Here is how it decides, and how to set it up.
On this page
Short answer
Kubernetes Horizontal Pod Autoscaling, or HPA, is a Kubernetes feature that adds or removes replicas of a workload automatically as demand changes. It watches a metric such as CPU use or request rate, compares it with a target you set, and changes the number of running pods so the workload keeps up with load without manual intervention.
What is Kubernetes Horizontal Pod Autoscaling?
Kubernetes Horizontal Pod Autoscaling, or HPA, is a Kubernetes feature that adds or removes replicas of a workload automatically as demand changes. It watches a metric such as CPU use or request rate, compares it with a target you set, and changes the number of running pods so the workload keeps up with load without manual intervention.
Horizontal scaling means adding more copies of the same thing. A deployment with three pods can grow to ten when traffic rises, then shrink back to three when it falls. That is different from vertical scaling, which gives each pod more CPU or memory, and from cluster scaling, which adds more nodes to the cluster.
HPA matters because traffic is rarely flat. A shop gets more visitors in the evening, a report job spikes at the end of the month, and a marketing campaign can double traffic in an hour. Without autoscaling, someone has to watch the load and change the replica count by hand, which is slow and error-prone. For teams running workloads in the cloud, cloud services provide the nodes HPA scales across.
How does HPA decide to scale?
HPA works in a loop. Every few seconds the HPA controller reads the current value of a metric for a workload, compares it with the target, and works out how many replicas would bring the metric back to target. If the answer is more than the current count, it adds pods; if it is fewer, it removes them.
The simplest metric is CPU use. If you set a target of 70 percent average CPU and the pods are running at 100 percent, HPA roughly doubles the replica count, because twice as many pods would share the same work at about half the CPU each. The same logic applies to memory use and to custom metrics such as requests per second.
HPA does not scale instantly. It waits for a short stabilisation period before scaling down, so a brief dip in traffic does not remove pods that will be needed again in a minute. Scaling up is faster, because running out of capacity hurts more than running a few extra pods for a while.
| Metric | What it measures | Good for |
|---|---|---|
| CPU use | Average CPU across pods | General workloads with steady work |
| Memory use | Average memory across pods | Workloads that grow memory with load |
| Requests per second | HTTP requests per pod | Web services with clear request load |
| Queue length | Items waiting in a queue | Workers that process jobs from a queue |
| Custom metric | Any number from your app | Workloads where CPU does not track load |
Custom metrics need a metrics pipeline, such as Prometheus with an adapter, so HPA can read them. CPU and memory work out of the box with the metrics server, which is why most teams start there.
HPA vs vertical scaling vs cluster autoscaling
HPA, vertical scaling and cluster autoscaling solve different problems, and most production clusters use all three. HPA changes the number of pods. Vertical scaling, through the Vertical Pod Autoscaler, changes how much CPU and memory each pod gets. Cluster autoscaling changes how many nodes the cluster has.
The three work together in a chain. When traffic rises, HPA adds pods. If the nodes run out of room for those pods, the cluster autoscaler adds a node. If a single pod is too small for its work, vertical scaling gives it more resources. Each layer handles the problem the layer below cannot fix.
Using HPA and vertical scaling on the same metric at the same time is a bad idea, because they fight each other: HPA adds pods while vertical scaling grows each pod, and the two never settle. Pick one for CPU and memory, and use the other for a different metric, or run them on different workloads.
Cluster autoscaling also has its own limits. Adding a node takes minutes, while adding a pod takes seconds, so a sudden spike still needs spare room on the existing nodes. Teams that expect sharp spikes keep a small buffer of spare capacity, so HPA can act at once while the cluster catches up.
How to set up HPA in six steps
Setting up HPA is straightforward once the workload has resource requests set, because HPA needs requests to work out what a percentage of CPU actually means.
- Set resource requests on the workload. Give each pod a CPU and memory request, so Kubernetes knows how much capacity a pod needs and HPA knows what 70 percent means.
- Install the metrics server. Most managed clusters include it; on a self-managed cluster, install it so CPU and memory numbers are available to HPA.
- Create the HPA object. Name the workload, set the metric and target, and set the minimum and maximum replica counts. The maximum should match what the cluster can actually run.
- Load test the workload. Send traffic at the level you expect, watch the replica count move, and check that the app starts fast enough for new pods to help.
- Set the stabilisation windows. Give scale-down a longer window than scale-up, so a brief dip does not remove pods that will be needed again soon.
- Watch it for a week. Check the replica history, the metric values and any events where HPA hit the maximum. Adjust the target and the limits based on what you see, not on what you guessed.
Teams that manage clusters as part of a wider platform often hand this work to DevOps services, since the tuning depends on the app, the traffic pattern and the cluster, not just on the HPA settings.
Which metrics and targets should you use?
CPU use is the right first metric for most workloads, with a target of 60 to 80 percent. A lower target leaves more headroom for spikes but runs more pods; a higher target saves money but leaves less room when traffic jumps. Start at 70 percent and adjust after a week of real traffic.
Memory is a poor scaling metric for most apps, because memory use rarely falls when load falls. A pod that caches data keeps its memory even when idle, so HPA would never scale it down. Use memory as a metric only when the app genuinely releases memory as load drops.
Requests per second is the best metric for web services, because it tracks the work users actually create. It needs a metrics pipeline, so it is a second step, not a first one. Queue length is the best metric for workers, because it directly measures the backlog the workers exist to clear.
Whatever metric you pick, set a minimum replica count that keeps the service up during a node failure, and a maximum that the cluster and the database can handle. An HPA that scales a web tier to fifty pods while the database can only handle ten connections per pod will break the database, not help the service.
Example (hypothetical): a shop scales for a sale
Example (hypothetical): an online shop runs its checkout service with 4 pods, each using about 70 percent CPU at normal traffic. HPA is set with a CPU target of 70 percent, a minimum of 4 pods and a maximum of 20 pods.
A sale starts at 6 p.m. and traffic triples. CPU use climbs to 100 percent on all 4 pods, so HPA works out that 12 pods would bring the average back to about 70 percent. It adds pods in two rounds, reaching 12 pods within a few minutes, and CPU settles near the target.
At 10 p.m. traffic falls back to normal. HPA waits through its scale-down window, then removes pods a few at a time until the service is back to 4 pods. The shop handled the sale without anyone touching the replica count, and the bill for the extra pods only covered the hours they ran.
The key number is the start-up time. If a new pod takes three minutes to become ready, HPA must start scaling before the old pods are full, which is why the target is 70 percent and not 95 percent.
What are the common mistakes?
The most common mistakes are forgetting resource requests, setting the maximum too high, and scaling on the wrong metric.
- No resource requests set. Without requests, HPA cannot work out what a CPU percentage means, and it will not scale at all.
- Maximum replicas too high. Fifty pods can overwhelm a database or a downstream service that was never sized for them.
- Scaling on memory for a caching app. Memory does not fall with load, so the workload never scales down.
- Running HPA and vertical scaling on the same metric. The two fight each other and the replica count never settles.
- Ignoring pod start-up time. If a pod takes minutes to start, scaling must begin early, with a lower target.
- No minimum replicas. Scaling to zero leaves nothing running when the next request arrives.
Autoscaling is not a substitute for load testing. Test the workload under real traffic, watch how HPA behaves, and tune the targets before the traffic arrives for real. For teams building the app itself, custom software development should include start-up speed as a requirement, since slow starts make every autoscaler slower.
Key takeaways
- HPA adds or removes pods as a metric moves away from its target
- CPU use is the best first metric; requests per second suits web services best
- HPA, vertical scaling and cluster autoscaling solve different problems and work together
- Resource requests are required, or HPA cannot work out what a percentage means
- Set a minimum that survives a node failure and a maximum the database can handle
Frequently asked questions
Does HPA work with any Kubernetes workload?
HPA works with Deployments, StatefulSets, ReplicaSets and ReplicationControllers. It does not work with a single Pod, because there is nothing to scale. DaemonSets are also out of scope, since they run one pod per node by design.
How fast does HPA react to a traffic spike?
HPA checks metrics roughly every 15 seconds and can add pods within a minute, but the new pods still need to start and become ready. If a pod takes three minutes to start, the service feels the spike for three minutes, which is why headroom and a sensible target matter more than the check interval.
Can HPA scale a workload to zero?
Yes, if the minimum replica count is set to zero, but that is rarely a good idea for a service that must answer requests. Scaling to zero saves money for batch jobs and dev environments, but a live service should keep at least two pods running so one node failure does not take it down.
What is the difference between HPA and the cluster autoscaler?
HPA changes the number of pods in a workload. The cluster autoscaler changes the number of nodes in the cluster. When HPA adds pods and the nodes run out of room, the cluster autoscaler adds a node so the new pods have somewhere to run.
Do I need Prometheus for HPA?
No, not for CPU and memory. Those work with the metrics server, which most clusters include. Prometheus, or another metrics source with an adapter, is needed only for custom metrics such as requests per second or queue length.
Why is my HPA not scaling?
The usual causes are missing resource requests, a metrics server that is not running, a target that is already met, or a maximum replica count that has been reached. Check the HPA events first: they say plainly whether the metric is missing, the target is met, or the maximum is the limit.