Last active
October 1, 2019 14:40
-
-
Save toopay/22546f0c9974394697785b9a6d77d1e7 to your computer and use it in GitHub Desktop.
drain.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
concurrency_count=2 | |
# Ensure to have correct current-context on your kubeconfig | |
KUBECONFIG=~/.kube/config | |
nodes_draining(){ | |
ps -ef |grep "[k]ubectl drain" | |
return $? | |
} | |
wait_for_replace_confirmation(){ | |
confirmation=true | |
while $confirmation | |
do read -r -p "Please replace nodes and confirm completion [Y/yes]" input | |
case $input in | |
[yY][eE][sS]|[yY]) | |
echo "Replace confirmed. Proceeding" | |
confirmation=false | |
;; | |
*) | |
echo "Invalid input..." | |
;; | |
esac | |
done | |
} | |
node_private_ips(){ | |
private_ips=() | |
for node in "${active_rolling_nodes[@]}"; do | |
internal_ip="$(kubectl --kubeconfig=${KUBECONFIG} describe node $node |grep InternalIP |awk '{print $2}')" | |
private_ips=("${private_ips[@]}" "$internal_ip") | |
done | |
echo ${private_ips[@]} | |
} | |
roll_nodeset(){ | |
echo "Draining Nodes: ${active_rolling_nodes[@]}" | |
while nodes_draining; do | |
echo "Waiting on draining nodes..." | |
sleep 5 | |
done | |
echo "Please Execute Node Replace: $(node_private_ips)" | |
wait_for_replace_confirmation | |
echo "Uncordoning ${#active_rolling_nodes[@]} nodes" | |
} | |
kube_nodes=($(kubectl --kubeconfig=${KUBECONFIG} get nodes |tail -n +2 |grep -v master |awk '{print $1}')) | |
active=1 | |
active_rolling_nodes=() | |
for node in "${kube_nodes[@]}"; do | |
echo "CurrentNode: ${node}" | |
if [[ $active < $concurrency_count ]]; then | |
kubectl --kubeconfig=${KUBECONFIG} drain --ignore-daemonsets ${node} & | |
active_rolling_nodes=("${active_rolling_nodes[@]}" "$node") | |
active=$(expr $active + 1) | |
else | |
kubectl --kubeconfig=${KUBECONFIG} drain --ignore-daemonsets ${node} | |
active_rolling_nodes=("${active_rolling_nodes[@]}" "$node") | |
roll_nodeset $active_rolling_nodes | |
active_rolling_nodes=() | |
active=1 | |
fi | |
done | |
roll_nodeset $active_rolling_nodes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment