Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #!/bin/bash
  2. concurrency_count=2
  3. # Ensure to have correct current-context on your kubeconfig
  4. KUBECONFIG=~/.kube/config
  5.  
  6. nodes_draining(){
  7. ps -ef |grep "[k]ubectl drain"
  8. return $?
  9. }
  10.  
  11. wait_for_replace_confirmation(){
  12. confirmation=true
  13. while $confirmation
  14. do read -r -p "Please replace nodes and confirm completion [Y/yes]" input
  15.  
  16. case $input in
  17. [yY][eE][sS]|[yY])
  18. echo "Replace confirmed. Proceeding"
  19. confirmation=false
  20. ;;
  21. *)
  22. echo "Invalid input..."
  23. ;;
  24. esac
  25. done
  26. }
  27.  
  28. node_private_ips(){
  29. private_ips=()
  30. for node in "${active_rolling_nodes[@]}"; do
  31. internal_ip="$(kubectl --kubeconfig=${KUBECONFIG} describe node $node |grep InternalIP |awk '{print $2}')"
  32. private_ips=("${private_ips[@]}" "$internal_ip")
  33. done
  34. echo ${private_ips[@]}
  35. }
  36.  
  37. roll_nodeset(){
  38. echo "Draining Nodes: ${active_rolling_nodes[@]}"
  39. while nodes_draining; do
  40. echo "Waiting on draining nodes..."
  41. sleep 5
  42. done
  43. echo "Please Execute Node Replace: $(node_private_ips)"
  44. wait_for_replace_confirmation
  45. echo "Uncordoning ${#active_rolling_nodes[@]} nodes"
  46. }
  47.  
  48. kube_nodes=($(kubectl --kubeconfig=${KUBECONFIG} get nodes |tail -n +2 |grep -v master |awk '{print $1}'))
  49. active=1
  50. active_rolling_nodes=()
  51. for node in "${kube_nodes[@]}"; do
  52. echo "CurrentNode: ${node}"
  53. if [[ $active < $concurrency_count ]]; then
  54. kubectl --kubeconfig=${KUBECONFIG} drain --ignore-daemonsets ${node} &
  55. active_rolling_nodes=("${active_rolling_nodes[@]}" "$node")
  56. active=$(expr $active + 1)
  57. else
  58. kubectl --kubeconfig=${KUBECONFIG} drain --ignore-daemonsets ${node}
  59. active_rolling_nodes=("${active_rolling_nodes[@]}" "$node")
  60. roll_nodeset $active_rolling_nodes
  61. active_rolling_nodes=()
  62. active=1
  63. fi
  64. done
  65.  
  66. roll_nodeset $active_rolling_nodes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement