Chapter 2 · Practice 4 of 5
Scale replicas with a HorizontalPodAutoscaler, based on requests
Why it matters
A HorizontalPodAutoscaler (HPA) adds and removes replicas to keep a metric near a target. For CPU and memory, the target is a percentage of the request, so an HPA on a Deployment with no requests cannot compute utilisation and will not scale.
Practical rules:
- Set requests first, then pick a utilisation target that leaves headroom for spikes while new Pods start.
- Set
minReplicasto at least 2 for anything user-facing so one Pod restart is not an outage. - Set
maxReplicasfrom what your database and downstream services can take, not from what the cluster could fit. - Do not set
spec.replicasin the Deployment you apply from Git when an HPA owns the count, or every deploy resets it.
For queue workers, scale on queue depth through a custom or external metric rather than CPU.
To check, run kubectl get hpa and confirm the TARGETS column shows real numbers. <unknown> usually means missing requests or a missing metrics server.
Example
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: checkout
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: checkout
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70