Sub topic 8
How terms, expressions, and nodeSelector combine
This chapter assumes you can write a pod manifest, apply it with kubectl, and read node labels. It also assumes you know the nodeSelector field and the required and preferred node affinity rules from earlier chapters. Here you learn how several constraints on one pod combine into a single scheduling decision.
The three levels of combination
A pod can carry constraints at three levels. The scheduler checks all of them, and a node must pass every required check.
| Level | Where it appears | How entries combine |
|---|---|---|
| `nodeSelector` | `spec.nodeSelector` | AND across all key-value pairs |
| `matchExpressions` | inside one entry of `nodeSelectorTerms` | AND across all expressions |
| `nodeSelectorTerms` | under `requiredDuringSchedulingIgnoredDuringExecution` | OR across all terms |
Both nodeSelector and node affinity must be satisfied when they appear together. Kubernetes does not let one override the other.
Expressions inside one term: AND
When one term lists several matchExpressions, every expression must be true. The example below accepts a node only if its example.com/gpu-count label is an integer greater than 1 and it has no example.com/maintenance label.
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: example.com/gpu-count
operator: Gt
values:
- "1"
- key: example.com/maintenance
operator: DoesNotExistA node with gpu-count=4 and no maintenance label passes. A node with gpu-count=4 and maintenance=true fails the second expression, so the whole term fails.
Terms: OR
Each item under nodeSelectorTerms starts with - matchExpressions:. A node needs to satisfy only one of these terms. The pod below runs on a node in zone-a or on a node labeled disktype=ssd.
apiVersion: v1
kind: Pod
metadata:
name: either-or
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: topology.kubernetes.io/zone
operator: In
values:
- zone-a
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: nginx
image: nginx:1.27A common indentation mistake
The position of the - changes the meaning. Compare these two fragments.
# AND: one term, two expressions
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values: ["ssd"]
- key: kubernetes.io/os
operator: In
values: ["linux"]# OR: two terms, one expression each
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values: ["ssd"]
- matchExpressions:
- key: kubernetes.io/os
operator: In
values: ["linux"]The first fragment demands an SSD node that also runs Linux. The second accepts any SSD node, or any Linux node. A misplaced dash can silently turn a strict rule into a loose one.
Combining with nodeSelector
Suppose a pod has nodeSelector: {disktype: ssd} and a required affinity that allows zone-a or zone-b. A node is eligible only if it has disktype=ssd and is in one of those two zones. A node in zone-a without the SSD label is rejected.
Preferred rules do not filter
Preferred rules (preferredDuringSchedulingIgnoredDuringExecution) never remove a node. They add their weight to the score of nodes that match. If you combine them with required rules, the required rules filter first and the preferred rules rank the nodes that remain. The prefer-ssd example from the previous chapter works this way: Linux is mandatory, and SSD only adds 80 points.
Exercise
A pod has nodeSelector: {disktype: ssd} and a required node affinity with two nodeSelectorTerms. Term 1 requires zone In [zone-a]. Term 2 requires zone In [zone-b]. A node has disktype=ssd and zone=zone-b. Can the pod be scheduled on it? What if the node has zone=zone-b but no disktype label?
Show answer
Yes for the first node. It satisfies the nodeSelector, and it matches term 2, which is enough because terms are ORed. No for the second node, because every nodeSelector pair must match regardless of the affinity terms.