Sub topic 14
Spot, on-demand and instance diversification
This chapter assumes you know what a Karpenter NodePool and EC2NodeClass are, and that you understand resource requests and Pending pods. It covers how Karpenter chooses between spot and on-demand capacity, and why offering many instance types makes spot dependable.
Two capacity types
On AWS, an EC2 instance is bought in one of two ways.
- On-demand instances are billed per second at a fixed price. AWS does not reclaim them.
- Spot instances use spare AWS capacity at a discount, often large. AWS can reclaim a spot instance with a two-minute interruption notice.
Karpenter exposes this choice through the well-known label karpenter.sh/capacity-type. You set it in a NodePool requirement:
requirements:
- key: karpenter.sh/capacity-type
operator: In
values: ["spot", "on-demand"]When both values are allowed, Karpenter prefers spot. It launches on-demand only if no spot capacity can satisfy the pending pods. A pod can also pin itself with nodeSelector: {karpenter.sh/capacity-type: on-demand}.
Why diversification matters
Spot capacity is a set of separate pools. Each pool is one instance type in one availability zone. A pool can run out while others stay available. If your NodePool allows only m5.large, you depend on a single pool per zone.
Diversifying means allowing many instance types, sizes, generations and zones. Karpenter then sends AWS a list of candidate types, and AWS fulfills the request from the pools least likely to be interrupted. This is the price-capacity-optimized strategy that Karpenter uses for spot.
You widen the choice with requirements on instance properties instead of naming specific types:
requirements:
- key: karpenter.k8s.aws/instance-category
operator: In
values: ["c", "m", "r"]
- key: karpenter.k8s.aws/instance-generation
operator: Gt
values: ["4"]
- key: kubernetes.io/arch
operator: In
values: ["amd64"]This allows the compute, general-purpose and memory families from generation 5 onward. New instance types that match are picked up without you editing the NodePool.
Handling interruptions
When AWS sends a spot interruption notice, Karpenter reads it from an SQS queue that you configure at install time. It then cordons and drains the node and launches a replacement. Without that queue, Karpenter only learns about the loss when the node disappears.
The two minutes only help if your pods shut down quickly. Set a sensible terminationGracePeriodSeconds, and run several replicas spread across nodes.
Common errors
- A narrow instance list. One or two types cause
InsufficientInstanceCapacityerrors and slow launches. Widen the requirements. - Spot for everything. Single-replica or slow-starting services suffer from interruptions. Keep them on on-demand, using a separate NodePool or a selector.
- Tiny instances only. Per-node overhead such as DaemonSets and pod limits wastes more of a small node.
- Missing interruption queue. Nodes vanish without a graceful drain.
Exercise
A NodePool allows only m5.large and spot. Pods go Pending with capacity errors. What two changes make this more resilient?
Show answer
Widen the instance requirements to several categories, sizes and generations so Karpenter has many spot pools to choose from. Also add on-demand to the allowed capacity types so Karpenter can fall back when spot is unavailable.