DEV Community

Cover image for Scheduling Pods with Node Affinity in Kubernetes
Labby for LabEx

Posted on

Scheduling Pods with Node Affinity in Kubernetes

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
Enter fullscreen mode Exit fullscreen mode
  1. Apply the changes:
kubectl apply -f node-with-label.yaml
Enter fullscreen mode Exit fullscreen mode
  1. Verify that the node has been created and labeled:
kubectl get nodes --show-labels
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Apply the changes:
kubectl apply -f pod-with-node-affinity.yaml
Enter fullscreen mode Exit fullscreen mode
  1. Verify that the pod is scheduled on the node with the type=web label:
kubectl get pod pod-with-node-affinity -o wide
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Apply the changes:
kubectl apply -f pod-with-node-anti-affinity.yaml
Enter fullscreen mode Exit fullscreen mode
  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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Apply the changes:
kubectl apply -f pod-with-node-affinity-and-selector.yaml
Enter fullscreen mode Exit fullscreen mode
  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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Apply the changes:
kubectl apply -f pod-with-multiple-node-affinity.yaml
Enter fullscreen mode Exit fullscreen mode
  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
Enter fullscreen mode Exit fullscreen mode

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?

Top comments (0)