Node Affinity What are the differences between each operator and when to use each one?

Hello,
I’m not able to get the difference between operators (Not in , Exist, In, …) and when exactly i the appropriate moment to use one rather than the others ?

Let’s start at the beginning

  • Affinity - Pod wants to be aligned with whatever the match expression says.
  • Anti Affinity - Pod wants to be away from whatever the match expression says
  • Pod Affinity - The match expression applies to labels of other pods
  • Node Affinity - The match expression applies to labels of nodes

The most frequent use of all these types is pod anti-affinity, and that’s usually to ensure all the replicas of a deployment come up on different nodes as that is more fault tolerant to the cluster losing a node.

To the match expressions.

  • Key - the name of the label (pod or node) to check. Scheduler will look for all pods with this label in the same namespace (pod affinity), or all the nodes (node affinity).
  • Values - This is a list of one or more values for that label to have
  • Operator - which you’re asking about
    • Exists - The label with the given name exists (values are not checked for this), so all pods (or nodes) having this label with any value are matched
    • NotExists - all pods (or nodes) not having this label with any value are matched
    • In - Any pod (or node) that has a label matching key and its value is any of the values in the values list is matched
    • NotIn - Any pod (or node) that has a label matching key and its value is anything other than the values in the values list is matched

Once the scheduler has worked through all the above, it will schedule the new pod on a node that as closely matches all the required terms (affinity) or on a node that does not match the required terms (anti-affinity)

1 Like