NodeAffinity

NodeAffinity is a concept used to schedule pods in cluster nodes.
How it works
Affinity work by maching the label on the node with affinity set on pod. That mean that a label must be set on a node
k label node <node-name> color=red
Then Configure you pods with an affinity that must or should be matched to a node label before a pods is scheduled on the node.
Example yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: redis
name: redis
spec:
containers:
- image: redis
name: redis
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: color
operator: In
values:
- red
status: {}
As opposed to taints and tolerance where if a pod with the required tolerance had been assigned to a node, then in the future the node taint is updated to a value that don`t tolarate the already scheduled pod. That scheduled pod has to be revoked from the node.
with nodeAffinity there are two properties to fix this issue
requiredDuringSchedulingIgnoredDuringExecution - as the name suggest, it ensure the condition is met during initial sheculing of the pod. If it does not match the pod remains pending mode until on of the nodes is labeled wiith a matching label
preferredDuringSchedulingIgnoredDuringExecution - The main defference is that this tries to match the affity condition but if it fails. It will shedule the pod in any node. We should use this if the pod must be scheduled.
Example of pod with preferredDuringSchedulingIgnoredDuringExecution
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: redis
name: redis
spec:
containers:
- image: redis
name: redis
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1 # gives a priority
preference:
matchExpressions:
- key: disktype
operator: In
values:
- hdd
status: {}
That all I have about NodeAffinities.

