In brief
Kubernetes autoscaling works when pods have resource requests set, the HPA scales on a metric that rises with traffic, and a load test has confirmed how fast replicas appear. The HorizontalPodAutoscaler computes CPU utilization relative to requests, so without them it does not scale at all. The replica count must also fit the rest of the system (the database, connection pools and external API limits), because new pods move the bottleneck elsewhere rather than removing it.
How the HorizontalPodAutoscaler works
The HorizontalPodAutoscaler (HPA) is a controller that changes the number of replicas of a workload (a Deployment or StatefulSet) so that a chosen metric approaches its target value. The Kubernetes documentation gives the algorithm:
`desiredReplicas = ceil(currentReplicas × currentMetricValue / desiredMetricValue)`
Example: 4 replicas, average CPU utilization 90%, target 60%. The HPA computes ceil(4 × 90 / 60) = 6 replicas. By default the controller checks metrics every 15 seconds and ignores changes within the tolerance, 10% by default, so it does not react to small fluctuations.
Two default behaviors matter. Scaling down uses a 300-second stabilization window: the HPA takes the highest recommendation from the last five minutes, so replicas disappear with a delay after a peak. Scaling up has no stabilization window, so the controller reacts immediately. You can change both in the `behavior` section.
Resource requests and limits: the foundation of scaling
CPU utilization in the HPA is a percentage of the resource request, not of the limit and not of the node's cores. The documentation states that if any container in a pod has no request set, utilization is undefined and the autoscaler takes no action for that metric. This is the most common reason behind "the HPA exists, but does nothing".
Requests and limits work differently:
| Setting | What it does | Effect of a mistake |
|---|---|---|
| requests.cpu | guarantees a CPU share, the basis for scheduling and the HPA | too low: the HPA scales too early and nodes are overcommitted |
| limits.cpu | upper bound enforced by throttling | too low: response times rise even though the node has idle CPU |
| requests.memory | memory reservation when scheduling pods | too low: pods land on nodes that later run out of memory |
| limits.memory | when exceeded, the kernel may kill the container (OOM kill) | too low: restarts under load with no trace in application logs |
For JVM and .NET applications the memory limit must fit the heap, native memory and threads. A container killed for exceeding its limit looks like a memory leak, which we cover in the article on memory leaks in the JVM and .NET.
Which metric to choose
CPU is a good metric for services that compute: pricing, validation, encryption. It works poorly where a pod mostly waits for a database or an external API, because CPU stays idle while response times rise. The HPA in the autoscaling/v2 API also supports custom and external metrics:
- requests per second per pod, from application or gateway metrics,
- queue length or consumer lag, for services that process messages, as covered in the article on queues and asynchronous processing,
- the number of active connections or sessions.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: activations
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: activations
minReplicas: 4
maxReplicas: 12
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
behavior:
scaleDown:
stabilizationWindowSeconds: 300A 60% target leaves headroom for the time new pods take to start. The longer the application takes to start, the lower the target or the higher the minimum replica count should be.
Limits of autoscaling: nodes, the database and startup time
The HPA adds pods, not nodes. When the cluster runs out of room, new pods wait in the Pending state, and a separate node autoscaler decides on new nodes, sizing them to the resource requests of pending pods. A node usually takes longer to start than a pod, so for sudden peaks a minimum replica count or spare cluster capacity often matters more than the HPA itself.
The second limit sits outside the cluster. Each new replica opens its own database connection pool, so maxReplicas multiplied by pool size must fit within the database limit; we show the calculation in the article on database connection pools. The same applies to external API limits and per-instance licenses.
The third limit is startup time. Between the HPA decision and the first request served come the image pull, process start, warm-up and a passing readiness probe. A pod without a correct probe receives traffic before it is ready and distorts the measurement.
How to verify autoscaling with a load test
Autoscaling needs testing like any other system feature:
- Define the expectation: at what traffic there should be how many replicas, and how quickly they should appear.
- Run a test that ramps traffic from the minimum to the forecast peak and record the replica count, the time from HPA decision to pod readiness, and response time percentiles.
- Add a spike test: a traffic jump within seconds, as after a push notification or the start of a sale. Check that the minimum replica count survives the scaling delay.
- Push to maxReplicas and see what fails first: the database, the connection pool, the nodes or an external API.
- After traffic drops, check that replicas go away according to the stabilization window and that terminating pods finish in-flight requests.
- Compare the cost: how many replicas the system held throughout the test. The article on FinOps expands on costs.
Choosing test types is covered in the article on load, stress and endurance testing, and when Kubernetes makes sense at all in the article on Kubernetes in the enterprise. An example of a launch with a sudden traffic peak is the project on performance testing a service activation system.
Questions and answers
Why does the HPA not scale despite high load?
Most often because containers have no CPU requests set, so utilization is undefined. Other causes are a broken metrics server, maxReplicas already reached, no room in the cluster (pods stuck in Pending), or a metric that does not grow with traffic, such as CPU in a service that waits on the database.
Should I set CPU limits for containers?
It depends on the cluster. A limit protects neighboring pods from one greedy container, but it is enforced by throttling, which raises response times even when the node has spare capacity. Many teams set accurate requests and a cautious CPU limit or none at all, but always a memory limit. Confirm the choice with a load test.
What should the minimum replica count be?
Enough to handle typical traffic and the first wave of a peak before new replicas arrive, plus headroom for losing one node. For critical services that is usually at least two or three replicas across different zones. A lower minimum saves money but slows the response to a sudden traffic spike.
What is the difference between HPA and VPA?
The HPA changes the number of replicas, while the VerticalPodAutoscaler (VPA) changes the resource requests of individual pods based on usage. VPA is not part of the core Kubernetes API and is installed separately. It helps choose requests, especially early in a project. Two autoscalers reacting to the same CPU signal can interfere, so VPA often runs in Off mode, which only provides recommendations.