Mastering Kubernetes Node Management with kubectl

labby

Labby

Posted on September 9, 2024

Mastering Kubernetes Node Management with kubectl

Introduction

This article covers the following tech skills:

Skills Graph

In this lab, you will learn how to use kubectl to examine the nodes in your Kubernetes cluster. You will start with basic node information and work your way up to more advanced topics such as taints and tolerations. This lab assumes that you have a Kubernetes cluster already set up and have kubectl installed.

Basic Node Information

The first thing you will do is get basic information about the nodes in your cluster.

  1. To view a list of nodes in your cluster, run the following command:
   kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

This will display a list of all the nodes in your cluster along with their status.

  1. To get more detailed information about a specific node, run the following command:
   kubectl describe node minikube
Enter fullscreen mode Exit fullscreen mode

Replace minikube with the name of the node you want to examine. This will give you detailed information about the node's status, capacity, and usage.

Labels and Annotations

Labels and annotations can be used to add metadata to nodes in your cluster. This metadata can be used to select nodes for specific tasks or to filter nodes based on certain criteria.

  1. To view the labels and annotations for a specific node, run the following command:
   kubectl get node minikube --show-labels=true
Enter fullscreen mode Exit fullscreen mode

This will display the labels and annotations for the specified node.

  1. To add a label to a node, run the following command:
   kubectl label node minikube org=labex
Enter fullscreen mode Exit fullscreen mode
  1. To add an annotation to a node, run the following command:
   kubectl annotate node minikube environment=production
Enter fullscreen mode Exit fullscreen mode
  1. Use the following command to check the labels on the node:
   kubectl get nodes --show-labels
Enter fullscreen mode Exit fullscreen mode

This will output a list of all the nodes in the cluster along with their labels,Nodes can be labeled to help identify their purpose or characteristics.

Taints and Tolerations

Taints and tolerations can be used to control which pods can be scheduled on which nodes in your cluster. A taint is a special label that marks a node as unsuitable for certain types of pods, and a toleration is a setting that allows a pod to be scheduled on a node with a matching taint.

  1. To view the taints for a specific node, run the following command:
   kubectl describe node minikube | grep Taints
Enter fullscreen mode Exit fullscreen mode

This will display the taints for the specified node.

  1. To add a taint to a node, run the following command:
   kubectl taint node minikube app=backend:NoSchedule
Enter fullscreen mode Exit fullscreen mode
  1. Create a toleration to a pod, run the following command:
   cat << EOF | kubectl apply -f -
   apiVersion: v1
   kind: Pod
   metadata:
     name: my-pod
   spec:
     containers:
       - name: my-container
         image: nginx
     tolerations:
       - key: app
         operator: Exists
         effect: NoSchedule
   EOF
Enter fullscreen mode Exit fullscreen mode

This pod uses app as the name of the taint and NoSchedule as the effect the taint should have.

View Node Capacity and Resource Usage

To view the available resources on a node, use the following command:

kubectl describe node minikube | grep -A 8 "Allocated resources"
Enter fullscreen mode Exit fullscreen mode

Replace minikube with the name of the node you want to examine.

This will provide detailed information about the node, including its capacity and current resource usage.

View Node Events

In Kubernetes, you can use the following command to filter all events related to a specific node:

kubectl get events --field-selector involvedObject.kind=Node,involvedObject.name=minikube
Enter fullscreen mode Exit fullscreen mode

Replace minikube with the name of the node you want to query. This command will list all events related to that node, such as restarts, upgrades, and so on.

Cordon and Uncordon a Node

In some cases, you may need to take a node out of rotation for maintenance or other reasons. Kubernetes provides a way to mark a node as unschedulable so that no new pods are scheduled on it. This is called "cordon".

To cordon a node, use the following command:

kubectl cordon minikube
Enter fullscreen mode Exit fullscreen mode

Replace minikube with the name of the node you want to cordon.

Then Use the following command to check the node status:

kubectl get node
Enter fullscreen mode Exit fullscreen mode

To uncordon a node and allow new pods to be scheduled on it, use the following command:

kubectl uncordon minikube
Enter fullscreen mode Exit fullscreen mode

Replace minikube with the name of the node you want to uncordon.

Note that cordoning a node does not automatically move any existing pods off the node. You should manually delete or move the pods before cordoning the node to avoid any disruption.

Congratulations, you have learned how to cordon and uncordon a node in Kubernetes.

Summary

In this lab, you learned how to examine nodes in a Kubernetes cluster using kubectl. You learned how to list nodes, check their status, view their labels, and inspect their capacity and resource usage. You also learned how to drain and uncordon nodes for maintenance and upgrades.

MindMap


🚀 Practice Now: Examine Nodes with Kubectl


Want to Learn More?

💖 💪 🙅 🚩
labby
Labby

Posted on September 9, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related