How does the Horizontal Pod Autoscaler (HPA) scale pods based on CPU/RAM metrics?
❓ Interview Prompt: Explain HPA control loop, Metrics Server, target utilization, scale-up/scale-down stabilization, and CPU-based autoscaling configuration.
The Horizontal Pod Autoscaler (HPA) automatically adjusts the number of Pod replicas in a Deployment (or StatefulSet/ReplicaSet) based on observed metrics.
Control Loop (every 15 seconds):
desiredReplicas = ceil(currentReplicas × (currentMetric / targetMetric))
spec.replicas if the desired count differs.Scale-Up vs Scale-Down:
Prerequisite: Metrics Server
HPA requires the Kubernetes Metrics Server to be installed — it aggregates CPU/RAM metrics from Kubelet on each node.
Resource Requests are Mandatory:
HPA calculates CPU utilization as: currentCPU / request.cpu. Without resource requests defined, HPA cannot calculate utilization percentages.
Code Implementation Example
# HPA: scale Deployment based on CPU utilization
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
minReplicas: 2 # never scale below 2 (availability floor)
maxReplicas: 20 # never exceed 20 replicas (cost ceiling)
metrics:
# Scale when average CPU > 70% of requested CPU
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
# Also scale when average memory > 80%
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
behavior:
scaleDown:
stabilizationWindowSeconds: 300 # 5min cooldown prevents flappingHPA requires Metrics Server + resource requests. Scale on CPU utilization target (not usage). Use scale-down stabilization window to prevent replica count flapping.