Sub topic 12
Scaling Pods and Nodes
This chapter assumes you can deploy a workload (the earlier "Deploying and Inspecting an Application" chapter), know how managed node groups work, and have given pods AWS permissions with IRSA or Pod Identity.
Two layers of scaling
Scaling on EKS happens at two layers that work together:
- Pod scaling changes the number of replicas. The Horizontal Pod Autoscaler (HPA) does this.
- Node scaling changes the number of EC2 instances. The Cluster Autoscaler or Karpenter does this.
HPA adds pods when load rises. If the nodes have no room, the new pods stay Pending. A node autoscaler sees those pending pods and adds capacity.
Scaling pods with the HPA
The HPA compares observed usage with the CPU or memory your pods request. It needs the Kubernetes Metrics Server, which EKS does not install by default:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
kubectl top podsYour pods must also declare resources.requests. Utilization is a percentage of the request, so without one the HPA cannot compute it. Add this to the container in the web Deployment:
resources:
requests:
cpu: 250m
memory: 128MiThen create the HPA:
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: 60Check it with kubectl get hpa web. If the target column shows <unknown>, Metrics Server is missing or the pods have no CPU request.
Scaling nodes
Cluster Autoscaler
The Cluster Autoscaler runs as a Deployment in the cluster. It changes the desired size of the Auto Scaling groups behind your managed node groups, within each group's minSize and maxSize. It needs IAM permissions to call Auto Scaling APIs, which you grant with IRSA or Pod Identity. It can never go past maxSize, so a node group set to 4 stops growing at 4 nodes.
Karpenter
Karpenter does not use node groups. It reads the requirements of pending pods (CPU, memory, architecture, zone) and launches EC2 instances that fit them directly. You describe the allowed choices in a NodePool and an EC2NodeClass. Karpenter also removes or replaces underused nodes to consolidate workloads. It needs its own IAM role and is usually installed with Helm.
| Aspect | Cluster Autoscaler | Karpenter |
|---|---|---|
| Unit of scaling | Node group (Auto Scaling group) | Individual EC2 instance |
| Instance choice | Fixed by the node group | Chosen per pending pod |
| Limits | Group `minSize` and `maxSize` | `limits` in the NodePool |
| Consolidation | Removes empty or underused nodes | Removes and replaces nodes to save cost |
Common errors
- Pods stay Pending after the HPA scales up. The node group may be at
maxSize, or the pod requests more than any single instance has. - HPA never scales down. It waits several minutes (a stabilization window) before reducing replicas, so scale-down is slower than scale-up.
- Autoscaler pods crash with access denied. The role lacks the permissions it needs, or the service account is not linked to it.
Exercise
You apply the HPA above, but kubectl get hpa web shows <unknown>/60% for CPU. Name two causes and how to check each.
Show answer
Metrics Server is not installed, which you check with kubectl top pods failing. Or the container has no resources.requests.cpu, which you check by describing the Deployment. Fix the missing piece and the HPA reports a real percentage.