Horizontal Pod Autoscaler
The HPA scales the number of pods in a Deployment up or down based on CPU, memory, or custom metrics.
While the Cluster Autoscaler and Karpenter scale nodes, the Horizontal Pod Autoscaler (HPA) scales pods. It adjusts a Deployment's replica count to keep a metric — usually CPU utilization — near a target.
How it works
- The HPA reads pod metrics from the Metrics Server (install it as an add-on).
- If average CPU exceeds the target, it adds replicas; if it drops, it removes them.
- Node autoscaling then supplies capacity for the extra pods if needed.
HPA and Cluster Autoscaler/Karpenter work as a team: pods scale first, nodes follow.
Example
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60When to use it
- An API team deploys an HPA on their user-facing service so pod count scales from 3 to 50 automatically during a marketing campaign traffic spike.
- A data processing team configures an HPA on a consumer Deployment targeting 70% CPU, keeping pods from being overwhelmed by message queue bursts.
- A cost team uses HPA with memory metrics to scale a JVM service based on heap usage, right-sizing replicas without setting CPU thresholds that JVMs saturate unevenly.
More examples
Create an HPA for a Deployment
Creates an HPA that keeps CPU utilization around 60% by scaling the api Deployment between 2 and 20 replicas.
kubectl autoscale deployment api \
--min=2 \
--max=20 \
--cpu-percent=60
kubectl get hpa apiHPA manifest with metrics
A declarative HPA manifest using autoscaling/v2 that targets average CPU utilization across all pods in the Deployment.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60Install Metrics Server for HPA
Installs the Metrics Server add-on that the HPA requires to collect CPU and memory usage data from kubelets.
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
kubectl top nodes
kubectl top pods
Discussion