Sub topic 6
Choosing a match operator
This chapter assumes you can write a pod manifest, apply it with kubectl, and read node labels. You should also know that required node affinity uses nodeSelectorTerms and matchExpressions. Here you will learn which operator to put in each expression.
The shape of an expression
Each entry in matchExpressions has three fields: key, operator, and usually values. The key is a node label name. The operator says how to compare that label with values.
Kubernetes supports six operators in node affinity.
| Operator | Matches a node when | `values` |
|---|---|---|
| `In` | the label exists and its value is in the list | one or more strings |
| `NotIn` | the label value is not in the list, or the label is missing | one or more strings |
| `Exists` | the label key is present, with any value | must be empty |
| `DoesNotExist` | the label key is absent | must be empty |
| `Gt` | the label value, read as an integer, is greater than the given number | exactly one integer, as a string |
| `Lt` | the label value, read as an integer, is less than the given number | exactly one integer, as a string |
Picking an operator
Use In when you know the allowed values. It is the closest match to nodeSelector, and it also allows several values, such as two zones.
Use NotIn to exclude values. A node with no such label still matches, because the label value is not in the list. If you want to exclude a value but also require that the label exists, add an Exists expression for the same key.
Use Exists and DoesNotExist when the presence of a label is the signal, not its value. For example, a node might carry example.com/maintenance with no meaningful value.
Use Gt and Lt for numeric labels. The label value must parse as an integer. If it does not, the node does not match.
A worked example
The next manifest requires a node with more than one GPU that is not in maintenance. The two expressions sit in one term, so both must be true.
apiVersion: v1
kind: Pod
metadata:
name: gpu-job
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: example.com/gpu-count
operator: Gt
values:
- "1"
- key: example.com/maintenance
operator: DoesNotExist
containers:
- name: nginx
image: nginx:1.27Quote the number in values. Label values and the values list are strings, so an unquoted 1 makes the manifest fail validation.
To try it, label a node and check the result:
kubectl label nodes worker-1 example.com/gpu-count=4
kubectl get nodes -L example.com/gpu-count,example.com/maintenanceA node with gpu-count=4 and no maintenance label matches. A node with gpu-count=1 fails Gt. A node with maintenance=true fails DoesNotExist, even if its GPU count is high.
Common errors
- Values on
Exists.ExistsandDoesNotExistmust not have avalueslist. The API server rejects the manifest if you add one. - Several values on
GtorLt. These operators take exactly one value. - Unquoted numbers. Write
"1", not1. - Assuming
NotInneeds the label. Nodes without the label passNotIn. This can place pods on nodes you did not mean to allow. - Non-numeric labels with
GtorLt. A value such asmanynever matches. - Expecting
Gtto include equality.Gt 1excludes a node labeled1. For "at least 2", useGtwith"1".
Exercise
A pod must avoid nodes labeled disktype=hdd, but it should still be allowed on nodes that have no disktype label at all. Which operator do you use, and how would you change the rule to also require that the disktype label exists?
Show answer
Use NotIn with values: ["hdd"]. Nodes without a disktype label match, because the label value is not in the list. To also require the label, add a second expression in the same term with key: disktype and operator: Exists. Both expressions must then hold.
Exercise
A node is labeled example.com/gpu-count=2. Does it match operator: Gt with values: ["2"]? Does it match operator: Lt with values: ["3"]?
Show answer
It does not match Gt "2", because 2 is not greater than 2. It does match Lt "3", because 2 is less than 3.