How to gracefully remove a node from Kubernetes?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
As applications and workloads scale, it becomes crucial to manage the underlying infrastructure effectively. Kubernetes is a popular choice for container orchestration, providing tools to manage clusters of nodes with simplicity and resilience. However, there can be scenarios where it's necessary to remove a node from a Kubernetes cluster. This process must be carried out gracefully to avoid disruptions or downtime. Here’s a detailed guide on how to gracefully remove a node from a Kubernetes cluster.
Key Concepts and Preparations
Before diving into the process, it's essential to understand some key concepts and preparatory steps:
- Node: A node is a worker machine in Kubernetes, which could be a physical machine or a virtual machine. Each node runs Pods, which are the smallest deployable units of computing that you can create and manage in Kubernetes.
- Cordoning: This is the first step towards gracefully removing a node. Cordoning a node means marking it as unschedulable so that no new Pods can be scheduled on that node.
- Draining: This step involves evicting all Pods from the node. The process ensures that any workload on the node is rescheduled onto other nodes, without loss of data or disruption.
- Kubelet and Node Registration: The kubelet is an agent that runs on each node in the cluster, ensuring that the containers are running in a Pod. Nodes are automatically registered with the cluster, and they need to be deregistered when removed.
Steps to Gracefully Remove a Node
1. Cordoning the Node
Cordoning the node prevents new Pods from being scheduled onto it. Use the following command:
Check the status:
This command will mark the node as SchedulingDisabled.
2. Draining the Node
Draining will evict the Pods from the node safely:
--ignore-daemonsets: Does not evict DaemonSets managed Pods.--delete-emptydir-data: Deletes Pods using emptyDir; you might want to exclude this if data retention is necessary.
Ensure no pending Pods:
3. Removing the Node from Cluster
Once all the Pods have been safely evicted and terminated, remove the node from the cluster:
This command will deregister the node from the cluster.
4. Decomission Node
After removing the node from Kubernetes, ensure that you properly decommission it from the underlying infrastructure, whether it's a cloud provider or an on-premises server.
Technical Considerations
- Pod Disruption Budgets (PDBs): They define how voluntary disruptions are managed for Pods. Ensure PDBs are configured to avoid breaking service-level objectives (SLOs).
- Persistent Volumes: If your Pods use Persistent Volumes, consider the impact of moving Pods on data persistence. Use appropriate storage classes or claims to facilitate smooth transitions.
- DaemonSets: Nodes with DaemonSets will restart Pods across remaining nodes; drain operations are regularly ignored for DaemonSets.
Summary Table
Here is a summary of the key steps and considerations while removing a node:
| Step | Command | Considerations |
| Cordon Node | kubectl cordon <node-name> | Ensures no new Pods are scheduled on the node |
| Drain Node | kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data kubectl get pods --all-namespaces -o wide | grep <node-name> | Carefully evict Pods and respect Pod Disruption Budgets (PDBs) |
| Delete Node | kubectl delete node <node-name> | Remove node from the Kubernetes cluster registry |
| Decommission Node | - | Properly decommission from physical or cloud infrastructure |
Conclusion
Gracefully removing a node from a Kubernetes cluster ensures that workloads continue to function smoothly and that there's minimal service disruption. It's crucial to understand each step and its implications, especially in large-scale environments where the failure of one service could have cascading impacts. Making use of features like Pod Disruption Budgets and understanding Persistent Volume Claims will further help in maintaining service reliability.
Related reading
- How to handle database migrations with Kubernetes and Skaffold
- How to handle S3 events inside a Kubernetes Cluster?
- How to identify schedulable nodes in Kubernetes
- How to identify the storage space left in a persistent volume claim?
- How to handle a situation of feature scaling in machine learning model deployment when you have only one testing instance?
- How to handle data migrations in distributed microservice databases
- How to import a generated Kubernetes cluster's namespace in terraform
- How to improve random number generation in kubernetes cluster containers?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.