What Is Horizontal Pod Autoscaling?
Learn what Horizontal Pod Autoscaling is in Kubernetes, how it scales replicas by metrics, and how to answer it in an interview.
Expected Interview Answer
Horizontal Pod Autoscaling (HPA) is a Kubernetes controller that automatically increases or decreases the number of pod replicas in a deployment based on observed metrics such as CPU utilization, memory usage, or custom application metrics.
The HPA controller polls the metrics API at a fixed interval, compares the observed value against the target value defined in the HPA spec, and computes a desired replica count using a ratio-based formula. It then updates the replica count on the target Deployment or ReplicaSet, and the standard Kubernetes scheduler places or removes pods to match. HPA respects configured minReplicas and maxReplicas bounds and applies stabilization windows and scaling policies to avoid rapid flapping. It differs from Vertical Pod Autoscaling, which resizes the resource requests of existing pods rather than changing their count.
- Automatically matches capacity to real-time load
- Reduces cost by scaling down during idle periods
- Improves availability during traffic spikes
- Works with custom metrics beyond CPU and memory
AI Mentor Explanation
Horizontal Pod Autoscaling is like a franchise cricket team calling up extra net bowlers from the reserves whenever the main squad is under heavy training load before a big match. The team management watches how hard the bowlers are working and adds more the moment fatigue metrics rise past a threshold. Once training intensity drops back down, the extra bowlers are released back to the reserve pool so the squad stays lean. The head coach also sets a floor and ceiling on how many bowlers can ever be called up, so the squad never shrinks below match-readiness or balloons past the budget.
Step-by-Step Explanation
Step 1
Define the HPA resource
Set target CPU/memory or custom metric utilization along with minReplicas and maxReplicas on a Deployment.
Step 2
Metrics collection
The metrics-server or a custom metrics adapter reports current resource usage per pod to the HPA controller.
Step 3
Compute desired replicas
The controller calculates desiredReplicas = ceil(currentReplicas * (currentMetric / targetMetric)) on each sync interval.
Step 4
Apply and stabilize
The controller patches the replica count, applying stabilization windows to prevent rapid scale up/down flapping.
What Interviewer Expects
- Understanding that HPA changes replica count, not pod size
- Knowledge of the metrics-server dependency
- Awareness of min/max replica bounds and stabilization windows
- Distinction from Vertical Pod Autoscaling
Common Mistakes
- Confusing HPA with Vertical Pod Autoscaling
- Forgetting that HPA requires resource requests to be set on pods
- Not accounting for scale-down stabilization delay
- Assuming HPA works without a metrics pipeline installed
Best Answer (HR Friendly)
โHorizontal Pod Autoscaling is a Kubernetes feature that automatically adds or removes copies of an application when traffic goes up or down, so the system stays responsive during busy periods and saves resources when things are quiet, without anyone manually adjusting capacity.โ
Code Example
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70Follow-up Questions
- How is Horizontal Pod Autoscaling different from Vertical Pod Autoscaling?
- What happens if the metrics-server is not installed?
- How would you scale based on a custom application metric like queue length?
- What is a stabilization window and why does it matter?
MCQ Practice
1. What does Horizontal Pod Autoscaling primarily change?
HPA scales the replica count of a target workload; it does not resize existing pods.
2. What component does HPA depend on to read CPU/memory usage?
HPA queries the metrics API, typically served by metrics-server or a custom/external metrics adapter.
3. Which Kubernetes feature resizes CPU/memory requests on existing pods instead of changing replica count?
Vertical Pod Autoscaling adjusts the resource requests/limits of pods rather than adding or removing replicas.
Flash Cards
What does HPA stand for? โ Horizontal Pod Autoscaling โ automatically adjusts pod replica count based on metrics.
What metric does HPA use by default? โ CPU utilization, though memory and custom/external metrics can also be configured.
What two bounds must every HPA define? โ minReplicas and maxReplicas.
HPA vs VPA โ what is the core difference? โ HPA changes the number of pods; VPA changes the resource sizing of each pod.