Sub topic 15
Influencing where Pods run
This chapter assumes you can read a Pod manifest, know what labels and selectors are, and understand resource requests. By default the scheduler picks any node with enough free capacity. You can narrow that choice with a few Pod fields.
How the scheduler decides
The scheduler works in two steps. It first filters out nodes that cannot run the Pod. It then scores the remaining nodes and picks the best one. The constraints below mostly act on the filter step. If no node passes, the Pod stays Pending.
The scheduler already uses resource requests. A node must have enough unreserved CPU and memory for the sum of the Pod's requests. Limits play no part in this decision.
nodeSelector
nodeSelector is the simplest constraint. It is a map of labels, and the node must carry all of them.
First label a node, then reference the label in the Pod.
kubectl label node worker-1 disktype=ssdapiVersion: v1
kind: Pod
metadata:
name: web-ssd
spec:
nodeSelector:
disktype: ssd
containers:
- name: nginx
image: nginx:1.27If no node has disktype=ssd, the Pod remains Pending.
Node affinity
Node affinity expresses the same idea with more operators, such as In, NotIn, and Exists. It also supports preferences.
apiVersion: v1
kind: Pod
metadata:
name: web-affinity
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 50
preference:
matchExpressions:
- key: topology.kubernetes.io/zone
operator: In
values:
- zone-a
containers:
- name: nginx
image: nginx:1.27The required rule is a hard filter. The preferred rule only raises a node's score. "IgnoredDuringExecution" means that if node labels change after the Pod is running, the Pod is not moved.
Pod affinity and anti-affinity work the same way, but they match labels on other Pods. They are commonly used to spread replicas across nodes.
Taints and tolerations
Selectors attract Pods to nodes. Taints do the opposite: a taint on a node repels Pods unless they carry a matching toleration.
kubectl taint nodes worker-2 dedicated=gpu:NoScheduleapiVersion: v1
kind: Pod
metadata:
name: gpu-job
spec:
tolerations:
- key: dedicated
operator: Equal
value: gpu
effect: NoSchedule
containers:
- name: app
image: busybox:1.36
command: ["sleep", "3600"]A toleration only permits scheduling on the tainted node. It does not require it. To pin a Pod to that node, combine the toleration with a nodeSelector or node affinity.
| Effect | Behavior |
|---|---|
| `NoSchedule` | New Pods without a toleration are not scheduled on the node. |
| `PreferNoSchedule` | The scheduler tries to avoid the node but may use it. |
| `NoExecute` | Also evicts running Pods that lack a toleration. |
Diagnosing a Pending Pod
Check the Events section of kubectl describe pod. A message like 0/3 nodes are available: 3 node(s) didn't match Pod's node affinity/selector names the constraint that failed.
Exercise
A Pod has a toleration for the taint dedicated=gpu:NoSchedule and no nodeSelector. Will it always run on the tainted node?
Show answer
No. The toleration only allows the Pod to be scheduled there. The scheduler can still place it on any other node that fits. To force it onto the tainted node, add a nodeSelector or required node affinity that matches a label on that node.