Archive · Kubernetes

Autoscaling Pods with the HPA

Install metrics-server and scale a Deployment on CPU use.

1 min read

  • Kubernetes 1.22
  • metrics-server 0.5
  • autoscaling/v1

The Horizontal Pod Autoscaler changes replica counts based on metrics. It needs metrics-server.

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.5.0/components.yaml
kubectl top nodes
kubectl top pods

The pods must declare a CPU request, because utilisation is measured against it.

kubectl autoscale deployment web --cpu-percent=60 --min=2 --max=10
kubectl get hpa -w

Generate load to watch it work:

kubectl run load --rm -it --image=busybox:1.33 -- sh -c "while true; do wget -qO- http://web; done"

The HPA scales up quickly and down slowly (a 5 minute stabilisation window) to avoid flapping. It adjusts pods, not nodes: add the Cluster Autoscaler so new nodes appear when pods cannot be scheduled. Custom metrics need an adapter such as the Prometheus adapter.