Examine Nodes with Kubectl

KubernetesKubernetesBeginner
Practice Now

Introduction

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ConfigurationandVersioningGroup(["`Configuration and Versioning`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/cordon("`Cordon`") kubernetes/BasicCommandsGroup -.-> kubernetes/uncordon("`Uncordon`") kubernetes/BasicCommandsGroup -.-> kubernetes/taint("`Taint`") kubernetes/BasicCommandsGroup -.-> kubernetes/annotate("`Annotate`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/ConfigurationandVersioningGroup -.-> kubernetes/label("`Label`") subgraph Lab Skills kubernetes/describe -.-> lab-9790{{"`Examine Nodes with Kubectl`"}} kubernetes/get -.-> lab-9790{{"`Examine Nodes with Kubectl`"}} kubernetes/cordon -.-> lab-9790{{"`Examine Nodes with Kubectl`"}} kubernetes/uncordon -.-> lab-9790{{"`Examine Nodes with Kubectl`"}} kubernetes/taint -.-> lab-9790{{"`Examine Nodes with Kubectl`"}} kubernetes/annotate -.-> lab-9790{{"`Examine Nodes with Kubectl`"}} kubernetes/apply -.-> lab-9790{{"`Examine Nodes with Kubectl`"}} kubernetes/label -.-> lab-9790{{"`Examine Nodes with Kubectl`"}} end

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

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

  2. To get more detailed information about a specific node, run the following command:

    kubectl describe node minikube

    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

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

  2. To add a label to a node, run the following command:

    kubectl label node minikube org=labex
  3. To add an annotation to a node, run the following command:

    kubectl annotate node minikube environment=production
  4. Use the following command to check the labels on the node:

    kubectl get nodes --show-labels

    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

    This will display the taints for the specified node.

  2. To add a taint to a node, run the following command:

    kubectl taint node minikube app=backend:NoSchedule
  3. 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

    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"

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

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

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

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

kubectl uncordon minikube

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.

Other Kubernetes Tutorials you may like