Sub topic 12
How HPA and VPA Depend on Requests
This chapter assumes you know how requests and limits work, how the scheduler uses requests, and how to read kubectl top output. It covers how the two main autoscalers use the request value as their baseline.
Why autoscalers care about requests
Both autoscalers need a reference for "how much of its resources is this pod using?" Kubernetes uses the container's request as that reference. The Horizontal Pod Autoscaler (HPA) adds or removes pod replicas. The Vertical Pod Autoscaler (VPA) changes the request values of the pods themselves.
If requests are wrong, both autoscalers make wrong decisions. This is a common source of surprising scaling behavior.
HPA: utilization is a percentage of the request
When you configure HPA with a target of averageUtilization, the percentage is measured against the request, not the limit and not the node's capacity. For a pod with a CPU request of 250m that uses 200m, the utilization is 80%.
The HPA controller uses this formula:
desiredReplicas = ceil(currentReplicas * currentUtilization / targetUtilization)Here is an HPA that targets 70% CPU utilization:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70With 4 replicas averaging 140% of their request and a 70% target, the result is ceil(4 * 140 / 70) = 8 replicas.
Two consequences follow:
- A pod with no CPU request has no utilization value. HPA reports that it cannot compute the metric, and it does not scale on that metric. The same applies to memory.
- A request that is too high makes utilization look low, so HPA scales out late. A request that is too low makes utilization look high, so HPA scales out too early and wastes capacity.
For a pod with several containers, HPA sums the requests and usage across containers. A sidecar with a large request and little usage lowers the pod's utilization figure. The ContainerResource metric type lets you target one container instead.
VPA: it recommends and changes requests
VPA watches historical usage and computes a recommended request for each container. It runs as add-on components, so it is not installed in a cluster by default. Its updateMode field controls what it does with the recommendation:
| updateMode | Behavior |
|---|---|
| `Off` | Only publishes recommendations in the VPA object's status. |
| `Initial` | Applies recommendations when pods are created, never to running pods. |
| `Recreate` | Evicts running pods so they restart with new requests. |
| `Auto` | Currently behaves like `Recreate`. It may use in-place resizing when the cluster supports it. |
Starting with Off is the safest way to learn what VPA would do. You can read the result with kubectl describe vpa web. The recommendation includes lower bound, target, and upper bound values.
Because changing a request normally means restarting the pod, Recreate mode can disrupt a workload. Use a PodDisruptionBudget and run more than one replica.
Combining HPA and VPA
Do not let both autoscalers act on the same resource metric for the same workload. HPA scales on utilization relative to the request. If VPA changes the request, the utilization changes too, and the two controllers can fight each other.
Safe patterns are:
- Use HPA on CPU and memory, and run VPA in
Offmode only to get recommendations. - Use HPA on a custom or external metric such as requests per second, and let VPA manage CPU and memory requests.
Common errors
- HPA shows
<unknown>for its target. The pod has no request for that resource, or metrics-server is not working. - HPA never scales out even though usage is high. The request may be far above real usage, so utilization stays under the target.
- Replicas grow without bound under load. Check that
maxReplicasis set sensibly and that the cluster has capacity, because new pods whose requests do not fit will stayPending.
Exercise
A deployment's pods each request 500m CPU. There are 3 replicas, each using 350m on average, and the HPA target is 50% utilization. How many replicas will HPA aim for?
Show answer
Utilization is 350m / 500m = 70%. The formula gives ceil(3 * 70 / 50) = ceil(4.2) = 5 replicas.
Exercise
Why is it risky to run HPA on CPU utilization and VPA in Auto mode on the same deployment?
Show answer
VPA changes the CPU request, which changes the utilization percentage that HPA computes. The two controllers can keep reacting to each other's changes, which causes unstable scaling.