HPA
The Horizontal Pod Autoscaler (HPA) is a Kubernetes controller that automatically changes the number of pod replicas in a Deployment or StatefulSet based on observed metrics such as CPU, memory, or custom application metrics. It adds capacity during spikes and removes it when load drops.
How HPA decides
HPA reads metrics every 15 seconds by default and computes:
desiredReplicas = ceil(currentReplicas * (currentMetric / targetMetric))If average CPU is 85 percent against a 70 percent target with 5 replicas, it scales to ceil(5 * 85 / 70) = 7.
or custom adapter"] -->|"every 15s"| H["HPA controller"] H --> D{"Compare to target"} D -->|"above target"| U["Scale up"] D -->|"below target"| W["Scale down
after stabilization"] classDef src fill:#FFEDD5,color:#1E293B classDef ctl fill:#326CE5,color:#ffffff classDef decide fill:#475569,color:#ffffff classDef act fill:#D1FAE5,color:#1E293B class M src class H ctl class D decide class U,W act
Example
apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata: name: order-service-hpa namespace: productionspec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: order-service minReplicas: 3 maxReplicas: 20 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70 behavior: scaleUp: stabilizationWindowSeconds: 0 policies: - type: Pods value: 4 periodSeconds: 60 scaleDown: stabilizationWindowSeconds: 300 policies: - type: Percent value: 10 periodSeconds: 60HPA needs the Metrics Server for CPU and memory, and containers must have resource requests, because utilisation is measured relative to the request.
kubectl get hpa -n productionkubectl describe hpa order-service-hpa -n productionkubectl top pods -n productionTroubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Target shows unknown | Metrics Server missing or failing | Install and check metrics-server |
| Never scales | No CPU requests on pods | Add resource requests |
| Replicas flap | No scale-down stabilization | Set a stabilization window |
| Stuck at max | Ceiling reached | Raise maxReplicas or fix the bottleneck |
RememberDo not combine HPA CPU scaling with a manually managed replicas field in your manifest, because each apply can reset the replica count.
SecurityA high
maxReplicaslets an attack or runaway load burn through node capacity and cloud spend. Cap it and use namespace quotas.
Common MistakeScaling on CPU when the real bottleneck is a downstream database. More pods then increase pressure without improving throughput.
Frequently Asked Questions
Can HPA scale on metrics other than CPU?
Yes. Memory works through the Metrics Server, and custom or external metrics such as request rate or queue depth work through adapters like the Prometheus Adapter or KEDA.
Why is HPA not scaling although the app is overloaded?
Most often pods have no CPU requests, the Metrics Server is unavailable, or the bottleneck is not the metric HPA watches. kubectl describe hpa shows the reason in its conditions.
HPA vs Cluster Autoscaler?
HPA scales pods. Cluster Autoscaler or Karpenter scales nodes so the new pods have somewhere to run. Production setups use both.