Scheduing with Node Affinity

KubernetesKubernetesBeginner
Practice Now

Introduction

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ConfigurationandVersioningGroup(["`Configuration and Versioning`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicsGroup(["`Basics`"]) kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/ConfigurationandVersioningGroup -.-> kubernetes/label("`Label`") kubernetes/BasicsGroup -.-> kubernetes/initialization("`Initialization`") subgraph Lab Skills kubernetes/create -.-> lab-18468{{"`Scheduing with Node Affinity`"}} kubernetes/get -.-> lab-18468{{"`Scheduing with Node Affinity`"}} kubernetes/apply -.-> lab-18468{{"`Scheduing with Node Affinity`"}} kubernetes/label -.-> lab-18468{{"`Scheduing with Node Affinity`"}} kubernetes/initialization -.-> lab-18468{{"`Scheduing with Node Affinity`"}} end

Start the Minikube Cluster

Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.

  1. Navigate to your working directory:

    Open the terminal and navigate to the default project folder:

    cd /home/labex/project
  2. Start Minikube:

    Start Minikube to initialize a Kubernetes cluster:

    minikube start
    • This command sets up a single-node Kubernetes cluster on your local machine.
    • Minikube may take a few minutes to start depending on your system's performance.
  3. Verify Minikube is running:

    Check the status of the Minikube cluster:

    minikube status
    • Look for components like kubelet and apiserver listed as Running.
    • If the cluster is not running, rerun minikube start.

If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.

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.

Other Kubernetes Tutorials you may like