Sub topic 12
Defining NodePools and EC2NodeClasses
This chapter assumes you know Pods, Deployments and resource requests, can read YAML, and know what an EC2 subnet, security group and IAM role are. Karpenter is already installed in your cluster.
Karpenter splits its configuration into two objects. A NodePool says what kind of nodes may exist and how they are managed. An EC2NodeClass says how to launch them on AWS. Each NodePool points to one EC2NodeClass, and several NodePools can share the same one.
The EC2NodeClass: how to launch
The EC2NodeClass holds the AWS-specific settings.
apiVersion: karpenter.k8s.aws/v1
kind: EC2NodeClass
metadata:
name: default
spec:
role: KarpenterNodeRole-my-cluster
amiSelectorTerms:
- alias: al2023@latest
subnetSelectorTerms:
- tags:
karpenter.sh/discovery: my-cluster
securityGroupSelectorTerms:
- tags:
karpenter.sh/discovery: my-clusterroleis the IAM role that nodes use. It needs the permissions a worker node requires to join the cluster.amiSelectorTermspicks the image. The aliasal2023@latestselects the newest Amazon Linux 2023 AMI.subnetSelectorTermsandsecurityGroupSelectorTermsfind resources by tag. You tag the subnets and security groups once, and Karpenter discovers them. You don't list IDs.
The NodePool: what is allowed
The NodePool sets the rules for the nodes.
apiVersion: karpenter.sh/v1
kind: NodePool
metadata:
name: default
spec:
template:
spec:
requirements:
- key: kubernetes.io/arch
operator: In
values: ["amd64"]
- key: karpenter.sh/capacity-type
operator: In
values: ["spot", "on-demand"]
nodeClassRef:
group: karpenter.k8s.aws
kind: EC2NodeClass
name: default
expireAfter: 720h
limits:
cpu: 1000
disruption:
consolidationPolicy: WhenEmptyOrUnderutilized
consolidateAfter: 1mRead it in four parts:
- requirements limit the instances Karpenter may choose. Here nodes must be amd64 and may be spot or on-demand. You don't list instance types. Karpenter picks from every type that matches.
- nodeClassRef links to the EC2NodeClass by group, kind and name.
- limits cap the total resources in this pool. Once the nodes add up to 1000 CPUs, Karpenter stops launching nodes for this pool.
- disruption and
expireAftercontrol removal. Empty or underutilized nodes are consolidated after one minute, and every node is replaced after 720 hours (30 days).
Common errors
- The EC2NodeClass is not ready. A selector that matches no subnet, security group or AMI leaves the class unusable. Run
kubectl describe ec2nodeclass defaultand read the status conditions. - Pods stay Pending with no new node. Check whether the pool's
limitsare reached, or whether the pod's constraints conflict with the pool's requirements. - Wrong role name.
roletakes the IAM role name, not its ARN.
Exercise
A pod requests an arm64 node, but your only NodePool requires amd64. What happens, and how do you fix it?
Show answer
The pod stays Pending because no NodePool can satisfy it, so Karpenter launches nothing. Fix it by adding arm64 to the kubernetes.io/arch values in an existing NodePool, or by creating a second NodePool for arm64 that references an EC2NodeClass with an arm64-compatible AMI.