Example of NodeAffinity

You must include the selector rules in the pod’s YAML definition file in order to use the node selector in Kubernetes. For example, the YAML file below defines a pod with a necessary node selector for nodes with the label “nginx” 

In the following example, we have used the ‘nodeSelector’ parameter for showing the required node affinity for nodes that do have the label ‘nginx’. This means that the pod will be scheduled on the nodes which contain this label.

Let’s take another example of Node Affinity. For this, we will first create a deployment. The deployment will have the following features:

  • name: Blue
  • image: nginx
  • replicas: 3

In order to create this deployment we can use two different approaches. However, for simplicity, we will use an imperative approach. We can create this deployment using the command:

  • To check whether or not the deployment was created successfully, execute the command:

  •  We have two nodes in my cluster, the control plane, and node01. 

  • For this example, we want to Set Node Affinity to the deployment to place the pods on node01 only.
  1. Name of the deployment: blue
  2. Replicas: 3
  3. Image: nginx
  4. NodeAffinity: requiredDuringSchedulingIgnoredDuringExecution
  5. Key: color
  6. value: blue
  • In order to achieve this, we will edit the deployment using:

  • Once we are in we will add the following block of script on the same level of container:

  • The specs of a Kubernetes object are specified in the “spec” field. It is used for defining the pod’s affinity rules.
  • The node affinity and all other scheduling preferences for the pod are mentioned in the “affinity” column. The preferences of the pod for the nodes on which it is scheduled are specified in the “node affinity” column.
  • The nodeSelectorTerms requirements must be met for the pod to be scheduled, according to the “requiredDuringSchedulingIgnoredDuringExecution” parameter. The rules are ignored while the pod is being executed, thus if the node’s labels change while the pod is running, it won’t be rescheduled.
  • The ‘nodeSelectorTerms’ field is used to define the requirements for a node to satisfy for scheduling. It has mentioned the ‘matchExpressions’ field in its instance.
  • In this, we have used the ‘matchExpressions’ for specifying a set of label selectors to match with the labels specified on the node. We have used the operator ‘In’ and the corresponding values ‘blue’ are matching against the ‘color’ label key.
  • Thus, this code directs that the pod be scheduled on nodes that have the label “color” and the value “blue” assigned to them.
  • Once we have updated the file, We can see that the pods are running on node01. To check the configuration we should run the command:

As we can see that all the pods are deployed on the node01 node. In Conclusion, your Kubernetes clusters’ performance and resource efficiency may be enhanced by using node affinity. Assuring that your pods are scheduled on nodes with the resources they require through the use of node affinity can help to reduce resource contention and enhance application performance.

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

Similar Reads

What is Node Affinity?

Node Affinity in Kubernetes is a feature in Kubernetes that facilitates us to specify the rules for scheduling the pod based on the node labels. It facilitates providing a way for pod placement by expressing the requirements about the nodes where the pods should be scheduled. Node Affinity allows to expression of various conditions as preferred such as node attributes, and anti-affinity rules to avoid certain nodes and to have more complex label expressions for pod placement strategies....

What Are Node Labels?

Node Labels in kubernetes are key-value pairs to kubernetes nodes. It is used for describing the characteristics of nodes such as hardware capabilities, geographical locations, environment and other any metadata. These labels are used to organize and categorize the nodes on various factors like geo-locations, hardware resources etc.. Node labels are essential in kubernetes in scheduling the workloads on certain required nodes. It facilitates the user to define affinity or anti-affinity rules, node selectors and other scheduling constraints to control the pod distribution across the cluster. These Labels provides the flexible mechanism for grouping and targeting the nodes based on different criteria, with efficient resource allocation and management....

What Are the Types of Node Affinity in Kubernetes?

Required node affinity and Preferred node affinity. We use the required node affinity to specify which pod needs to be scheduled on which node. The node’s CPU or memory capacity, location in a particular area or data center, or any other special label that the node has been given may all have an impact on this specification....

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:...

Difference Between Node Selector And Node Affinity

In Kubernetes, the concepts of node selector and node affinity are used to control the scheduling of pods onto the required cluster nodes....

Difference Between Pod Affnity vs Node Affinity

The following are the differences between Pod Affinity and Node Affinity:...

Advantages Of Kuberentes Node Affinity

The following are the advantages of kubernetes Node Affinity:...

Disadvantages Of Kubernetes Node Affinity

The following are the disadvantages of kuberentes Node Affinity:...

Difference of Pod Affinity, Intra-pod Affinity and Anti-Affinity

The following are the difference of pod affinity, Intra-pod affinity and Anti-Affinity:...

How to Assign Pods to Nodes Using Node Affinity ? A Step-By-Step Guide

Assigning the pds to Nodes using the node affinity provides the control on which nodes our pods to be scheduled, based on the node labels. The following is the step by step guide for assigning the pods to nodes using node affinity:...

Adding A Label to a Node

On adding the labels to the nodes provides to specify additional metadata to the node, which can be used for various purposes like node selection, scheduling and grouping....

Schedule A Pod Using Required Node Affinity

The following are the steps for sechule a pod using required Node Affinity:...

Schedule a Pod Using Preferred Node Affinity

The following are the steps for scheduling the pod using preferred Node Affinity:...

Assigning Pods to Nodes

The following are the steps for assigning the pods to nodes:...

Command to See Existing labels of the Node

The following is the command used to see the existing labels of the Node:...

Command to set New Labels to Node

The following is the command used to set the new labels to the Node:...

Example of NodeAffinity

You must include the selector rules in the pod’s YAML definition file in order to use the node selector in Kubernetes. For example, the YAML file below defines a pod with a necessary node selector for nodes with the label “nginx”...

Kubernetes Node Affinity – FAQs

What are the two types of Kubernetes Nodes?...

Contact Us