Autoscaling: Pods and nodes
Scale on demand at two levels: the Horizontal Pod Autoscaler adds Pods under load, and the Cluster Autoscaler adds nodes when Pods have nowhere to run. Both shown live on EKS, plus where VPA fits.
By the end of this lesson you'll be able to
0 of 4 completeVerified lab: the HPA scaling shop-web from 3 to 8 Pods under load, and the Cluster Autoscaler adding a
third node and removing it, on the course's EKS cluster. The output shown is real.
Code walkthrough: the Vertical Pod Autoscaler and Karpenter (explained, not run).
Cloud lab: adding nodes needs a cloud cluster; the HPA also works on kind with metrics-server.
Traffic isn't flat. A shop is quiet at 4 a.m. and busy at 8 p.m. Running enough for the peak all day wastes money; running for the average falls over at the peak. Kubernetes scales at two levels: more Pods when the app is busy, and more nodes when the Pods have nowhere to run.
Horizontal Pod Autoscaler
The HPA watches a metric and changes a Deployment's replica count:
# 10-hpa.yaml apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: shop-web spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: shop-web minReplicas: 2 maxReplicas: 8 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 50 # percent of each Pod's CPU request
How it decides, every 15 seconds:
- It reads the Pods' CPU use from metrics-server.
- It divides by the CPU request (100m for shop-web) to get a utilisation percentage.
- It computes
desired = current replicas x (current utilisation / target), rounds up, and stays withinminReplicasandmaxReplicas.
A load-generator Pod then called /burn (200 ms of CPU per request) in a loop. The real readings, every 30
seconds:
$ kubectl get hpa shop-web NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE shop-web Deployment/shop-web cpu: 1%/50% 2 8 3 39s $ kubectl get hpa shop-web # after 30s of load shop-web Deployment/shop-web cpu: 334%/50% 2 8 3 79s $ kubectl get hpa shop-web # after 60s of load shop-web Deployment/shop-web cpu: 167%/50% 2 8 8 112s $ kubectl get hpa shop-web # after 120s of load shop-web Deployment/shop-web cpu: 123%/50% 2 8 8 2m59s $ kubectl get hpa shop-web # after 240s of load shop-web Deployment/shop-web cpu: 110%/50% 2 8 8 5m14s $ kubectl describe hpa shop-web ... Events: Normal SuccessfulRescale 4m1s horizontal-pod-autoscaler New size: 6; reason: cpu resource utilization (percentage of request) above target Normal SuccessfulRescale 3m46s horizontal-pod-autoscaler New size: 8; reason: cpu resource utilization (percentage of request) above target
kubectl get hpa shop-webkubectltalk to a Kubernetes clustergetlist objects
kubectl describe hpa shop-webdescribeshow details and events
At 334% the HPA wanted far more than 8 Pods, scaled to 6, then to 8 (the maximum) 15 seconds later. CPU stayed
above target because 8 was the cap: in real life that's the signal to raise maxReplicas or make the app
cheaper per request. When load stops, the HPA waits 5 minutes (its scale-down stabilisation window) before
removing Pods, so a short dip doesn't cause flapping.
Two requirements trip people up: the Pods must have a CPU request (otherwise the target shows <unknown>),
and metrics-server must be running. The HPA can also scale on memory or on custom metrics, such as requests
per second from Prometheus.
Cluster Autoscaler
More Pods need more room. When no node has enough unreserved CPU or memory, new Pods stay Pending. The Cluster Autoscaler watches for exactly that and grows the node group; it also removes nodes it can empty.
On EKS it runs in the cluster, finds node groups by their tags, and changes the Auto Scaling group's desired count. It needs IAM permissions, given here through Pod Identity, and installs with Helm:
$ helm install cluster-autoscaler autoscaler/cluster-autoscaler -n kube-system \ --set autoDiscovery.clusterName=academy --set awsRegion=ap-south-1 \ --set rbac.serviceAccount.name=cluster-autoscaler --wait NAME: cluster-autoscaler NAMESPACE: kube-system STATUS: deployed REVISION: 1
(The first attempt failed with a timeout downloading the chart from GitHub; running the same command again
worked. Network errors during helm install are worth one retry before digging deeper.)
Then a Deployment of 6 Pods, each requesting 600m of CPU, on two nodes with 1930m allocatable each:
$ kubectl get pods -l app=big ... 4 Running, 2 Pending ... Warning FailedScheduling ... 0/2 nodes are available: 2 Insufficient cpu. ... $ kubectl logs -n kube-system deployment/cluster-autoscaler-aws-cluster-autoscaler | grep -iE "scale-up plan|setting group" I1109 06:40:04.897347 1 orchestrator.go:1095] "Estimated nodes needed" iterationId="a538b217-65" nodeCount=1 nodeGroupId="eks-default-4f8ac3296b79a6f028385674b6-..." I1109 06:40:04.897367 1 orchestrator.go:168] "Final scale-up plan" ... I1109 06:40:04.897397 1 executor.go:148] "Scale-up: setting group size" ... $ kubectl get nodes -L topology.kubernetes.io/zone NAME STATUS ROLES AGE VERSION ZONE ip-10-42-0-115.ap-south-1.compute.internal Ready <none> 51m v1.37.1-eks-6e7947a ap-south-1a ip-10-42-14-248.ap-south-1.compute.internal Ready <none> 28s v1.37.1-eks-6e7947a ap-south-1a ip-10-42-28-149.ap-south-1.compute.internal Ready <none> 51m v1.37.1-eks-6e7947a ap-south-1b $ kubectl get pods -l app=big -o wide NAME READY STATUS RESTARTS AGE IP NODE big-6688b8d444-gwpjr 1/1 Running 0 71s 10.42.1.185 ip-10-42-14-248.ap-south-1.compute.internal big-6688b8d444-qqngk 1/1 Running 0 71s 10.42.2.24 ip-10-42-14-248.ap-south-1.compute.internal ... $ aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[?contains(AutoScalingGroupName,'eks-default')].{min:MinSize,desired:DesiredCapacity,max:MaxSize}" --output table | desired | max | min | | 3 | 3 | 2 |
kubectl get pods -l app=bigkubectltalk to a Kubernetes clustergetlist objects-lonly objects with this label
kubectl logs -n kube-system deployment/cluster-autoscaler-aws-cluster-autoscaler | grep -iE "scale-up plan|setting group"logsshow a Pod’s output-nin this namespace|pipe: send output to the next commandgrepfind lines that match a pattern
kubectl get pods -l app=big -o wide-ooutput format
aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[?contains(AutoScalingGroupName,'eks-default')].{min:MinSize,desired:DesiredCapacity,max:MaxSize}" --output tableawsthe AWS command line--querypick fields from the JSON response--outputoutput format
(Columns trimmed.) The autoscaler estimated one node was needed, set the Auto Scaling group's desired size from 2 to 3, and a new node, 28 seconds old, took the two Pending Pods. A node takes a couple of minutes to boot and join, so Pod autoscaling reacts in seconds while node autoscaling reacts in minutes. After the big Deployment was deleted, the autoscaler removed the extra node again once it had been underused for its scale-down delay.
It looks at requests, not real usage: an idle node full of requested CPU is "full". That's another reason to set requests honestly. Karpenter is the main alternative on AWS: instead of resizing fixed node groups, it launches whichever instance type fits the Pending Pods, usually faster and cheaper.
Vertical Pod Autoscaler
The VPA adjusts a Pod's requests instead of its count: it watches real usage and recommends, or applies, better CPU and memory requests. It's an add-on (not installed in the lab), and in "recommendation" mode it's a safe way to size requests from real data. Don't let the VPA and an HPA both act on CPU for the same Deployment, since they'd fight.
Its updateMode decides how far it goes: Off only recommends, Initial sets requests on new Pods, and
Recreate also evicts running Pods so their replacements get the new requests.
Watch both autoscalers react
Apply the HPA to shop-web and start the load Pod. Record kubectl get hpa every 30 seconds for 3 minutes and the SuccessfulRescale events. On EKS with the Cluster Autoscaler, create a Deployment that requests more CPU than your nodes have free and record the node count until the Pending Pods run.
~/autoscaling.txtReveal one safe solution
$ kubectl apply -f 10-hpa.yaml -f 10-load.yaml $ for i in 1 2 3 4 5 6; do sleep 30; kubectl get hpa shop-web --no-headers; done > ~/autoscaling.txt $ kubectl describe hpa shop-web | grep SuccessfulRescale >> ~/autoscaling.txt $ kubectl delete pod load $ kubectl apply -f 10-big.yaml && kubectl scale deployment big --replicas=6 $ for i in $(seq 1 12); do sleep 20; kubectl get nodes --no-headers | wc -l; done >> ~/autoscaling.txt $ kubectl delete deployment big
kubectl apply -f 10-hpa.yaml -f 10-load.yamlkubectltalk to a Kubernetes clusterapplycreate or update from a file-ffrom this file
for i in 1 2 3 4 5 6; do sleep 30; kubectl get hpa shop-web --no-headers; done > ~/autoscaling.txtforloop over a listdostart of what the loop runs each timedoneend of the loop>write output to a file, replacing it
kubectl describe hpa shop-web | grep SuccessfulRescale >> ~/autoscaling.txtdescribeshow details and events|pipe: send output to the next commandgrepfind lines that match a pattern>>append output to a file
kubectl delete pod loaddeletedelete an object
kubectl apply -f 10-big.yaml && kubectl scale deployment big --replicas=6&&run the next command only if this one workedscalechange the number of replicas--replicas=6how many copies
for i in $(seq 1 12); do sleep 20; kubectl get nodes --no-headers | wc -l; done >> ~/autoscaling.txt$(seq 1 12);the output of another commandwccount lines, words and bytes
The HPA should climb within a minute. If TARGETS shows <unknown>, check the Pods have a CPU request and
metrics-server is running.
Try it yourself
0 of 5 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
The HPA target is 50% CPU and the Pods report 334%. What does the HPA do?
Official reading for this lesson
Or get every quiz answer right and it completes itself.
Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.