Traffic changes; autoscaling keeps latency stable without paying for peak capacity all day.
Three layers of autoscaling work together:
- HorizontalPodAutoscaler (
autoscaling/v2) changes a Deployment's or StatefulSet'sreplicasto keep a metric near a target. CPU and memory utilisation are measured relative to requests, from metrics-server. Custom or external metrics (queue length, requests per second) come through an adapter such as Prometheus Adapter or KEDA, which can also scale to zero. - VerticalPodAutoscaler (an add-on) recommends or applies better requests based on observed usage. Don't let VPA and HPA both act on CPU for the same workload.
- Node autoscaling: the Cluster Autoscaler or Karpenter adds nodes when pods are Pending for lack of room and removes underused ones.
HPA details that matter:
- It needs resource requests; without them utilisation can't be computed.
minReplicas/maxReplicasbound it.behaviorcontrols scale speed: scale-down has a 5-minute stabilisation window by default to avoid flapping.- Remove
replicasfrom your Deployment manifest (or the next apply fights the HPA).
The formula is roughly desired = ceil(current * currentMetric / target). Scaling is reactive and new pods need time to start, so keep some headroom and fast startup.
Example
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: {name: api}
spec:
scaleTargetRef: {apiVersion: apps/v1, kind: Deployment, name: api}
minReplicas: 3
maxReplicas: 30
metrics:
- type: Resource
resource:
name: cpu
target: {type: Utilization, averageUtilization: 70} # % of requests
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies: [{type: Percent, value: 50, periodSeconds: 60}]
# kubectl get hpa api
# NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS
# api Deployment/api cpu: 85%/70% 3 30 4Remember
- HPA scales replicas on metrics relative to requests
- KEDA/adapters for queue- or traffic-based scaling
- Node autoscalers add nodes when pods are Pending
Watch out for
- Keeping replicas: 3 in the Deployment manifest so every deploy resets what the HPA chose.