Sub topic 1
Two layers of autoscaling: pods and nodes
Prerequisites
This chapter assumes you know Pods, Deployments and Nodes, and that you can read a manifest and run kubectl. You should also know what a resource request is and have a rough idea of what a cloud virtual machine is. AWS is used for examples.
The two layers
Kubernetes autoscaling works at two levels, and each level answers a different question.
- Pod-level scaling decides how many replicas, or how large each replica is, a workload needs. The Horizontal Pod Autoscaler (HPA) changes the replica count of a Deployment based on metrics such as CPU use. The Vertical Pod Autoscaler (VPA) changes the resource requests of the pods.
- Node-level scaling decides how many machines the cluster has. The Cluster Autoscaler and Karpenter add nodes when pods cannot be placed and remove nodes that are not needed.
The two layers do not talk to each other directly. The HPA creates more pods. If the nodes are full, those pods cannot be scheduled. A node autoscaler reacts to that.
What triggers node scaling
A node autoscaler does not watch CPU use on nodes. It watches for pods that the scheduler marked unschedulable. Such a pod is in the Pending phase, and its events say it does not fit on any current node. Scale-up is driven by what pods request, not by what they actually use.
Scale-down works in the opposite direction. A node is a candidate for removal when the pods on it could run elsewhere, or when it has nothing useful running.
Here is a Deployment you can use to see this. It starts at zero replicas and asks for 1 CPU and 1Gi of memory per pod.
apiVersion: apps/v1
kind: Deployment
metadata:
name: inflate
spec:
replicas: 0
selector:
matchLabels:
app: inflate
template:
metadata:
labels:
app: inflate
spec:
containers:
- name: inflate
image: registry.k8s.io/pause:3.9
resources:
requests:
cpu: "1"
memory: 1GiScale it up and watch what happens:
kubectl scale deployment inflate --replicas=10
kubectl get pods -l app=inflate
kubectl describe pod -l app=inflate | grep -A5 Events
kubectl get nodes -wIf the cluster has no room, some pods show Pending. The events report a FailedScheduling reason such as insufficient cpu. With a node autoscaler installed, a new node then joins and the pods start.
Common errors
- Expecting the autoscaler to react to high node CPU. It reacts to Pending pods and to requests.
- Leaving out resource requests. Pods without requests look free to the scheduler, so nodes get overcommitted and the autoscaler sees little demand.
- Running an HPA with no node autoscaler. New replicas stay Pending when the cluster is full.
- Assuming every Pending pod can be fixed with a new node. A pod whose request is larger than any possible node, or whose selectors match nothing, stays Pending.
Exercise
An HPA raises a Deployment from 5 to 20 replicas, but 8 pods stay Pending and node CPU use is only 30%. Which layer is missing or misconfigured, and why does low CPU use not prevent the problem?
Show answer
The node layer is missing or not working, because no node autoscaler is adding capacity. Low CPU use does not matter because the scheduler places pods by their requests. The existing nodes have their requestable CPU and memory reserved, so the new pods do not fit even though the real usage is low.