Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement