Redistribute pods after adding a node in Kubernetes
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In a Kubernetes cluster, efficient resource allocation is crucial for maintaining optimal performance. The addition of a new node to a cluster offers more resources, which necessitates the redistribution of pods to leverage these fresh capacities. This redistribution helps prevent overutilized nodes from throttling application performance and encourages better resource usage across the cluster.
Understanding Kubernetes Scheduler and Pod Distribution
Role of the Scheduler
The Kubernetes scheduler is a core component responsible for placing pods on nodes based on resource requirements, policy constraints, and other criteria set by the cluster administrator. When new nodes are added to a cluster, the scheduler re-evaluates the available resources and makes decisions to optimize pod placement.
The Need for Pod Redistribution
While Kubernetes dynamically balances workloads, the automatic redistribution of existing pods does not occur by default upon adding a new node. The existing pods remain on their nodes unless manually intervened, potentially leading to non-optimal resource distribution.
Steps for Redistributing Pods
Redistributing pods across newly added nodes can be accomplished manually or through automated scripts. Here is a general approach:
Manual Pod Eviction
- Cordon the overloaded node so the scheduler stops placing new pods there:
- Drain the node to evict workloads and let controllers reschedule them:
- Delete specific controller-managed pods if needed. Their Deployment, StatefulSet, or ReplicaSet will recreate them and the scheduler can place them on the newly added node:
Automating Redistribution
- Cluster Autoscaler: Although primarily used for dynamic provisioning of nodes, combining it with custom scripts can aid in rebalancing the pods based on node load.
- Pod Disruption Budgets (PDBs): Enable safe eviction of pods by defining acceptable disruption levels, ensuring continuity of service during rebalancing.
- Custom Scripts and Applications: Automate pod redistribution by writing scripts that utilize Kubernetes’ API to perform actions like node cordon, drain, and eviction automatically.
Simulation and Validation
Test your pod redistribution strategy in a development or staging environment before applying it in production. Use observability tools such as Prometheus and Grafana to monitor the new state of the cluster and validate that load-balancing objectives are being met.
Best Practices
Resource Requests and Limits
Define accurate resource requests and limits for your pods. This ensures the Kubernetes scheduler can make informed decisions on where to place the workloads.
Regular Node Capacity Checks
Periodically assess the load on each node. Use Kubernetes metrics and logging to identify nodes that may not be optimally utilized or are experiencing resource contention.
Automated Monitoring
Implement automated monitoring to preemptively detect when nodes are over or under-utilized. This aids in deciding when manual interventions or scaling events are required.
Quick Reference
- Add the new node to the cluster and confirm it is
Ready. - Use
kubectl cordon NODE_NAMEto stop new scheduling on an overloaded node. - Use
kubectl drain NODE_NAME --ignore-daemonsets --delete-emptydir-datato evict existing pods safely. - Delete controller-managed pods with
kubectl delete pod POD_NAMEif you want them rescheduled immediately. - Validate the new distribution with
kubectl get pods -o wideplus cluster metrics from Prometheus or Grafana.
Conclusion
Adding a new node to a Kubernetes cluster presents an opportunity to improve performance and resource utilization through effective pod redistribution. While Kubernetes offers robust tools for dynamic workload management, manual intervention or scripting may be necessary to re-balance the workloads initially. Understanding the processes and best practices for redistributing pods ensures leveraging the full potential of a scalable and responsive Kubernetes environment.
Related reading
- Remove Kubernetes Readiness Probe
- Rendered manifests contain a resource that already exists. Could not get information about the resource resource name may not be empty
- Renew kubernetes pki after expired
- Replace contents of an item in a list using Kustomize
- Replication Controller VS Deployment in Kubernetes
- Requeue a kubernetes event in a non-blocking reconcile loop
- Required value must specify a volume type when statically provisioning PV
- Restart container within pod

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.