The Wild

Kubernetes: Migrate Pods Automatically to Another Node

· 5 min read · Mammals

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to automatically move pods from one node to another in Kubernetes, typically for maintenance, workload balancing, or node draining. You'll use built-in controllers like Deployments and StatefulSets, plus the kubectl drain command, which safely evicts pods and reschedules them onto healthy nodes. Before starting, you need a working Kubernetes cluster with at least two nodes, kubectl configured, and sufficient resources on the target nodes.

What You'll Need

  • A Kubernetes cluster with 2+ nodes (worker nodes)
  • kubectl installed and configured to access your cluster
  • Permissions to drain nodes (cluster-admin or equivalent)
  • Your workloads running as Deployments, StatefulSets, or ReplicaSets (not bare pods)
  • Target nodes with enough CPU/memory to host the migrated pods

Step-by-Step

  1. Check current pod distribution. Run kubectl get pods -o wide to see which nodes your pods are on. Identify the node you want to migrate pods away from (e.g., for maintenance).
  2. Verify your workloads are managed by a controller. Bare pods (not part of a ReplicaSet/Deployment) will not be rescheduled. Use kubectl get deployments and kubectl get statefulsets to confirm. If you have bare pods, recreate them as Deployments first.
  3. Cordon the node (optional but recommended). Run kubectl cordon <node-name>. This marks the node as unschedulable, preventing new pods from landing there while you drain it.
  4. Drain the node. Run kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data. The --ignore-daemonsets flag keeps DaemonSet pods (they’re managed separately), and --delete-emptydir-data is required if any pod uses emptyDir volumes. This evicts pods gracefully, respecting PodDisruptionBudgets.
  5. Watch the migration. In another terminal, run kubectl get pods -o wide -w to see pods terminating on the old node and being recreated on other nodes. The controller (e.g., Deployment) automatically creates replacement pods on available nodes.
  6. Wait for all pods to become Running. Use kubectl get pods -o wide and confirm every pod is in Running state on a different node. If any pod is stuck in Pending, check resource availability on other nodes.
  7. Uncordon the node (when ready). After maintenance, run kubectl uncordon <node-name> to allow new pods to be scheduled there again. Note: existing pods will not automatically move back unless you delete them or scale down/up.
  8. Optional: force migration without draining. If you want to move pods without cordoning, you can delete pods manually: kubectl delete pod <pod-name>. The controller recreates them on another node. This is less graceful and may cause downtime.

Common Mistakes

  • Draining without --ignore-daemonsets: DaemonSet pods will block the drain unless you add this flag, causing the drain to hang.
  • Forgetting --delete-emptydir-data: If any pod uses emptyDir, the drain will fail with an error. Only use this flag if you accept losing that temporary data.
  • Draining a node with a single replica: If you have only one replica of a Deployment, draining will cause downtime because there’s no other node to run it. Ensure you have enough replicas or accept the outage.
  • Not checking PodDisruptionBudgets: If a PDB allows zero disruptions, the drain will hang. Check with kubectl get pdb and adjust if needed.
  • Draining the control-plane node: Draining a control-plane node can break cluster management. Only drain worker nodes unless you know what you’re doing.

Tips

  • Use kubectl drain --dry-run=client to preview what would be evicted without actually doing it.
  • Set a terminationGracePeriodSeconds in your pod spec to allow graceful shutdown of your application.
  • Use podAntiAffinity to spread pods across nodes, making migration smoother and improving resilience.
  • Automate node drains with a CI/CD pipeline or a cron job if you do regular maintenance.
  • Monitor cluster health with kubectl get events to see why pods might be stuck.

Troubleshooting

  • Drain hangs forever: Check for pods that are not evicting. Run kubectl get pods -o wide | grep <node-name> and look for pods with Terminating status. Check events with kubectl describe pod <pod-name> for errors like PDB violations or volume detach issues.
  • Pods stuck in Pending after drain: This means no node has enough resources. Check node capacity with kubectl describe node and free up resources or add nodes.
  • Pods keep landing on the drained node: If you didn’t cordon the node first, new pods might schedule there. Cordon before draining.
  • DaemonSet pods are not migrating: DaemonSets are tied to nodes; they will not move. That’s expected. If you need them off, you must delete the DaemonSet temporarily.
  • StatefulSet pods lose data: If using local volumes, data is bound to the node. Draining will not move the data. Consider using network storage (e.g., NFS, cloud volumes) for stateful workloads.

Frequently Asked Questions

Will draining a node automatically move my pods?

Yes, for pods managed by a controller (Deployment, StatefulSet, ReplicaSet). The controller recreates them on other nodes. Bare pods are not recreated.

What happens to pods with emptyDir volumes?

They are evicted and the emptyDir data is deleted. The pod is recreated on another node with a fresh emptyDir. You must use --delete-emptydir-data to allow the drain.

Can I migrate pods without downtime?

Yes, if you have multiple replicas and a PodDisruptionBudget that allows at least one disruption. The drain will evict pods one by one, keeping the service available.

How do I move pods back after maintenance?

Uncordon the node, then delete the pods you want to move. The controller will recreate them, and they may land on the uncordoned node. Alternatively, scale down and up the Deployment.

Does draining a node affect the cluster?

No, if you have other nodes. The control plane remains intact. Draining only affects the workloads on that specific node.

Can I automate this process?

Yes, you can write a script or use tools like kubectl drain in a CI/CD pipeline. For scheduled maintenance, consider using a Kubernetes CronJob that runs the drain command.