Interview Prep/Python for DevOps/Kubernetes, Docker, SSH & fleet automation

You must drain and replace 50 Kubernetes nodes one at a time with Python, respecting PodDisruptionBudgets. How?

AdvancedScenarioKubernetes, Docker, SSH & fleet automation
Answer

Situation read → Controlled rolling node replacement.

What I'd do →

  1. For each node: cordon (patch_node(name, {"spec": {"unschedulable": True}})).
  2. Evict pods with the Eviction API (create_namespaced_pod_eviction), skipping DaemonSet and mirror pods. A 429 response means a PDB is blocking; back off and retry.
  3. Wait until the node has no evictable pods, then terminate the instance (ASG terminate_instance_in_auto_scaling_group with ShouldDecrementDesiredCapacity=False).
  4. Wait for the replacement node to be Ready before continuing.
  5. Prefer managed node group updates or Karpenter drift, which do this already.

Why → The Eviction API respects PDBs; deleting pods directly does not.

Interview tip → "Evict, don't delete" is the key line.

All Python for DevOps questions Roadmap: Python for automation

More on Kubernetes, Docker, SSH & fleet automation