Lesson 57 · Section 5: Application Lifecycle Management
Lesson 57: Autoscaling: match capacity to load, by hand or automatically
The map of scaling in Kubernetes: more Pods or bigger Pods, more nodes or bigger nodes, done by hand or by an autoscaler. How every autoscaler works as a feedback loop on metrics and requests, which tool answers which problem, and what the exam expects. · 16 min read · 5 min video
Official competency this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why autoscaling exists
Load is never constant. A shop is quiet at four in the morning and busy at eight in the evening; a batch system is idle until the nightly job arrives. If you give an application a fixed size, you have to choose which of those moments it is sized for, and both choices cost something:
- Sized for the peak, it wastes CPU and memory (and money) for most of the day.
- Sized for the average, it slows down, drops requests or gets killed when the peak comes.
You can change the size by hand, and you will in the exam, but that only works while somebody is watching. Autoscaling hands the watching to a controller: you state a goal ("keep average CPU around 70%", "between 2 and 10 replicas"), and Kubernetes changes the size as the load moves.
This lesson is the map. The next three lessons take the pieces one at a time: the Horizontal Pod Autoscaler, the in-place resize of Pods, and the Vertical Pod Autoscaler.
What you can scale
Parts:
- More Pods (kubectl scale, HPA)
- Workload (your Pods)
- Bigger Pods (resize, VPA)
- More nodes (Cluster Autoscaler)
- Cluster (the nodes)
- Bigger nodes (replace the machine)
Connections:
- Workload to More Pods: out
- Workload to Bigger Pods: up
- Cluster to More nodes: out
- Cluster to Bigger nodes: up
Horizontal scaling (scaling out and in) changes how many copies run. Vertical scaling (scaling up and down) changes how much CPU and memory each copy gets. Both apply at two levels, which gives four combinations:
| By hand | Automatically | |
|---|---|---|
| More Pods (workload, horizontal) | kubectl scale, or edit spec.replicas | HorizontalPodAutoscaler (HPA), built into Kubernetes |
| Bigger Pods (workload, vertical) | kubectl set resources (replaces the Pods), or an in-place resize of a running Pod | VerticalPodAutoscaler (VPA), an add-on you install |
| More nodes (cluster, horizontal) | Create a machine and run kubeadm join | Cluster Autoscaler or Karpenter, add-ons that need a cloud provider |
| Bigger nodes (cluster, vertical) | Drain the node, replace or resize the machine, bring it back | Not done in practice: node autoscalers add and remove machines instead |
Two words in that table decide what you can use in a given cluster:
- Built in means the API and its controller ship with Kubernetes. The HPA is the only autoscaler for which that
is true: the
HorizontalPodAutoscalerkind is in theautoscaling/v2API and its controller runs inside kube-controller-manager. - Add-on means somebody has to install it first. The VPA brings its own CustomResourceDefinitions and controllers. The Cluster Autoscaler has to talk to a cloud API to create machines, so on a cluster of fixed machines, like the kubeadm clusters in the exam, there is nothing for it to do.
Out or up?
| Horizontal: more Pods | Vertical: bigger Pods | |
|---|---|---|
| Fits | Stateless applications that can run as identical copies behind a Service | Applications that cannot simply be copied: a single-instance database, a legacy app |
| Upper limit | The capacity of the whole cluster | The size of one node: a Pod cannot span nodes |
| Resilience | Better: copies are spread over nodes | Unchanged: one big Pod is still one Pod |
| Disruption | None: existing Pods keep running | Traditionally the Pod is recreated; an in-place resize can avoid that |
| Typical use | Following the load through the day | Getting requests and limits right ("right-sizing") |
Most workloads use both ideas, but for different jobs: vertical scaling to find the right size of one copy, and horizontal scaling to follow the load.
How it works
Parts:
- Pods (use CPU and memory)
- kubelet (measures containers)
- metrics-server (collects the numbers)
- API server (metrics.k8s.io)
- Autoscaler (compares to target)
- Deployment web (replicas: 3 → 5)
Connections:
- Pods to kubelet: usage
- kubelet to metrics-server: scrape
- metrics-server to API server: serves the metrics
- API server to Autoscaler: reads
- Autoscaler to Deployment web: scales
- Deployment web to Pods: adds or removes Pods
Every autoscaler runs the same loop:
- Measure. The kubelet on each node measures the CPU and memory its containers use. metrics-server collects
those numbers from every kubelet and serves them through the API server as the resource metrics API
(
metrics.k8s.io). This is the same datakubectl topshows. - Compare. The autoscaler reads the metrics for the Pods of one workload and compares them with the target you set.
- Change. If they differ enough, it edits the workload: the HPA writes a new replica count, the VPA new requests. It does not create or delete Pods itself.
- Repeat. The Deployment and ReplicaSet controllers, the scheduler and the kubelet react as they would to any edit, the usage changes, and the loop measures again. The HPA runs its loop every 15 seconds by default.
That design has two consequences that explain most autoscaling problems.
No metrics, no autoscaling. metrics-server is not part of a kubeadm cluster; it is an add-on. Without it the
loop has nothing to read: kubectl top fails with Metrics API not available, and an HPA shows its target as
<unknown> and never scales.
No requests, no percentage. "70% CPU" means 70% of the container's CPU request, averaged over the Pods. A container without a CPU request has no 100% to measure against, so a utilization target cannot be computed for it. Requests are also what the scheduler and the node autoscaler count, so they are the foundation of all three.
Under the hood · The scale subresource: why one autoscaler works for many kinds
kubectl scale and the HPA do not edit the Deployment's full specification. They use a small sub-API of the object
called the scale subresource (/scale), which exposes only the replica count and the Pod selector. Any kind that
offers it can be scaled the same way: Deployments, ReplicaSets, StatefulSets, and custom resources that declare it.
A DaemonSet has no replica count and no scale subresource: it runs one Pod per matching node, so its size follows
the number of nodes. That is why kubectl scale daemonset fails and an HPA cannot target one.
The subresource is also why scaling does not start a rollout. It changes spec.replicas, not the Pod template, so
the same ReplicaSet simply gets more or fewer Pods.
Under the hood · Why autoscalers move carefully
Metrics are noisy, and new Pods take time to start, so an autoscaler that reacted to every reading would add and remove Pods continuously ("flapping"). Each one therefore damps its decisions. The HPA ignores differences of less than about 10% from the target, scales up quickly, and by default waits for five minutes of consistently lower load before it scales down. The node autoscalers wait before they remove a node that looks underused.
In practice this means: scaling up is visible within a minute or so, scaling down takes several minutes, and neither is instant. Autoscaling absorbs trends, not sudden spikes; the spare capacity you keep (the gap between the target and 100%) is what absorbs the spike while new Pods start.
The autoscalers, and when to use which
| Autoscaler | Changes | Reacts to | Comes with Kubernetes? |
|---|---|---|---|
| HorizontalPodAutoscaler | replicas of a Deployment, StatefulSet or ReplicaSet | CPU or memory utilization, or custom and external metrics | Yes (needs metrics-server for CPU and memory) |
| VerticalPodAutoscaler | Requests and limits of the containers | Observed usage over time | No: add-on with its own CRDs |
| Cluster Autoscaler, Karpenter | Number of nodes | Pods that cannot be scheduled; nodes that are underused | No: add-on, needs a cloud provider |
| KEDA | replicas, including down to zero | Events, such as the length of a queue | No: add-on (CNCF project) |
| Cluster Proportional Autoscaler | replicas of a system workload such as cluster DNS | The number of nodes and cores in the cluster | No: add-on |
Picking one is mostly a matter of naming the symptom:
- Too many requests for the Pods I have → more Pods: HPA.
- Each Pod is too small or far too big for what it does → right-size: VPA, or change the requests yourself.
- Pods are
Pendingbecause no node has room → more nodes: node autoscaler, or add a node by hand. - Work arrives as messages or jobs, not as CPU load → KEDA.
Pods first, then nodes
Parts:
- Traffic rises (CPU above target)
- Pod autoscaler (3 → 6 replicas)
- New Pods (Pending: no room)
- Node autoscaler (asks the cloud)
- New node (joins the cluster)
- Pods Running (load is shared)
Connections:
- Traffic rises to Pod autoscaler: CPU up
- Pod autoscaler to New Pods: adds
- New Pods to Node autoscaler: unschedulable
- Node autoscaler to New node: adds
- New node to Pods Running: placed
The Pod autoscaler and the node autoscaler do not know about each other. They are connected only through the scheduler:
- Load rises, and the HPA raises the replica count.
- The new Pods need a node. If none has enough unreserved requests, they stay
Pendingwith aFailedSchedulingevent. - Unschedulable Pods are exactly what a node autoscaler watches for. It works out which kind of node would fit them and asks the cloud provider for one.
- The node joins, the scheduler places the Pods, and they start.
Scaling in runs the other way: the HPA removes Pods, some nodes end up mostly empty, and the node autoscaler drains and deletes them.
Notice what the node autoscaler does not look at: real CPU usage. A cluster whose nodes are 90% busy but where every Pod is scheduled gets no new node, and a cluster whose nodes are idle but fully reserved by requests does.
Using it
Example · Scale a workload by hand
The Deployment web in the namespace shop should run five replicas.
kubectl -n shop scale deployment web --replicas=5
kubectl -n shop get deployment web
NAME READY UP-TO-DATE AVAILABLE AGE
web 5/5 5 5 2d
The same command works for a StatefulSet or a ReplicaSet, and for several objects at once
(kubectl scale deployment web api --replicas=3). --replicas=0 stops the application without deleting it.
kubectl edit deployment web and changing spec.replicas has the same effect. No rollout starts, because the Pod
template did not change.
Example · Give each Pod more resources by hand
Each web Pod needs a CPU request of 200m and a memory request of 128Mi.
kubectl -n shop set resources deployment web -c nginx \
--requests=cpu=200m,memory=128Mi --limits=cpu=500m,memory=256Mi
kubectl -n shop rollout status deployment web
This changes the Pod template, so the Deployment replaces every Pod in a rolling update. That is manual vertical scaling the classic way. A running Pod can also be resized without being replaced, which has its own lesson (In-Place Resize of Pods).
Example · Check that the cluster can autoscale at all
Before creating any autoscaler, check the two things the loop depends on.
kubectl top nodes # works only if metrics-server is running
kubectl -n kube-system get deployment metrics-server
kubectl -n shop get deployment web \
-o jsonpath='{.spec.template.spec.containers[*].resources}{"\n"}'
NAME CPU(cores) CPU(%) MEMORY(bytes) MEMORY(%)
controlplane 187m 9% 1320Mi 35%
node01 64m 3% 702Mi 18%
NAME READY UP-TO-DATE AVAILABLE AGE
metrics-server 1/1 1 1 6d
{"limits":{"cpu":"500m","memory":"256Mi"},"requests":{"cpu":"200m","memory":"128Mi"}}
If kubectl top answers error: Metrics API not available, fix metrics-server first. If the resources come back
empty ({}), add requests first.
Example · Hand the replica count to an autoscaler
Keep web between 2 and 10 replicas, aiming for 70% average CPU utilization.
kubectl -n shop autoscale deployment web --min=2 --max=10 --cpu=70%
kubectl -n shop get hpa web
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
web Deployment/web cpu: 20%/70% 2 10 2 45s
TARGETS reads "current / target". Usage is far below the target here, so the HPA has settled on the minimum. On
kubectl older than v1.34 the flag is --cpu-percent=70; check kubectl autoscale --help for the one your version
accepts. The next lesson covers the HPA manifest, its algorithm and its behaviour settings.
Tricks
Common mistakes
- Creating an HPA in a cluster without metrics-server. It is accepted, and then does nothing:
TARGETSstays<unknown>. - Autoscaling on utilization without resource requests. The percentage is of the request. No request, no percentage.
- Scaling by hand while an autoscaler is active.
kubectl scaleworks for a moment, then the HPA puts its own number back. ChangeminReplicasandmaxReplicasinstead, or delete the HPA. - Expecting the HPA to fix
PendingPods. More replicas do not help when no node has room. That is a capacity problem: lower the requests, free space, or add a node. - Trying to scale a DaemonSet. It has no replica count. To run fewer copies, restrict the nodes it matches.
- Scaling the ReplicaSet of a Deployment. The Deployment owns it and resets the count. Scale the Deployment.
- Expecting vertical scaling to be free. Changing resources in a Deployment's template restarts every Pod, and no Pod can be given more than its node has.
- Expecting instant reactions. Autoscalers act on averages over time, and scale down slowly on purpose.
Recap
- Scaling answers two questions: Pods or nodes, and out (more) or up (bigger). Each can be done by hand or by an autoscaler.
- By hand:
kubectl scalefor replicas,kubectl set resourcesfor requests and limits,kubeadm joinfor nodes. - Automatically: the HPA (built in) for replicas, the VPA (add-on) for Pod size, a node autoscaler (add-on, needs a cloud) for nodes.
- Every autoscaler is a loop on metrics: it needs metrics-server and resource requests to work.
- Pod and node autoscaling chain together through
PendingPods; the exam concentrates on workload autoscaling.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.
Create a HorizontalPodAutoscaler for a Deployment, and give it what it needs to read a CPU percentage at all.
Build · Level 2 · standard · target 15 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.