Types of Node Affinity
Based on node properties, Node Affinity is used to specify the scheduling preferences for pods. You can further categorize Node Affinity into Three types:
1.RequiredDuringSchedulingRequiredDuringExecution
This is a hard rule. The pod must be scheduled on a node that complies with the node affinity criteria If no nodes in the cluster satisfy the rule, then the pod will remain unscheduled. If the node labels are changed in the future the pod will be evicted.
affinity:
nodeAffinity:
requiredDuringSchedulingRequiredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: <label-key>
operator: In
values:
- <label-value>
In this case, the label key-value pair specified in the “nodeSelectorTerms” prevents the pod from being scheduled on nodes that do not have it.
2.RequiredDuringSchedulingIgnoredDuringExecution
This is the second hard rule. The pod will be scheduled only if the pod labels are matched with the node labels. If the node labels are changed in the future the pod will not be evicted.
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: <label-key>
operator: In
values:
- <label-value>
3. PreferredDuringSchedulingIgnoredDuringExecution
This is a soft rule. This specifies the primary way for scheduling a node for a pod in accordance with the node affinity rule. The pod will still be scheduled on a node that does not match with the rule if none of the cluster’s nodes do. Below is the example of PreferredDuringSchedulingIgnoredDuringExecution:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: <label-key>
operator: In
values:
- <label-value>
In this case, the pod will be scheduled on a node that has the label key-value pair specified in the “match expressions” section. If nodes do not meet this requirement, the pod will still be scheduled on a different node.
Node affinity is a powerful resource that may be used to improve Kubernetes cluster performance and resource usage, but it also has pros and cons. The following are some of the advantages and disadvantages of node affinity:
Node Affinity in Kubernetes
Node affinity in Kubernetes refers to the ability to assign a Kubernetes pod to a specific node or group of nodes in a cluster based on specific criteria. A feature called node affinity is employed to guarantee that particular pods are located on particular nodes in a cluster. This facilitates better resource management and performance optimization of the application.
In Kubernetes, a node is a physical or virtual machine that controls one or more pods. Pods are the smallest deployable components in Kubernetes and are used to run containerized applications. With the use of node affinity, specific pods can be scheduled on particular nodes on the basis of a variety of factors, such as the node’s CPU or memory capacity or its location within a particular region or data center.
Table of Content
- What is Node Affinity?
- What Are Node Labels?
- What Are the Types of Node Affinity in Kubernetes?
- Types of Node Affinity
- Difference Between Node Selector And Node Affinity
- Difference Between Pod Affnity vs Node Affinity
- Advantages Of Kuberentes Node Affinity
- Disadvantages Of Kubernetes Node Affinity
- Difference of Pod Affinity, Intra-pod Affinity and Anti-Affinity
- How to Assign Pods to Nodes Using Node Affinity ? A Step-By-Step Guide
- Adding A Label to a Node
- Schedule A Pod Using Required Node Affinity
- Schedule a Pod Using Preferred Node Affinity
- Assigning Pods to Nodes
- Command to See Existing labels of the Node
- Command to set New Labels to Node
- Example of NodeAffinity
- Kubernetes Node Affinity – FAQs
Contact Us