nodeSelector is the simplest way to constrain Pods to nodes with specific labels. Affinity and anti-affinity expands
the types of constraints you can define. Some of the benefits of affinity and anti-affinity include:
The affinity/anti-affinity language is more expressive. nodeSelector only selects nodes with all the specified
labels. Affinity/anti-affinity gives you more control over the selection logic.
•
You can indicate that a rule is soft or preferred, so that the scheduler still schedules the Pod even if it can't
find a matching node.
•
You can constrain a Pod using labels on other Pods running on the node (or other topological domain),
instead of just node labels, which allows you to define rules for which Pods can be co-located on a node.
•
The affinity feature consists of two types of affinity:
• Node affinity functions like the nodeSelector field but is more expressive and allows you to specify soft rules.
• Inter-pod affinity/anti-affinity allows you to constrain Pods against labels on other Pods
There are two types of node affinity:
requiredDuringSchedulingIgnoredDuringExecution: The scheduler can't schedule the Pod unless the rule is
met. This functions like nodeSelector, but with a more expressive syntax.
•
preferredDuringSchedulingIgnoredDuringExecution: The scheduler tries to find a node that meets the rule. If a
matching node is not available, the scheduler still schedules the Pod
•
Note: want to see the example and want to read more than refer this link.