Skip to content

Instantly share code, notes, and snippets.

@toopay
Last active October 1, 2019 14:40
Show Gist options
  • Select an option

  • Save toopay/22546f0c9974394697785b9a6d77d1e7 to your computer and use it in GitHub Desktop.

Select an option

Save toopay/22546f0c9974394697785b9a6d77d1e7 to your computer and use it in GitHub Desktop.

Revisions

  1. toopay revised this gist Oct 1, 2019. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions drain.sh
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,7 @@
    #!/bin/bash
    concurrency_count=2
    #KUBECONFIG=$(git rev-parse --show-toplevel)/k8s/secrets/kubeconfig\.dev
    # Ensure to have correct current-context on your kubeconfig
    KUBECONFIG=~/.kube/config
    #KUBECONFIG=$(git rev-parse --show-toplevel)/kubernetes/qa/kubeconfig

    nodes_draining(){
    ps -ef |grep "[k]ubectl drain"
  2. toopay revised this gist Oct 1, 2019. 1 changed file with 13 additions and 11 deletions.
    24 changes: 13 additions & 11 deletions drain.sh
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,19 @@
    #!/bin/bash
    concurrency_count=2
    # Ensure your current-context pointing to correct env
    #KUBECONFIG=$(git rev-parse --show-toplevel)/k8s/secrets/kubeconfig\.dev
    KUBECONFIG=~/.kube/config
    #KUBECONFIG=$(git rev-parse --show-toplevel)/kubernetes/qa/kubeconfig

    nodes_draining(){
    ps -ef |grep "[k]ubectl drain"
    return $?
    }
    wait_for_reboot_confirmation(){

    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"
    @@ -24,7 +25,7 @@ wait_for_reboot_confirmation(){
    esac
    done
    }

    node_private_ips(){
    private_ips=()
    for node in "${active_rolling_nodes[@]}"; do
    @@ -33,17 +34,18 @@ node_private_ips(){
    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_reboot_confirmation
    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=()
    @@ -61,5 +63,5 @@ for node in "${kube_nodes[@]}"; do
    active=1
    fi
    done
    roll_nodeset $active_rolling_nodes

    roll_nodeset $active_rolling_nodes
  3. toopay created this gist Sep 30, 2019.
    65 changes: 65 additions & 0 deletions drain.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #!/bin/bash
    concurrency_count=2
    # Ensure your current-context pointing to correct env
    KUBECONFIG=~/.kube/config
    nodes_draining(){
    ps -ef |grep "[k]ubectl drain"
    return $?
    }
    wait_for_reboot_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_reboot_confirmation
    }
    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