Sub topic 6
How the HPA decides the replica count
This chapter assumes you can apply manifests with kubectl, you have created a basic HorizontalPodAutoscaler (HPA), and your Pods declare CPU requests. Those requests matter here because the HPA measures utilization against them.
The core formula
The HPA control loop runs about every 15 seconds. Each pass it reads the current metric, compares it with your target, and computes a desired replica count:
desiredReplicas = ceil[ currentReplicas * ( currentMetricValue / desiredMetricValue ) ]
Example: 4 replicas, current average CPU utilization 90%, target 60%
desiredReplicas = ceil[ 4 * (90 / 60) ] = ceil[ 6.0 ] = 6The ratio currentMetricValue / desiredMetricValue is the scaling signal. A ratio above 1 means load is over target, so replicas go up. A ratio below 1 means replicas go down. The ceil rounds up, so the HPA never picks a fractional or lower-than-needed count.
What "utilization" means
With type: Utilization, the HPA compares average usage to the Pods' CPU requests, not to their limits or to the node's capacity. The reference manifest keeps average CPU at about 70% of requests:
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: 70If each Pod requests 200m and uses 140m, utilization is 70%. Without a CPU request, the HPA cannot compute a percentage and reports the metric as unknown.
Tolerance
The HPA skips scaling when the ratio is within a tolerance of 1.0. The default is 0.1, so ratios between 0.9 and 1.1 change nothing. This stops constant small adjustments.
| Current | Target | Ratio | Result with 4 replicas |
|---|---|---|---|
| 90% | 60% | 1.50 | Scale to 6 |
| 72% | 70% | 1.03 | No change (within tolerance) |
| 30% | 60% | 0.50 | Scale to 2 |
| 65% | 70% | 0.93 | No change (within tolerance) |
Bounds and unready Pods
After the formula, the HPA clamps the result to minReplicas and maxReplicas. If the formula gives 14 and maxReplicas is 10, the target is 10.
The HPA also adjusts its input for Pods whose data is unreliable:
- Pods that are still starting up or have no metrics yet are treated conservatively. When scaling up, a Pod with missing metrics is assumed to use 0% of the target. When scaling down, it is assumed to use 100%. This damps overreaction in both directions.
- Pods being deleted, and Pods that have failed, are ignored.
When several metrics are listed, the HPA computes a desired count for each one and uses the largest. One busy metric is enough to scale out.
The decision flow
The behavior step is covered in the next chapter. It can slow the change you computed here.
Checking the numbers yourself
Compare your hand calculation with what the HPA reports:
kubectl top pods
kubectl get hpa web
kubectl describe hpa webkubectl get hpa shows a line such as cpu: 90%/70%, which is current over target. Plug those values and the current replica count into the formula to predict the next step.
Exercise
A Deployment has 5 replicas and an HPA targeting 50% average CPU utilization. Current average utilization is 80%, and maxReplicas is 7. What replica count does the HPA choose, and why?
Show answer
The formula gives ceil[5 * (80 / 50)] = ceil[8.0] = 8. That exceeds maxReplicas, so the HPA clamps the result to 7.
Exercise
With 10 replicas and a 60% target, current average utilization is 63%. Does the HPA scale, assuming the default tolerance?
Show answer
No. The ratio is 63 / 60 = 1.05, which is within 0.1 of 1.0, so the replica count stays at 10.