Kubernetes Cordon and Uncordon Command

KubernetesKubernetesBeginner
Practice Now

Introduction

In a Kubernetes cluster, nodes can go into various states, such as "ready" or "not ready". The cordon and uncordon commands are used to control the scheduling of pods on a particular node. The cordon command marks a node as "unschedulable", preventing new pods from being scheduled on that node, while the uncordon command marks a node as "schedulable" again, allowing new pods to be scheduled on that node. In this lab, we will explore how to use these commands to control the scheduling of pods on nodes in a Kubernetes cluster.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ConfigurationandVersioningGroup(["`Configuration and Versioning`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicsGroup(["`Basics`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/cordon("`Cordon`") kubernetes/BasicCommandsGroup -.-> kubernetes/uncordon("`Uncordon`") kubernetes/ConfigurationandVersioningGroup -.-> kubernetes/label("`Label`") kubernetes/BasicsGroup -.-> kubernetes/initialization("`Initialization`") subgraph Lab Skills kubernetes/describe -.-> lab-9664{{"`Kubernetes Cordon and Uncordon Command`"}} kubernetes/get -.-> lab-9664{{"`Kubernetes Cordon and Uncordon Command`"}} kubernetes/cordon -.-> lab-9664{{"`Kubernetes Cordon and Uncordon Command`"}} kubernetes/uncordon -.-> lab-9664{{"`Kubernetes Cordon and Uncordon Command`"}} kubernetes/label -.-> lab-9664{{"`Kubernetes Cordon and Uncordon Command`"}} kubernetes/initialization -.-> lab-9664{{"`Kubernetes Cordon and Uncordon Command`"}} 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.

Cordoning a Node

In this step, we will use the cordon command to mark a node as "unschedulable", preventing new pods from being scheduled on that node. Here are the steps:

  1. List the nodes in the cluster using the following command:
kubectl get nodes
  1. Cordon the node using the following command:
kubectl cordon minikube
  1. Verify that the node has been cordoned by checking the SchedulingDisabled field in the node's status using the following command:
kubectl get node | grep SchedulingDisabled

Uncordoning a Node

In this step, we will use the uncordon command to mark a node as "schedulable" again, allowing new pods to be scheduled on that node. Here are the steps:

  1. Uncordon the node using the following command:
kubectl uncordon minikube
  1. Verify that the node has been uncordoned by checking the SchedulingDisabled field in the node's status using the following command:
kubectl get node | grep SchedulingDisabled

Cordoning and Uncordoning Nodes with Pods

In this step, we will simulate a scenario where a node needs to be cordoned and uncordoned while it has pods running on it. Here are the steps:

  1. Execute the following command to enter the directory /home/labex/project/:
cd /home/labex/project/
  1. Create a deployment named "deploy.yaml" with multiple copies using the following YAML file in the directory /home/labex/project/:
## deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.16
          ports:
            - containerPort: 80
  1. Cordon the node using the following command:
kubectl cordon minikube
  1. Apply the YAML file using the following command::
kubectl apply -f deploy.yaml
  1. List the pods running on the nodes using the following command:
kubectl get pods -o wide

Check if the Pod is scheduled and started properly.

  1. Uncordon the node using the following command:
kubectl uncordon minikube
  1. Check the status of the pods running on the node to ensure that they are rescheduled on the uncordoned node using the following command:
kubectl get pods -o wide

Using Labels to Cordon and Uncordon Nodes

In this step, we will use labels to cordoned and uncordoned nodes based on specific criteria. Here are the steps:

  1. Label the nodes in the cluster using the following command:
kubectl label nodes minikube org=labex
  1. Cordon the nodes that have the specified label using the following command:
kubectl cordon -l org=labex
  1. Verify that the nodes have been cordoned by checking the SchedulingDisabled field in the nodes' status using the following command:
kubectl get node -l org=labex | grep SchedulingDisabled
  1. Uncordon the nodes that have the specified label using the following command:
kubectl uncordon -l org=labex
  1. Verify that the nodes have been uncordoned by checking the SchedulingDisabled field in the nodes' status using the following command:
kubectl get node -l org=labex | grep SchedulingDisabled

Summary

In this lab, we explored how to use the cordon and uncordon commands in Kubernetes to control the scheduling of pods on nodes. We learned how to cordoned and uncordoned nodes, even with running pods, and how to use labels to selectively cordoned and uncordoned nodes based on specific criteria. These commands are useful for managing the availability and resiliency of pods in a Kubernetes cluster.

Other Kubernetes Tutorials you may like