Interview Prep/Ansible/Rolling Updates, Delegation and Safe Deployments

How do you drain a Kubernetes node as part of a patching play?

IntermediateProductionRolling Updates, Delegation and Safe Deployments
Answer

The kubernetes.core collection has k8s_drain, which cordons the node and evicts pods, honouring PodDisruptionBudgets.

- kubernetes.core.k8s_drain:
    state: drain
    name: "{{ inventory_hostname }}"
    delete_options:
      ignore_daemonsets: true
      delete_emptydir_data: true
      wait_timeout: 600
  delegate_to: localhost

- ansible.builtin.reboot:

- kubernetes.core.k8s_drain:
    state: uncordon
    name: "{{ inventory_hostname }}"
  delegate_to: localhost

Run with serial: 1 (or small batches) so PDBs and capacity are respected. Node names in Kubernetes must match what you pass to name.

All Ansible questions Roadmap: Ansible

More on Rolling Updates, Delegation and Safe Deployments