How HPA Computes Replicas
How HPA Computes Replicas
The HPA controller runs inside kube-controller-manager and polls metrics every --horizontal-pod-autoscaler-sync-period (default 15s). For CPU utilization it computes:
desiredReplicas = ceil(currentReplicas × currentUtilization / targetUtilization)
A ±10% tolerance band prevents flapping: targeting 50% with 4 pods keeps the count stable while measured utilization sits between 45% and 55%. Scaling up takes effect on the next sync. Scaling down waits for --horizontal-pod-autoscaler-downscale-stabilization-window (default 5m) so a brief traffic dip never shrinks capacity you are about to need again.
Creating and Configuring HPAs
Creating and Configuring HPAs
The imperative generator still writes a real autoscaling/v2 object:
kubectl autoscale deployment api --cpu-percent=50 --min=2 --max=10
kubectl get hpa api -w
The declarative form combines metrics and controls the ramp:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
spec:
scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: api }
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target: { type: Utilization, averageUtilization: 60 }
The optional behavior field sets scaleUp/scaleDown policies (pods or percent per second) and stabilization windows per direction.
Pitfalls and Interview Angles
Pitfalls and Interview Angles
Unknown replicas means metrics are missing. Confirm kubectl top pods works — that requires metrics-server, which feeds the resource metrics API the HPA reads.
Utilization is relative to requests. With no CPU request declared, utilization is undefined and CPU scaling cannot work. Set requests first; memory targets behave the same way.
The controller also ignores changes inside the tolerance band, and scale-down is delayed five minutes by design.
Interview angle: derive the ceil formula, explain the tolerance band, name where custom metrics come from (prometheus-adapter serving custom.metrics.k8s.io), and say why HPA and VPA must not both manage CPU.
Quiz
1. A deployment has 4 pods at 80% CPU with a 50% target. What is the computed replica count?
2. kubectl top pods fails, so the HPA shows replicas as unknown. What is missing?
3. Why might utilization-based HPA do nothing on a workload with no CPU requests set?
Flashcards
Question
HPA replica formula
Click to reveal answer
Answer
desired = ceil(currentReplicas × currentUtilization / targetUtilization), with a ±10% tolerance band.
Question
Downscale stabilization window
Click to reveal answer
Answer
kube-controller-manager --horizontal-pod-autoscaler-downscale-stabilization-window, default 5m, delays scale-down.
Question
metrics-server
Click to reveal answer
Answer
Aggregates kubelet stats into the resource metrics API; required for kubectl top and CPU/memory HPAs.
Question
custom.metrics.k8s.io
Click to reveal answer
Answer
API served by prometheus-adapter so HPAs can target metrics like requests-per-second per pod.
Revision Notes
Key Takeaways
- 1. HPA polls every 15s and applies ceil(current × currentUtil / target) with a 10% tolerance
- 2. CPU/memory scaling requires metrics-server and declared requests on every container
- 3. Scale-up is immediate; scale-down waits out the five-minute stabilization window
Interview Tips
- • Derive the replica formula on the board, then mention the tolerance band that prevents flapping
- • Trace the metrics path: kubelet stats → metrics-server → resource metrics API → HPA controller
Cheat Sheet
HPA: ceil(cur×curUtil/target), ±10% tol, sync 15s, downscale stall 5m; needs requests+metrics-server; custom metrics via custom.metrics.k8s.io; unknown = missing metrics.
Continue Learning
Before You Start
- No prerequisites listed.