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:
Step 1: Firstly ensure that the kuberentes cluster is in running state with kubernetes master and worker nodes.
Step 2: Create label the nodes with their specifications so that we can make easier to deploy pods on where we want to deploy. The following command helps in setting the labels to the nodes.
kubectl label nodes <node-names> <label-key>=<label-value>
Step 3: Define the node affinity in Pod Yaml
- In you pod manefist file, try to define the node affinity rules under the spec section. The following is an example of Yaml code:
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: <label-key>
operator: In
values:
- <label-value>
Step 4: Apply the pod configuration
- After once defining the affinity rules save the yaml configuration file and apply it using the following command:
kubectl apply -f <file_name.yaml>
Step 5: Verify Pod Placement
- Verify on where the pod is placement on the Node with the following command;
kubectl get pods -o wide
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