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.
More on Rolling Updates, Delegation and Safe Deployments
How do you do a rolling update with Ansible?BeginnerWhat forms can `serial` take?IntermediateShow the standard pattern for a zero-downtime rolling deploy behind a load balancer.IntermediateWhy do you add a health check inside the deployment play?BeginnerHow do `any_errors_fatal` and `max_fail_percentage` help a rollout?BeginnerHow do you do a canary deployment?Intermediate