Sub topic 12
Scaling Pods and Nodes
This chapter covers how an EKS cluster grows and shrinks. It assumes you can read Kubernetes manifests, have a running cluster with a managed node group (as built earlier with eksctl), and understand how pods get IAM permissions through IRSA or EKS Pod Identity.
Two Layers of Scaling
Scaling on EKS happens at two separate layers, and each layer has its own tools.
- Pod scaling changes how many replicas of a workload run. The Horizontal Pod Autoscaler (HPA) does this.
- Node scaling changes how many EC2 instances exist to host those pods. The Cluster Autoscaler and Karpenter do this.
The layers work in sequence. When load rises, the HPA raises the replica count. If the new pods cannot be scheduled because no node has free capacity, they stay in the Pending state. A node autoscaler sees those pending pods and adds capacity.
Horizontal Pod Autoscaler
The HPA is built into Kubernetes. It compares a metric against a target and adjusts the replica count of a Deployment. The common case is CPU utilization, which is measured against the pod's CPU request.
The HPA needs the Kubernetes Metrics Server, which is not installed by default on EKS. You can install it as an EKS community add-on or with its manifest. Check that it works before you create an HPA:
kubectl top nodes
kubectl top podsIf these commands return an error such as "Metrics API not available", the HPA will report <unknown> for its targets and will not scale.
The following HPA scales the web Deployment from the earlier chapter between 2 and 10 replicas, aiming for 60% average CPU utilization.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60The Formula
The HPA computes the desired replica count with this rule:
desiredReplicas = ceil(currentReplicas × currentMetric / targetMetric)
For example, 2 pods averaging 90% CPU against a 60% target gives ceil(2 × 90 / 60) = 3 replicas. The controller checks every 15 seconds by default. It also ignores changes smaller than about 10% of the target, which prevents constant small adjustments.
Common HPA Mistakes
- No resource requests. Utilization is a percentage of the request. Without
resources.requests.cpuon the container, the HPA cannot compute it and shows<unknown>. Thewebmanifest from the deployment chapter has no requests, so add them before you attach an HPA. - Requests that are too large or too small. A request of 1 CPU on a pod that uses 100m will almost never reach 60%. A request that is too small makes the HPA scale out too eagerly.
- Fighting the replica field. If your manifest sets
spec.replicasand you reapply it, you reset the count the HPA chose. Removereplicasfrom the Deployment once an HPA manages it. - A
maxReplicasthat the cluster cannot hold. The HPA will request pods the nodes cannot fit, and they will wait for node scaling.
Scale-down is deliberately slow. By default the HPA waits five minutes (the stabilization window) before it removes replicas, so a brief dip in load does not remove capacity you need a moment later. You can tune this with the behavior field.
Scaling Nodes with the Cluster Autoscaler
The Cluster Autoscaler (CA) is a Kubernetes project that works with Auto Scaling groups. Managed node groups are backed by an Auto Scaling group, so the CA changes that group's desired size within its minSize and maxSize. These are the limits you set in the eksctl ClusterConfig earlier (1 and 4).
How it behaves:
- It finds pods that are
Pendingbecause of insufficient resources. - It simulates which node group could fit them and raises that group's desired size.
- It removes nodes that stay underused (below 50% of requests by default) for about 10 minutes, after moving their pods elsewhere.
The CA discovers node groups through tags on the Auto Scaling group, such as k8s.io/cluster-autoscaler/enabled and k8s.io/cluster-autoscaler/demo. Managed node groups created by eksctl can carry these tags when you set iam.withAddonPolicies.autoScaler: true or add them yourself.
The CA runs as a pod, so it needs AWS permissions to read and change Auto Scaling groups. Give them to its service account with IRSA or Pod Identity, using a policy that allows actions such as autoscaling:DescribeAutoScalingGroups and autoscaling:SetDesiredCapacity. Do not attach those permissions to the node role, because every pod on the node could then use them.
Limits of the Cluster Autoscaler
- It is bound to pre-defined node groups with fixed instance types. If your pods need a type no group offers, nothing scales.
- A group is tied to one instance type or a short list. A mixed fleet needs many groups.
- It reacts through the Auto Scaling group, so new nodes can take several minutes to be ready.
- It balances across Availability Zones only if you configure it to. A group that spans zones with persistent volumes can behave badly, because an EBS volume lives in one zone.
Scaling Nodes with Karpenter
Karpenter is an AWS-backed open source node provisioner. It does not use Auto Scaling groups. When it sees pending pods, it reads their requirements (CPU, memory, architecture, zone, labels) and calls the EC2 API to launch instances that fit. This is often called "group-less" scaling.
Karpenter is configured with two main objects:
- A
NodePooldescribes what Karpenter may launch: allowed instance categories, capacity types (on-demand or Spot), limits, and how nodes are consolidated or expired. - An
EC2NodeClassdescribes AWS-specific details: the AMI family, subnets, security groups, and the node IAM role.
This example lets Karpenter use Spot or on-demand instances and cap total CPU at 100. It assumes Karpenter is already installed and that the default EC2NodeClass exists.
apiVersion: karpenter.sh/v1
kind: NodePool
metadata:
name: default
spec:
template:
spec:
nodeClassRef:
group: karpenter.k8s.aws
kind: EC2NodeClass
name: default
requirements:
- key: karpenter.sh/capacity-type
operator: In
values: ["spot", "on-demand"]
- key: karpenter.k8s.aws/instance-category
operator: In
values: ["c", "m", "r"]
limits:
cpu: "100"
disruption:
consolidationPolicy: WhenEmptyOrUnderutilized
consolidateAfter: 1mThe API version and fields change between Karpenter releases, so check the documentation for the version you install.
Two behaviors set Karpenter apart:
- Bin-packing. It picks instance sizes that match the pending pods, instead of adding one more node of a fixed size.
- Consolidation. It replaces or removes nodes when pods can fit on fewer or cheaper ones. This moves pods, so they need a
PodDisruptionBudgetand tolerate being restarted.
Karpenter itself must run somewhere that it does not manage. The usual choice is a small managed node group or Fargate.
Choosing an Approach
| Question | Cluster Autoscaler | Karpenter |
|---|---|---|
| Scales through | Auto Scaling groups | EC2 API directly |
| Instance choice | Fixed per node group | Chosen per pending pod |
| Spot handling | Needs separate groups | Mixed in one NodePool |
| Scale-down | Removes underused nodes | Consolidates and replaces nodes |
| Setup effort | Lower, uses existing groups | Higher, new CRDs and IAM |
Use the Cluster Autoscaler when you have a few stable node groups and want minimal change. Use Karpenter when workloads vary in size, you want Spot savings, or node startup time matters. Running both on the same nodes causes conflicts, so manage each node group with one tool only.
Testing Your Setup
Generate load and watch each layer respond. This command runs a short-lived pod that sends requests to the web Service:
kubectl run load --rm -it --image=busybox:stable --restart=Never -- \
/bin/sh -c "while true; do wget -q -O- http://web >/dev/null; done"In other terminals, watch the results:
kubectl get hpa web --watch
kubectl get pods -l app=web --watch
kubectl get nodes --watchStop the load pod with Ctrl+C and watch replicas fall back after the stabilization window.
Failure Modes to Check
- Pods stay Pending after node scaling. Check
kubectl describe podfor the reason. A common cause is a node selector, taint, or zone constraint that no node group or NodePool satisfies. - IP exhaustion. With the VPC CNI, every pod takes a VPC IP. Many new nodes and pods can use up a subnet's addresses, so size subnets generously.
- Scale-in blocked. Pods with local storage, restrictive PodDisruptionBudgets, or the annotation
cluster-autoscaler.kubernetes.io/safe-to-evict: "false"keep a node alive. - Service quotas. EC2 vCPU quotas for your instance family can stop scale-out without a clear error in Kubernetes. Check the Auto Scaling activity history or Karpenter logs.
Exercise
A Deployment has 4 replicas with a CPU request of 200m each, and an HPA with a 50% utilization target. The pods currently use an average of 150m each. How many replicas does the HPA want, and what happens if the cluster has no room for the extra pods?
Show answer
Utilization is 150m / 200m = 75%. The desired count is ceil(4 × 75 / 50) = ceil(6) = 6 replicas. The two new pods are created but stay Pending because no node can fit them. The Cluster Autoscaler or Karpenter sees the pending pods and adds node capacity, and the pods are then scheduled.
Exercise
Your HPA shows <unknown>/60% for CPU. Name two likely causes.
Show answer
The Metrics Server is not installed or not working (check with kubectl top pods), or the containers have no CPU requests, so utilization cannot be calculated.