How to Add and Manage Nodes in a Kubernetes Cluster

Updated: January 30, 2024 By: Guest Contributor Post a comment

Introduction

Kubernetes has become the de facto standard for orchestrating containerized applications. Managing nodes, which are the workhorses of a Kubernetes cluster, is crucial for maintaining a resilient and scalable system. This guide will walk you through the basics of adding and managing nodes in a Kubernetes cluster, with practical code examples to help you understand each step. Whether you are a novice or an experienced user, you will find this guide useful.

Prerequisites

  • A running Kubernetes cluster
  • kubectl configured with cluster access

Adding a Node to the Cluster

Nodes in Kubernetes can be virtual or physical machines. Before you add a node, ensure that it satisfies the minimum requirements to run Kubernetes.

Step 1: Prepare the Node

   # Install Docker or a container runtime
   $ sudo apt-get update
   $ sudo apt-get install docker.io

   # Install kubeadm, kubelet, and kubectl
   $ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
   $ cat <

Step 2: Join the Cluster

After preparing the node, use the kubeadm join command provided by the cluster setup output or by your cluster administrator to join the node to your Kubernetes cluster.

   # Join the cluster
   $ kubeadm join --token <token> <control-plane-host>:<port> --discovery-token-ca-cert-hash sha256:<hash>

Listing Nodes

Once you have joined the new nodes to the cluster, you can list all nodes using the kubectl command.

   # Get a list of all nodes in the cluster
   $ kubectl get nodes

Managing Node Resources

It is often necessary to understand the resources available on your nodes. This can help in scheduling decisions and troubleshooting.

   # Describe resources on a node
   $ kubectl describe node <node-name>

Cordoning and Draining Nodes

Before performing maintenance on a node or removing it, you can mark it unschedulable, preventing new pods from being scheduled on it.

   # Mark the node as unschedulable
   $ kubectl cordon <node-name>

If necessary, existing pods can be safely evicted from the node.

   # Drain pods from the node (respecting PodDisruptionBudgets)
   $ kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data

Updating Nodes

Occasionally, you may need to update the software on your nodes. Here’s how to proceed with an update using kubeadm.

Step 1: Drain the Node

   $ kubectl drain <node-name> --ignore-daemonsets

Step 2: Upgrade the Node

   # Upgrade kubeadm
   $ sudo apt-get update
   $ sudo apt-get install -y --allow-change-held-packages kubeadm

   # Plan the upgrade
   $ kubeadm upgrade plan

   # Upgrade nodes
   $ kubeadm upgrade node

   # Upgrade kubelet and kubectl
   $ sudo apt-get update
   $ sudo apt-get install -y --allow-change-held-packages kubelet kubectl
   $ sudo systemctl restart kubelet

Decommissioning a Node

When you need to remove a node from the cluster, whether for maintenance or decommissioning, you first drain it and then remove it from the cluster.

   # Drain the node
   $ kubectl drain <node-name> --delete-local-data --force --ignore-daemonsets

   # Remove the node
   $ kubectl delete node <node-name>

Monitoring Node Health

Monitoring the health of nodes is critical for cluster resilience. Kubernetes provides node health checks out of the box, but you can also use third-party tools.

   # Check node conditions
   $ kubectl get nodes -o=jsonpath='{.items[*].status.conditions[?(@.type=="Ready")]}'

Applying Taints and Tolerations

Taints and tolerations are mechanisms that allow you to control which pods can schedule on which nodes.

   # Add a taint to a node
   $ kubectl taint nodes <node-name> key=value:taint-effect

Tolerations are added to pods and allow them to schedule on tainted nodes.

   # Example YAML with a toleration
   apiVersion: v1
   kind: Pod
   metadata:
     name: mypod
   spec:
     tolerations:
     - key: "key"
       operator: "Equal"
       value: "value"
       effect: "NoSchedule"
     containers:
     - name: mycontainer
       image: myimage

Conclusion

In this guide, you learned how to add, list, update, and remove nodes in a Kubernetes cluster, along with various resource management and scheduling strategies. By ensuring the proper management of nodes, you can maintain an efficient, reliable, and high-performing Kubernetes environment.