When deploying workloads in Kubernetes, controlling where your pods land is crucial. Two primary mechanisms facilitate this: nodeSelector
and nodeAffinity
. While they might seem similar at first glance, they serve different purposes and offer varying degrees of flexibility.
The Basics: nodeSelector
The nodeSelector
is the simplest way to constrain pods to specific nodes. It matches pods to nodes based on key-value pairs. For instance:
spec:
nodeSelector:
disktype: ssd
This configuration ensures that the pod is scheduled only on nodes labeled with disktype=ssd
.
However, nodeSelector
has its limitations. It doesn’t support complex queries or multiple values for a single key. If you attempt to specify multiple values for the same key, like so:
nodeSelector:
topology.kubernetes.io/zone: us-east-1a
topology.kubernetes.io/zone: us-east-1b
Only the last key-value pair is considered, effectively ignoring the previous ones. This behavior stems from the fact that YAML maps require unique keys, and Kubernetes doesn’t merge these entries.
Enter nodeAffinity
For more granular control, nodeAffinity
comes into play. It allows you to define rules using operators like In
, NotIn
, Exists
, and DoesNotExist
. This flexibility enables you to match pods to nodes based on a range of criteria.
Suppose you want to schedule a pod on nodes in either us-east-1a
or us-east-1b
. Here’s how you’d achieve that with nodeAffinity
: Continue reading Kubernetes Scheduling: nodeSelector vs nodeAffinity