Skip to main content

Scheduling Pods with Taints and Tolerations in Kubernetes

Hands-On Lab

 

Photo of Chad Crowell

Chad Crowell

DevOps Training Architect II in Content

Length

00:30:00

Difficulty

Intermediate

In this hands-on lab, you will be presented with a three-node cluster. One node is the master, and the other two are worker nodes. You will be responsible for splitting up the two worker nodes and making one of the worker nodes a production (prod) environment node and the other a development (dev) environment node. The purpose of identifying these two types (prod and dev) is to not accidentally deploy pods into the production environment. You will use taints and tolerations to achieve this, and then you will deploy two pods: One pod will be scheduled to the dev environment, and one pod will be scheduled to the prod environment. When you have verified the two pods are up and running and they are located within the correct environments, you may consider this hands-on lab complete.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Scheduling Pods with Taints and Tolerations in Kubernetes

In this hands-on lab, we have been given a three-node cluster. Within that cluster, we must perform the following tasks to taint the production node in order to repel work. We will create the necessary taint to properly label one of the nodes “prod.” Then, we will deploy two pods — one to each environment. One pod spec will contain the toleration for the taint.

Solution

Log in to the Master server using the credentials on the lab page (either in your local terminal, using the Instant Terminal feature, or using the public IPs):

ssh cloud_user@<KUBE_MASTER_PUBLIC_IP_ADDRESS>

Hint: When copying and pasting code into Vim from the lab guide, first enter :set paste (and then i to enter insert mode) to avoid adding unnecessary spaces and hashes.

Taint one of the worker nodes to repel work.

  1. List out the nodes:

    kubectl get nodes
  2. Taint the node, replacing <NODE_NAME> with one of the worker node names returned in the previous command:

    kubectl taint node <NODE_NAME> node-type=prod:NoSchedule

Schedule a pod to the dev environment.

  1. Create the dev-pod.yaml file:

    vim dev-pod.yaml
  2. Enter the following YAML to specify a pod that will be scheduled to the dev environment:

    apiVersion: v1
    kind: Pod
    metadata:
      name: dev-pod
      labels:
        app: busybox
    spec:
      containers:
      - name: dev
        image: busybox
        command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
  3. Save and quit the file by pressing Escape followed by wq!.

  4. Create the pod:

    kubectl create -f dev-pod.yaml

Schedule a pod to the prod environment.

  1. Create the prod-deployment.yaml file:

    vim prod-deployment.yaml
  2. Enter the following YAML to specify a pod that will be scheduled to the prod environment:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: prod
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: prod
      template:
        metadata:
          labels:
            app: prod
        spec:
          containers:
          - args:
            - sleep
            - "3600"
            image: busybox
            name: main
          tolerations:
          - key: node-type
            operator: Equal
            value: prod
            effect: NoSchedule
  3. Save and quit the file by pressing Escape followed by wq!.

  4. Create the pod:

    kubectl create -f prod-deployment.yaml

Verify each pod has been scheduled to the correct environment.

  1. Verify the pods have been scheduled:

    kubectl get pods -o wide
  2. Scale up the deployment:

    kubectl scale deployment/prod --replicas=3
  3. Look at our deployment again:

    kubectl get pods -o wide

    We should see that two more pods have been deployed.

Conclusion

Congratulations on successfully completing this hands-on lab!