Scheduing with Node Selectors

KubernetesKubernetesBeginner
Practice Now

Introduction

In this lab, we will start by creating a simple deployment and then assign Node Selectors to it. We will then move on to more complex scenarios where we will use different selectors 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/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") subgraph Lab Skills kubernetes/get -.-> lab-15001{{"`Scheduing with Node Selectors`"}} kubernetes/apply -.-> lab-15001{{"`Scheduing with Node Selectors`"}} end

Creating a Simple Deployment

In this step, we will create a simple deployment with a single pod.

  1. Create a file named simple-deployment.yaml with the following contents:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: simple-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: simple-app
  template:
    metadata:
      labels:
        app: simple-app
    spec:
      containers:
        - name: simple-container
          image: nginx:latest
  1. Use kubectl to create the deployment:
kubectl apply -f simple-deployment.yaml
  1. Verify that the deployment has been created:
kubectl get deployments

Assigning Node Selectors to a Deployment

In this step, we will assign a Node Selector to the deployment we created in Step 1.

  1. Create the nodes with a label:
kubectl label nodes minikube disk=ssd
  1. Edit the node-selector-deployment.yaml file and add the nodeSelector field under the spec.template.spec section:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: selector-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: selector-app
  template:
    metadata:
      labels:
        app: selector-app
    spec:
      nodeSelector:
        disk: ssd
      containers:
        - name: selector-container
          image: nginx:latest
  1. Use kubectl to apply the changes:
kubectl apply -f node-selector-deployment.yaml
  1. Verify that the pod has been scheduled on a node with the label disk=ssd:
kubectl get pods -o wide | grep selector-deployment

Using Different Node Selectors

In this step, we will use different Node Selectors to schedule pods on specific nodes based on the labels assigned to those nodes.

  1. Create three nodes with different labels:
kubectl label nodes minikube resigon=labex
kubectl label nodes minikube gpu=true
  1. Create a file named multi-selector-deployment.yaml with the following contents:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: multi-selector-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: multi-selector-app
  template:
    metadata:
      labels:
        app: multi-selector-app
    spec:
      containers:
        - name: multi-selector-container
          image: nginx:latest
      nodeSelector:
        resigon: labex
        gpu: "true"
  1. Apply the changes:
kubectl apply -f multi-selector-deployment.yaml
  1. Verify that the pods have been scheduled on nodes with the appropriate labels:
kubectl get pods -o wide | grep multi-selector-deployment

Summary

In this lab, we learned how to schedule pods on specific nodes using Node Selectors and Node Affinity. We started with a simple deployment and then moved on to more complex scenarios where we used different selectors and affinity rules to schedule pods on specific nodes based on the labels assigned to those nodes.

Congratulations! You have successfully completed this lab.

Other Kubernetes Tutorials you may like