Sub topic 9
Adding and removing nodes with the cluster autoscaler
This chapter assumes you can read Deployment and HPA manifests, know what resource requests are, and have seen the HPA add Pods. Pod scaling stops helping when the nodes are full. The cluster autoscaler changes the number of nodes so that Pods have somewhere to run.
What the cluster autoscaler does
The cluster autoscaler is a controller that runs in the cluster and talks to your cloud provider's node groups (for example AWS Auto Scaling groups or GKE node pools). It does two things:
- Scale up: when a Pod is
Pendingbecause no node has enough free resources, it adds a node from a node group where the Pod would fit. - Scale down: when a node is underused and its Pods can run elsewhere, it drains the node and removes it.
It is not part of core Kubernetes. You install it separately, or your provider runs it for you. Some clusters use other node provisioners such as Karpenter, which solve the same problem differently. The ideas below apply to both.
Requests drive every decision
The autoscaler does not look at live CPU use. It simulates scheduling using Pod requests. A Pod with no requests looks free to the scheduler, so it never causes a scale-up, and a node full of such Pods looks nearly empty. This is why the Deployment in the resource requests chapter declares cpu: 200m and memory: 128Mi.
How scale-up and scale-down work together
The HPA and the cluster autoscaler work in sequence. The HPA adds replicas. If the existing nodes cannot hold them, the new Pods stay Pending. The autoscaler sees the pending Pods and adds a node. The scheduler then places them.
Adding a node takes minutes, not seconds. Expect a delay between the HPA decision and Pods serving traffic.
When a node is removed
A node becomes a removal candidate when its requested resources stay below a threshold (50% by default) for a period (10 minutes by default). Before removing it, the autoscaler checks that every Pod on it can be rescheduled elsewhere. Some Pods block removal:
- Pods not managed by a controller, such as a bare Pod.
- Pods using local storage such as
emptyDirorhostPath. - Pods whose eviction would violate a PodDisruptionBudget.
- Pods with the annotation
cluster-autoscaler.kubernetes.io/safe-to-evict: "false".
Scale-down uses eviction, so the PodDisruptionBudget from the previous chapter protects your application here.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web
spec:
minAvailable: 2
selector:
matchLabels:
app: webCommon errors
| Symptom | Likely cause |
|---|---|
| Pods stay `Pending`, no node added | The Pod requests more than any node group's node can offer, or the node group is at its maximum size |
| Pods stay `Pending`, no node added | A node selector, taint toleration or zone constraint matches no node group |
| Nodes never removed | A blocking Pod (see above) or a PodDisruptionBudget with no room |
| Scale-up never happens for some Pods | The Pods have no resource requests |
To investigate, read the Pod's events and the autoscaler's own status.
kubectl describe pod <pending-pod-name>
kubectl -n kube-system get configmap cluster-autoscaler-status -o yamlThe Pod events show messages such as FailedScheduling and NotTriggerScaleUp with the reason. The status ConfigMap exists in default installations and lists each node group's health and recent scaling activity. Its name and location can differ between installations.
Exercise
An HPA raises a Deployment from 4 to 8 replicas. Four Pods stay Pending, and the cluster autoscaler adds no node. The node group is below its maximum size. Name two things you would check.
Show answer
Check the Pending Pod's events (kubectl describe pod) for the scale-up decision. Then check that the Pods have resource requests, that each request fits on a single node of the group, and that no node selector, taint or zone constraint rules out every node group.