Kubernetes 101 : Node Affinity

Types of affinities:
  • Node affinity : with node affinity pods can be scheduled on nodes that have certain labels.
  • Pod affinity : with pod affinity, pods can be scheduled where other specific pods live in order to make it easier for these pods to communicate for example.
  • pod anti-affinity : using pod anti-affinity, some pods are not scheduled on the same node as other pods, like pods that use up a lot of resources for example.
Below a Yaml configuration file for a pod that is scheduled on a node that has the labels "cpu=quad_core" and "graphics=nvidia" :

requiredDuringSchedulingIgnoredDuringExecution: means that the node affinity takes place during the scheduling of the pod and ignored while the pod is running, for example if the node doesn't meet the requirements for running the pods anymore.
preferredDuringSchedulingIgnoredDuringExecution: it is only "preferred" that the pod is scheduled on a node that meets the requirement but this is not "required".

The affinity "weight" parameter:

We can use a weight parameter for the node affinity as below:

preferredDuringSchedulingIgnoredDuringExecution: shows the node affinity preferences. 

The  weight value:

Each element (cpu or graphics) has a weight value. 
Kubernetes compares the labels of the nodes in the cluster to the label in the list of the Yaml file. 
Kubernetes adds up the weights of the matching labels for each node and tries to schedule the pod on the node with the highest weight.


If the node doesn't match all the preferred labels, the pod will be deployed on node with the label that has the higher weight , five in our example above.
The weight value could be between one and one hundred.


Leave as a comment: