💻 Tech
Node selector is used to specify the nodes to which the pods will be scheduled based on labels set on the node. Node affinity uses the same concept, but it can handle more complex conditions.
The two YAML files below demonstrate Node selector and Node affinity respectively. They are essentially the same, showing that at the basic requirements, the two concepts can be used. But what if you want the pods to still be scheduled to the nodes regardless of the labels? In that case, you can use Node affinity’s preferredDuringSchedulingIgnoredDuringExecution node affinity type (note that word preferred). And what if you want to check if the label just exists? You can use the operator Exists. Those are just some of the ways you can make the selection criteria more complex to meet your needs.
apiVersion:
kind: Pod
...
spec:
containers:
- name: nginx
image: nginx
nodeSelector:
size: Large
apiVersion:
kind: Pod
spec:
containers:
- name: nginx
image: nginx
affinity:
- nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: size
operator: In
values:
- Large