CKA — nodeSelector
CKA Exam Domain
| Domain | Weight |
|---|---|
| Workloads & Scheduling | 15% |
Node scheduling questions are among the most common in this domain. You’ll be expected to label nodes and assign Pods to them quickly.
What Is nodeSelector?
nodeSelector is the simplest form of node selection constraint in Kubernetes. It lets you tell the scheduler: “Only place this Pod on nodes that have these labels.”
Under the hood, the scheduler filters candidate nodes and only considers those whose labels match every key-value pair in the Pod’s nodeSelector field.
Key points:
- It’s a field in the Pod spec (
.spec.nodeSelector). - It uses exact label matching — all specified labels must be present on the node.
- It’s been available since Kubernetes v1.0.
Labeling Nodes
Before you can use nodeSelector, the target node must carry the label you want to match.
Add a label to a node:
kubectl label nodes worker-1 disktype=ssd
Verify the label:
kubectl get nodes --show-labels
Filter nodes by label:
kubectl get nodes -l disktype=ssd
Remove a label (trailing minus):
kubectl label nodes worker-1 disktype-
Tip: On the CKA exam,
kubectl labelis your go-to command. Practice the syntax until it’s muscle memory.
Using nodeSelector in Pod Spec
Once the node is labeled, reference the label in your Pod manifest:
apiVersion: v1
kind: Pod
metadata:
name: ssd-pod
spec:
containers:
- name: nginx
image: nginx:1.27
nodeSelector:
disktype: ssd
Apply and verify:
kubectl apply -f ssd-pod.yaml
kubectl get pod ssd-pod -o wide
The NODE column should show worker-1 (or whichever node carries the disktype=ssd label).
What Happens Without a Match?
If no node matches the nodeSelector labels, the Pod stays in Pending state indefinitely.
kubectl describe pod ssd-pod
Look for an event like:
Warning FailedScheduling 0/3 nodes are available: 3 node(s) didn't match Pod's node affinity/selector.
This is a common troubleshooting scenario on the exam — always check kubectl describe pod when a Pod is stuck in Pending.
nodeSelector Limitations
nodeSelector is simple, but that simplicity comes with trade-offs:
| Limitation | Details |
|---|---|
| Exact match only | You can’t say “disktype is ssd OR nvme” |
| No soft preference | If the label isn’t found, the Pod is Pending — there’s no fallback |
| No complex logic | No greater-than, less-than, or set-based operators |
| AND only | Multiple labels in nodeSelector are ANDed — all must match |
When you need more power, reach for Node Affinity (covered in the next lesson).
When to Use nodeSelector vs Affinity
| Feature | nodeSelector | Node Affinity |
|---|---|---|
| Simplicity | Very simple | More verbose YAML |
| Flexibility | Exact match only | Set-based operators (In, NotIn, Exists, Gt, Lt) |
| Soft vs Hard | Hard only | Both required and preferred rules |
| Common Use Case | Quick node pinning | Complex scheduling requirements |
Rule of thumb: If a single key-value label match is all you need, nodeSelector is the right tool. Otherwise, use Node Affinity.
kubectl Commands
# Label a node
kubectl label nodes <node-name> <key>=<value>
# Remove a label
kubectl label nodes <node-name> <key>-
# Show labels on all nodes
kubectl get nodes --show-labels
# Filter nodes by label
kubectl get nodes -l <key>=<value>
# Check which node a Pod landed on
kubectl get pod <pod-name> -o wide
# Describe a Pending Pod to see scheduling failures
kubectl describe pod <pod-name>
Practice Questions
Q1. Label node worker-2 with zone=us-east-1a, then create a Pod that uses nodeSelector to land on that node.
Show Answer
kubectl label nodes worker-2 zone=us-east-1a
apiVersion: v1
kind: Pod
metadata:
name: zone-pod
spec:
containers:
- name: nginx
image: nginx:1.27
nodeSelector:
zone: us-east-1a
kubectl apply -f zone-pod.yaml
kubectl get pod zone-pod -o wide # should show worker-2
Q2. A Pod is stuck in Pending. The nodeSelector says gpu=true but no node has that label. How do you fix it?
Show Answer
Add the label to a suitable node:
kubectl label nodes worker-1 gpu=true
The scheduler will automatically pick up the change and schedule the Pod.
Q3. Can you specify multiple labels in nodeSelector? What happens?
Show Answer
Yes. Multiple labels are ANDed — the node must have all of them:
nodeSelector:
disktype: ssd
zone: us-east-1a
The Pod will only be scheduled on a node that has both labels.
Q4. How do you remove the label disktype from node worker-1?
Show Answer
kubectl label nodes worker-1 disktype-
The trailing - removes the label.
Q5. You need a Pod to run on a node with either disktype=ssd or disktype=nvme. Can nodeSelector handle this?
Show Answer
No. nodeSelector only supports exact match — there’s no OR logic. You need Node Affinity with the In operator:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
- nvme