Scheduling Pods with Node Affinity in Kubernetes

Introduction

This article covers the following tech skills:

In Kubernetes, node affinity is used to schedule pods on nodes that match certain conditions. This can be used to ensure that pods are scheduled on specific types of nodes, or to…


This content originally appeared on DEV Community and was authored by Labby

Introduction

This article covers the following tech skills:

Skills Graph

In Kubernetes, node affinity is used to schedule pods on nodes that match certain conditions. This can be used to ensure that pods are scheduled on specific types of nodes, or to balance the workload across nodes. In this lab, we will learn how to use node affinity to schedule pods on specific nodes.

Creating a Node with Labels

In this step, we will create a node with a label that will be used to schedule pods.

  1. Create a file named node-with-label.yaml with the following contents in the /home/labex directory:
apiVersion: v1
kind: Node
metadata:
  name: minikube
  labels:
    type: web
  1. Apply the changes:
kubectl apply -f node-with-label.yaml
  1. Verify that the node has been created and labeled:
kubectl get nodes --show-labels

Creating a Pod with Node Affinity

In this step, we will create a pod with a node affinity rule that will ensure it is scheduled on a node with a specific label.

  1. Create a file named pod-with-node-affinity.yaml with the following contents in the /home/labex directory:
apiVersion: v1
kind: Pod
metadata:
  name: pod-with-node-affinity
spec:
  containers:
    - name: nginx
      image: nginx:latest
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
              - key: type
                operator: In
                values:
                  - web
  1. Apply the changes:
kubectl apply -f pod-with-node-affinity.yaml
  1. Verify that the pod is scheduled on the node with the type=web label:
kubectl get pod pod-with-node-affinity -o wide

Creating a Pod with Node Anti-Affinity

In this step, we will create a pod with a node anti-affinity rule that will ensure it is not scheduled on a node with a specific label.

  1. Create a file named pod-with-node-anti-affinity.yaml with the following contents in the /home/labex directory:
apiVersion: v1
kind: Pod
metadata:
  name: pod-with-node-anti-affinity
spec:
  containers:
    - name: nginx
      image: nginx:latest
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
              - key: type
                operator: NotIn
                values:
                  - web
  1. Apply the changes:
kubectl apply -f pod-with-node-anti-affinity.yaml
  1. Verify that the pod is not scheduled on the node with the type=db label:
kubectl get pod pod-with-node-anti-affinity -o wide

Creating a Pod with Node Affinity and Node Selector

In this step, we will create a pod with both a node affinity rule and a node selector that will ensure it is scheduled on a node with a specific label.

  1. Create a file named pod-with-node-affinity-and-selector.yaml with the following contents in the /home/labex directory:
apiVersion: v1
kind: Pod
metadata:
  name: pod-with-node-affinity-and-selector
spec:
  containers:
    - name: nginx
      image: nginx:latest
  nodeSelector:
    type: web
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
              - key: type
                operator: In
                values:
                  - db
  1. Apply the changes:
kubectl apply -f pod-with-node-affinity-and-selector.yaml
  1. Verify that the pod is not scheduled on the node with the type=web label:
kubectl get pod pod-with-node-affinity-and-selector -o wide

Creating a Pod with Multiple Node Affinity Rules

In this step, we will create a pod with multiple node affinity rules that will ensure it is scheduled on a node with labels that match all of the rules.

  1. Create a file named pod-with-multiple-node-affinity.yaml with the following contents in the /home/labex directory:
apiVersion: v1
kind: Pod
metadata:
  name: pod-with-multiple-node-affinity
spec:
  containers:
    - name: nginx
      image: nginx:latest
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
              - key: type
                operator: In
                values:
                  - web
          - matchExpressions:
              - key: disktype
                operator: In
                values:
                  - ssd
  1. Apply the changes:
kubectl apply -f pod-with-multiple-node-affinity.yaml
  1. Verify that the pod is scheduled on the node with the type=web and disktype=ssd labels:
kubectl get pod pod-with-multiple-node-affinity -o wide

Summary

In this lab, we learned how to use node affinity to schedule pods on specific nodes. We created a node with a label, and then created pods with node affinity rules that ensured they were scheduled on nodes with specific labels. We also created a pod with a node anti-affinity rule that ensured it was not scheduled on a node with a specific label. Finally, we created a pod with multiple node affinity rules that ensured it was scheduled on a node with labels that matched all of the rules.

MindMap

🚀 Practice Now: Scheduing with Node Affinity

Want to Learn More?


This content originally appeared on DEV Community and was authored by Labby


Print Share Comment Cite Upload Translate Updates
APA

Labby | Sciencx (2024-07-18T03:26:43+00:00) Scheduling Pods with Node Affinity in Kubernetes. Retrieved from https://www.scien.cx/2024/07/18/scheduling-pods-with-node-affinity-in-kubernetes/

MLA
" » Scheduling Pods with Node Affinity in Kubernetes." Labby | Sciencx - Thursday July 18, 2024, https://www.scien.cx/2024/07/18/scheduling-pods-with-node-affinity-in-kubernetes/
HARVARD
Labby | Sciencx Thursday July 18, 2024 » Scheduling Pods with Node Affinity in Kubernetes., viewed ,<https://www.scien.cx/2024/07/18/scheduling-pods-with-node-affinity-in-kubernetes/>
VANCOUVER
Labby | Sciencx - » Scheduling Pods with Node Affinity in Kubernetes. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/18/scheduling-pods-with-node-affinity-in-kubernetes/
CHICAGO
" » Scheduling Pods with Node Affinity in Kubernetes." Labby | Sciencx - Accessed . https://www.scien.cx/2024/07/18/scheduling-pods-with-node-affinity-in-kubernetes/
IEEE
" » Scheduling Pods with Node Affinity in Kubernetes." Labby | Sciencx [Online]. Available: https://www.scien.cx/2024/07/18/scheduling-pods-with-node-affinity-in-kubernetes/. [Accessed: ]
rf:citation
» Scheduling Pods with Node Affinity in Kubernetes | Labby | Sciencx | https://www.scien.cx/2024/07/18/scheduling-pods-with-node-affinity-in-kubernetes/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.