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(("`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-15001{{"`Scheduing with Node Selectors`"}} kubernetes/get -.-> lab-15001{{"`Scheduing with Node Selectors`"}} kubernetes/apply -.-> lab-15001{{"`Scheduing with Node Selectors`"}} kubernetes/label -.-> lab-15001{{"`Scheduing with Node Selectors`"}} kubernetes/initialization -.-> lab-15001{{"`Scheduing with Node Selectors`"}} 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 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