06 · Workloads: land each one where it belongs¶
Goal: an always-on service on stable-static, a batch job on
spot-burst, and proof that each landed where it should.
Both halves¶
A taint on a pool keeps pods out unless they tolerate it. A toleration lets a pod in, but it doesn't keep the pod out of other pools. To pin a workload to one pool you need both:
| Toleration | nodeSelector | |
|---|---|---|
| What it does | Lets the pod onto the tainted pool | Restricts the pod to nodes with that label |
| Without it | Pod can't use the pool | Pod could land on any untainted pool, e.g. general-purpose |
Our NodePools put a label (workload-tier: stable / burst) on their nodes
through template.metadata.labels, and a matching taint. The workloads use
both halves.
The stable service¶
# The always-on service. Lives only on stable-static.
# Walkthrough: docs/06-workloads.md
apiVersion: apps/v1
kind: Deployment
metadata:
name: checkout-api
namespace: demo-nodepools
labels:
app: checkout-api
tier: stable
spec:
replicas: 4
selector:
matchLabels:
app: checkout-api
template:
metadata:
labels:
app: checkout-api
tier: stable
spec:
# Both halves, always:
# toleration -> lets the pod IN to the tainted stable pool
# nodeSelector -> keeps it OUT of every other pool
nodeSelector:
workload-tier: stable
tolerations:
- key: workload-tier
operator: Equal
value: stable
effect: NoSchedule
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: checkout-api
containers:
- name: api
image: public.ecr.aws/docker/library/nginx:stable-alpine
ports:
- containerPort: 80
resources:
requests:
cpu: 250m
memory: 128Mi
limits:
memory: 128Mi
readinessProbe:
httpGet:
path: /
port: 80
periodSeconds: 5
---
# What makes node replacement (expiry, drift) survivable: one pod at a time.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: checkout-api
namespace: demo-nodepools
spec:
maxUnavailable: 1
selector:
matchLabels:
app: checkout-api
nodeSelector+tolerations: both halves, forworkload-tier=stable.topologySpreadConstraints: spread the four replicas across both nodes.- The PodDisruptionBudget (
maxUnavailable: 1) is the third leg of the "never disrupt" promise: when a node expires or drifts, its pods are evicted one at a time and the service keeps serving.
kubectl apply -f manifests/30-stable-app.yaml
kubectl -n demo-nodepools rollout status deploy/checkout-api --timeout=180s
No new nodes were needed: the pods went straight onto the static capacity that was already there.
The burst job¶
# Fault-tolerant batch work. Lives only on spot-burst.
# Runs ~3 minutes, then the Spot nodes it caused are consolidated away.
# Walkthrough: docs/06-workloads.md
apiVersion: batch/v1
kind: Job
metadata:
name: report-batch
namespace: demo-nodepools
labels:
app: report-batch
tier: burst
spec:
parallelism: 12 # 12 pods x 1 vCPU, well under the pool's 64
completions: 12
backoffLimit: 6 # Spot reclaims are expected, so retry
ttlSecondsAfterFinished: 600
template:
metadata:
labels:
app: report-batch
tier: burst
spec:
restartPolicy: OnFailure
nodeSelector:
workload-tier: burst
tolerations:
- key: workload-tier
operator: Equal
value: burst
effect: NoSchedule
terminationGracePeriodSeconds: 20
containers:
- name: worker
image: public.ecr.aws/docker/library/busybox:1.36 # multi-arch: runs on Graviton Spot too
command: ["sh", "-c", "echo \"crunching on $(hostname) ($(uname -m))\"; sleep 180; echo done"]
resources:
requests:
cpu: "1"
memory: 512Mi
limits:
memory: 512Mi
Twelve pods, each requesting one full vCPU, each sleeping three minutes.
busybox is a multi-arch image, so the pods run on Graviton Spot nodes too.
backoffLimit: 6 because a Spot reclaim killing a pod is expected, not an
error.
The pods go Pending: there's no Spot capacity yet. Auto Mode sees them and
launches Spot nodes to fit them. Watch it happen:
New NodeClaims appear for spot-burst within seconds, and in the test run all
12 pods were running about 30 seconds after the apply. Ctrl+C when the
NodeClaims are ready.
Where did everything land?¶
All nodes, with their pool, capacity type, instance type and architecture:
kubectl get nodes -L karpenter.sh/nodepool,karpenter.sh/capacity-type,node.kubernetes.io/instance-type,kubernetes.io/arch
You should see the two stable-static on-demand nodes from step 05, plus
new spot-burst nodes with CAPACITY-TYPE spot. Auto Mode picks whatever fits
cheapest from the allowed families and architectures; in the test run that was
two Graviton c7gd.2xlarge (arm64) Spot nodes for the 12 pods.
Pods, with the node each runs on:
To see the pool behind each pod in one table, join pods to node labels with
jq. The first command builds a node → pool/capacity map; the second looks
each pod's node up in it:
NODE_POOLS=$(kubectl get nodes -o json | jq -c '[.items[] | {key: .metadata.name, value: {pool: (.metadata.labels["karpenter.sh/nodepool"] // "-"), cap: (.metadata.labels["karpenter.sh/capacity-type"] // "-")}}] | from_entries')
kubectl -n demo-nodepools get pods -o json | jq -r --argjson n "$NODE_POOLS" '
["POD", "PHASE", "NODEPOOL", "CAPACITY"],
(.items[] | [.metadata.name, .status.phase, ($n[.spec.nodeName // ""].pool // "-"), ($n[.spec.nodeName // ""].cap // "-")])
| @tsv' | column -t
POD PHASE NODEPOOL CAPACITY
checkout-api-957945fb9-79ngc Running stable-static on-demand
checkout-api-957945fb9-pbn9r Running stable-static on-demand
checkout-api-957945fb9-s4hfd Running stable-static on-demand
checkout-api-957945fb9-vmjx9 Running stable-static on-demand
report-batch-2gnlr Running spot-burst spot
report-batch-4f9qz Running spot-burst spot
...
Every checkout-api pod is on stable-static; every report-batch pod is on
spot-burst; nothing of ours is on general-purpose. Both halves, doing their
jobs.
The job's own logs show the architecture each pod runs on:
If you get remote error: tls: internal error, the Spot nodes are only seconds
old and their kubelet certificates aren't ready yet. Wait 20 seconds and retry.
[pod/report-batch-4f9qz/worker] crunching on report-batch-4f9qz (aarch64)
[pod/report-batch-5hrqj/worker] crunching on report-batch-5hrqj (aarch64)
...
Next: 07 Prove the tuning. The batch takes about three minutes; step 08 watches what happens after it finishes.