-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode-drain.sh
148 lines (125 loc) · 3.19 KB
/
node-drain.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
# Script to automate node draining with RKE/k3s
sherlock() {
if [ -n "${RUNTIME_FLAG}" ]
then
echo "Setting container runtime as ${RUNTIME_FLAG}"
RUNTIME="${RUNTIME_FLAG}"
else
echo -n "Detecting container runtime... "
if $(command -v docker >/dev/null 2>&1)
then
if $(docker ps >/dev/null 2>&1)
then
RUNTIME=docker
echo "docker"
else
FOUND="docker "
fi
fi
if $(command -v k3s >/dev/null 2>&1)
then
if $(k3s crictl ps >/dev/null 2>&1)
then
RUNTIME=k3s
echo "k3s"
else
FOUND+="k3s"
fi
fi
if [ -z "${RUNTIME}" ]
then
echo -e "\n couldn't detect container runtime"
if [ -n "${FOUND}" ]
then
echo "Found ${FOUND} but could not execute commands successfully"
fi
fi
fi
}
setup() {
case "${RUNTIME}" in
docker)
KUBECTL_COMMAND="docker exec kubelet kubectl --kubeconfig=/etc/kubernetes/ssl/kubecfg-kube-node.yaml"
;;
k3s)
KUBECTL_COMMAND="/usr/local/bin/kubectl --kubeconfig=/var/lib/rancher/k3s/agent/kubelet.kubeconfig"
;;
esac
}
node_drain() {
echo "Finding node name"
NODE_NAME=$(${KUBECTL_COMMAND} get nodes -l kubernetes.io/hostname=$(hostname -s) -o=jsonpath='{.items[0].metadata.name}')
echo "Draining node"
if [ -z "${KUBECTL_COMMAND}" ]
then
echo "I need a kubectl command, something went wrong, sorry!"
exit 1
fi
${KUBECTL_COMMAND} drain ${NODE_NAME} --ignore-daemonsets ${DELETE_LOCAL_DATA} --timeout=100s --force
}
node_delete() {
echo "Deleting node"
${KUBECTL_COMMAND} delete node "${NODE_NAME}"
sleep 2
echo "Verifying node is deleted"
${KUBECTL_COMMAND} get node "${NODE_NAME}"
status=$?
count=0
while [ "${status}" -eq 0 ]
do
sleep 2
((count++))
if [ "${count}" -ge 5 ]
then
echo "Node is still in the cluster, using --force and exiting"
${KUBECTL_COMMAND} delete node "${NODE_NAME}" --force --grace-period=0
break
fi
${KUBECTL_COMMAND} get node "${NODE_NAME}"
status=$?
done
}
help() {
echo "node-drain systemd service for RKE and k3s
Usage: bash node-drain.sh [ -d -n -r <container runtime> ]
All flags are optional:
-d Delete local data, pods using emptyDir volumes will be drained as well
-n Delete node as well, useful for immutable infrastructure as nodes are replaced on shutdown
-r Override container runtime if not automatically detected (docker|k3s)"
}
# Check if we're running as root.
if [[ $EUID -ne 0 ]]
then
echo "This script must be run as root"
exit 1
fi
while getopts "dhnr:" opt; do
case $opt in
d)
DELETE_LOCAL_DATA="--delete-local-data"
;;
h)
help; exit 0
;;
n)
DELETE_NODE="true"
;;
r)
RUNTIME_FLAG="${OPTARG}"
;;
:)
echo "Option -$OPTARG requires an argument."
exit 1
;;
*)
help && exit 0
esac
done
sherlock
setup
node_drain
if [ "${DELETE_NODE}" == "true" ]
then
node_delete
fi