How to schedule Pods on specific nodes?
· Category: Kubernetes
Short answer
Use nodeSelector for simple matching, affinity for complex rules, and taints with tolerations to repel or attract Pods to specific nodes.
Steps
- Label nodes:
kubectl label nodes node1 disk=ssd. - Use
nodeSelectororaffinityin the Pod spec. - Use
taintson nodes andtolerationson Pods for dedicated nodes.
Example
nodeSelector:
disk: ssd
Affinity:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disk
operator: In
values:
- ssd
Tips
- Use
preferredDuringSchedulingfor soft constraints. - Taints are useful for dedicated nodes like GPU or control plane nodes.
- The scheduler also considers resource requests and pod affinity.
Common issues
- Incorrect labels cause Pods to remain Pending.
- Too many hard constraints can lead to unschedulable Pods.
- Changing labels does not evict existing Pods.