Simple Node Selector
Before exploring node affinity, consider a simple node selector that schedules a pod on a node labeled with size “Large”:Using Node Affinity
Node affinity uses a similar underlying concept but allows for more advanced expressions. The following example demonstrates how to schedule a pod on a node with a labelsize whose value is in the specified list:
In this configuration:
- The
affinityblock is defined under the podspec. nodeAffinityspecifies the criteria used for node scheduling.- The field
requiredDuringSchedulingIgnoredDuringExecutionindicates a mandatory requirement for scheduling; if no node meets the criteria, the pod is not scheduled. nodeSelectorTermsholds an array of conditions—in this case, ensuring that the node labelsizemust have a value included in the specified list.
NotIn operator:
Exists operator offers another approach. Rather than comparing against specific values, it checks for the presence of the label. This example schedules the pod on any node where the size label is defined:
Behavior and Lifecycle of Node Affinity
When a pod is created, the Kubernetes scheduler evaluates its node affinity rules to determine the node on which to schedule the pod. However, several scenarios can occur if node conditions change over time.Node Affinity Types
There are two primary types of node affinity currently supported:-
requiredDuringSchedulingIgnoredDuringExecution:
The scheduler enforces that the pod be placed on a node that satisfies the affinity rules. If no matching node is available, the pod remains unscheduled. Once the pod is running, changes to node labels do not impact the running pod. -
preferredDuringSchedulingIgnoredDuringExecution:
The scheduler attempts to honor the specified node affinity rules. If a matching node is not found, the pod can be scheduled on a non-matching node. Similarly, node label changes after scheduling are ignored.
Future releases of Kubernetes plan to introduce additional affinity types that enforce rules during both scheduling and execution:
- requiredDuringSchedulingRequiredDuringExecution
- preferredDuringSchedulingRequiredDuringExecution
Scheduling vs. Execution
Node affinity rules are applied during two key phases of a pod’s lifecycle:-
During Scheduling:
At the time of pod creation, the scheduler evaluates the node affinity rules to determine an appropriate node. If using the required type and no matching node is found (for example, if a node is missing the expected label “Large”), the pod will not be scheduled. -
During Execution:
Once the pod is running, changes in node labels are typically ignored for the current affinity types (“ignored during execution”). However, with forthcoming execution-enforced rules, pods might be evicted if the node subsequently fails to meet the affinity criteria.
A pod is scheduled on a node with the label
size=Large. If an administrator later removes this label, the pod continues to run under the current behavior. Future implementations with the “required during execution” option could result in pod eviction.
