• Node affinity is a way to control where a pod should be placed, by attracting it to certain nodes based on labels.

Node Affinity Configuration

In your pod definition, under spec, you define:

affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
        - matchExpressions:
            - key: size
              operator: In
              values:
                - large
  • key: The label key on the node (e.g., size)
  • operator: In, NotIn, or Exists
  • values: What the pod is looking for (e.g., large, medium)

Types of Node Affinity

  1. RequiredDuringSchedulingIgnoredDuringExecution
  • During scheduling: Pod must land on a matching node.
  • During execution: Pod continues running even if node labels change.
  1. PreferredDuringSchedulingIgnoredDuringExecution
  • During scheduling: Pod tries to go to a matching node, but will schedule elsewhere in certain scenario.
  • During execution: Pod continues running even if node labels change.

Node Affinity vs. Taints and Tolerations

  • Node Affinity: Pulls pods toward nodes
  • Taints and Tolerations: Pushes pods away from nodes

⬅️ Taints and Tolerations | Multi-Container Pods ➡️

References