Understanding Node Affinity in Kubernetes
Node affinity in Kubernetes is a way to control where pods are scheduled based on specific conditions on nodes. This ensures optimal placement and efficient use of resources. Node affinity offers more flexibility than node selectors.
Types of Node Affinity:
Required Node Affinity (hard requirement): Pods will only be scheduled on nodes that meet the specified conditions.
- requiredDuringSchedulingIgnoredDuringExecution: Schedule pod only if node matches.
Preferred Node Affinity (soft requirement): Kubernetes tries to schedule pods on nodes that meet the conditions, but it's not guaranteed.
- preferredDuringSchedulingIgnoredDuringExecution: Prefer scheduling on matching nodes.
Example Of Node Affinity :
Node Affinity Operators:
In: Node label value is in the specified list.
NotIn: Node label value is not in the specified list.
Exists: Node label key exists.
DoesNotExist: Node label key does not exist.
Gt/Lt: Node label value is greater/less than the specified value.
Common Use Cases:
Hardware-specific workloads: Schedule pods on nodes with specific hardware (e.g., GPU).
Resource-constrained workloads: Schedule pods on nodes with enough resources (e.g., memory).
Geographic constraints: Schedule pods in specific availability zones or regions.
Security and compliance: Schedule pods on nodes with specific security settings.
Subscribe to my newsletter
Read articles from Kaleem Mirza directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by