How does Kubernetes handle scaling with the Horizontal Pod Autoscaler?
Understand how the Kubernetes Horizontal Pod Autoscaler scales replicas on CPU and custom metrics, including the scaling formula and YAML examples.
Expected Interview Answer
The Horizontal Pod Autoscaler (HPA) automatically adjusts the number of Pod replicas in a workload (Deployment, ReplicaSet, or StatefulSet) up or down based on observed metrics such as CPU utilization, memory, or custom/external metrics, keeping them near a target you define.
A control loop in the controller-manager periodically (default every 15 seconds) queries the Metrics Server (or a custom metrics adapter) for the current metric values, then computes desiredReplicas = ceil(currentReplicas * currentMetric / targetMetric), clamped between minReplicas and maxReplicas. It applies stabilization windows and configurable scaling policies to avoid thrashing, and updates the workload's replica count. HPA scales the number of Pods (horizontal), unlike the Vertical Pod Autoscaler which resizes a Pod's CPU/memory requests.
- Automatically matches capacity to real-time demand
- Saves cost by scaling down during low traffic
- Improves reliability under load spikes
- Supports CPU, memory, custom, and external metrics
- Stabilization windows prevent rapid flapping
AI Mentor Explanation
The HPA is like a captain adjusting the number of fielders in the deep as the batter's scoring rate climbs. Watching runs per over as the live metric, the captain adds boundary riders when hitting intensifies and pulls them back into the circle when scoring slows, always keeping the field sized to the current threat rather than a fixed setup.
Step-by-Step Explanation
Step 1
Set resource requests
Pods must define CPU/memory requests so utilization percentages are meaningful.
Step 2
Deploy the Metrics Server
HPA reads current metrics from the Metrics Server or a custom metrics adapter.
Step 3
Create the HPA object
Define target metric, minReplicas, and maxReplicas against the workload.
Step 4
Controller polls metrics
Every ~15s the loop fetches current values and computes desiredReplicas.
Step 5
Apply scaling formula
desiredReplicas = ceil(currentReplicas * currentMetric / targetMetric), clamped to bounds.
Step 6
Stabilize and update
Stabilization windows and policies smooth the change before updating the replica count.
What Interviewer Expects
- The scaling formula and role of target metrics
- Dependency on resource requests and the Metrics Server
- Difference between HPA (horizontal) and VPA (vertical)
- min/max replica bounds and stabilization to prevent flapping
- Support for custom and external metrics beyond CPU
Common Mistakes
- Forgetting Pods need resource requests for CPU-based HPA to work
- Confusing HPA with the Cluster Autoscaler (which adds nodes)
- Assuming HPA only works on CPU when custom metrics are supported
- Not setting sensible min/max replicas causing over- or under-scaling
- Ignoring stabilization windows and getting scaling thrash
Best Answer (HR Friendly)
“The Horizontal Pod Autoscaler automatically adds or removes copies of an application depending on how busy it is, like opening more checkout lanes when a store gets crowded and closing them when it is quiet. This keeps the app responsive under load while saving resources during quiet periods.”
Code Example
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70kubectl autoscale deployment web --cpu-percent=70 --min=2 --max=10
kubectl get hpa web-hpa
kubectl describe hpa web-hpaFollow-up Questions
- How does HPA differ from the Vertical Pod Autoscaler?
- How does HPA interact with the Cluster Autoscaler?
- How would you scale on a custom or external metric like queue depth?
- What causes scaling flapping and how do stabilization windows help?
- Why must Pods define resource requests for CPU-based autoscaling?
MCQ Practice
1. What does the Horizontal Pod Autoscaler adjust?
HPA scales the replica count of a workload horizontally; resizing a Pod's resources is the VPA's job.
2. Which component supplies the metrics HPA reads by default?
The Metrics Server aggregates resource usage that the HPA control loop queries for CPU and memory.
3. What is required for CPU-utilization-based HPA to function?
Utilization is computed relative to CPU requests, so Pods must declare resource requests.
Flash Cards
HPA scaling formula? — desiredReplicas = ceil(currentReplicas * currentMetric / targetMetric), clamped to min/max.
HPA vs VPA? — HPA changes the number of Pods; VPA changes a Pod's CPU/memory requests.
What does HPA need to read metrics? — The Metrics Server (or a custom/external metrics adapter).
Why resource requests matter? — CPU utilization is measured relative to requests, so requests must be set.
Continue Learning
Related Interview Questions
What is Kubernetes and what problem does container orchestration solve?
easy
How does horizontal scaling differ from vertical scaling in Kubernetes?
medium
What is the difference between a Deployment, a ReplicaSet, and a Pod?
medium
What is a Kubernetes Service and what are the different service types?
medium