Schedule a Pod Using Preferred Node Affinity
The following are the steps for scheduling the pod using preferred Node Affinity:
Step 1: Identify the Node Label
- Firstly identify the node on which you want to deploy the pod, know its node labels.
Step 2: Define Pod Yaml
- Create a pod manifest Yaml with preferred node affinity specified under the spec section.
# Define Pod YAML with Preferred Node Affinity
apiVersion: v1
kind: Pod
metadata:
name: pod-with-preferred-affinity
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: <label-key>
operator: In
values:
- <label-value>
containers:
- name: example-container
image: nginx:latest
Step 3: Apply Pod Configuration
- Using the following command you can apply the pod configuration:
kubectl apply -f pod-with-preferred-affinity.yaml
Step 4: Verify the Pod Placement
- Check the pod scheduled on a node with the specified labels using 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