Skip to content

How to drain node from local node using docker command

This document (000020373) is provided subject to the disclaimer at the end of this document.

Environment

RKE1

Rancher v2.4.x , v2.5.x

Situation

Need a way to cordon or drain node when OS patching is automated, and the automation doesn't have access to Rancher API.

Resolution

Integrate the below command in the automation to drain the node in the OS patching workflow.

docker exec kubelet bash -c 'kubectl --kubeconfig <(kubectl --kubeconfig /etc/kubernetes/ssl/kubecfg-kube-node.yaml get configmap -n kube-system full-cluster-state -o json | jq -r .data.\"full-cluster-state\" | jq -r .currentState.certificatesBundle.\"kube-admin\".config | sed -e "/^[[:space:]]*server:/ s_:.*_: \"https://127.0.0.1:6443\"_") drain $(hostname -s) --delete-local-data=true --force=true --grace-period=60 --ignore-daemonsets=true --timeout=120s'

Please note that the below flags need to be changed according to your requirements.

--delete-local-data=true
--force=true
--grace-period=60
--ignore-daemonsets=true
--timeout=120s

Cause

Draining operation using kubeconfig file " /etc/kubernetes/ssl/kubecfg-kube-node.yaml" will result in errors like below since the " system:node" role is not authorized to access needed API groups.

cannot delete daemonsets.apps "nginx-ingress-controller" is forbidden: User "system:node" cannot get resource "daemonsets" in API group "apps" in the namespace "ingress-nginx"

But this kubeconfig file can access the config map "full-cluster-state" in namespace " kube-system"  contains the kubeconfig file, which has the privilege to do the drain operation.

Disclaimer

This Support Knowledgebase provides a valuable tool for SUSE customers and parties interested in our products and solutions to acquire information, ideas and learn from one another. Materials are provided for informational, personal or non-commercial use within your organization and are presented "AS IS" WITHOUT WARRANTY OF ANY KIND.