☁️Cloud

HPA

Horizontal Pod Autoscaler

An autoscaler in Kubernetes that automatically adjusts the number of Pods based on CPU/memory usage.

📖 Detailed Explanation

HPA (Horizontal Pod Autoscaler) is a core autoscaling component of Kubernetes that automatically adjusts the number of Pod replicas based on a workload's CPU/memory usage or custom metrics. Through horizontal scaling (Scale Out/In), it responds to traffic increases and saves resources during idle times.

By default, HPA checks metrics every 15 seconds (--horizontal-pod-autoscaler-sync-period) and determines the required number of replicas by calculating the difference between the target utilization (e.g., CPU 50%) and the current utilization. To prevent abrupt scaling fluctuations, a 5-minute stabilization window is applied by default for scale-down.

Since Kubernetes 1.23, HPA v2 has been stable, so scaling can be based not only on CPU/memory but also on external metrics such as Prometheus, and even on metrics from other objects. For example, scaling based on Kafka queue length or the number of HTTP requests is possible.

In practice, HPA is essential for stateless workloads such as web servers and API servers. However, since HPA alone cannot scale out when node resources are insufficient, it must be used together with Cluster Autoscaler (CA) or Karpenter to achieve complete automatic scaling.

💻 Code Example

# 간단한 HPA 생성 (CPU 50% 기준)
kubectl autoscale deployment web-app \
  --cpu-percent=50 \
  --min=2 \
  --max=10

# HPA 상태 확인
kubectl get hpa web-app
# NAME      REFERENCE            TARGETS   MINPODS   MAXPODS   REPLICAS
# web-app   Deployment/web-app   35%/50%   2         10        3

# HPA 상세 정보 확인
kubectl describe hpa web-app

# HPA 이벤트 및 스케일링 이력 확인
kubectl get hpa web-app -o yaml

# 실시간 모니터링
kubectl get hpa -w

# HPA 삭제
kubectl delete hpa web-app

🗣️ Say It Like This in Practice

💬 In a Meeting
"We're running HPA with CPU 50% and memory 70% thresholds, with min 3 and max 20. During peak times it automatically scales out to 15, and at night it drops to 3, which cut costs by 40%. We extended the scale-down stabilization window to 10 minutes to prevent unnecessary scaling oscillation."
💬 In an Interview
"We combined HPA and Cluster Autoscaler to implement fully automatic scaling. When HPA increases Pods, CA adds nodes, and when Pods decrease, CA removes nodes. We also integrated Prometheus Adapter to apply RPS-based scaling, and used the behavior setting to make scale-up aggressive and scale-down conservative."
💬 In Troubleshooting
"HPA isn't scaling out, and the metric shows as 'unknown'. The Deployment is missing resources.requests, so CPU utilization can't be calculated. Add requests and also check that metrics-server is healthy."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
resources.requests not set

HPA calculates utilization as the ratio of (current usage / requests). Without requests, CPU utilization cannot be determined, so HPA does not work. Be sure to set requests in the Deployment.

❌
Setting the scaling threshold too low

A low threshold such as CPU 30% triggers scaling even on minor load fluctuations, causing Pods to be created and deleted frequently (thrashing). Generally, 50-70% is appropriate, and adjust the stabilization window with behavior.

✅
The right approach

Use HPA behavior to set scale-up to be fast (stabilizationWindowSeconds: 0) and scale-down to be slow (300 seconds or more). By using it together with VPA (Vertical Pod Autoscaler), you can also automatically adjust appropriate requests values.

🔗 Related Terms

📚 Learn More