Skip to content

Instantly share code, notes, and snippets.

@hobbsh
Last active August 10, 2018 23:09
Show Gist options
  • Save hobbsh/a0ba3e92deaea3148c21e6ca9d765cda to your computer and use it in GitHub Desktop.
Save hobbsh/a0ba3e92deaea3148c21e6ca9d765cda to your computer and use it in GitHub Desktop.
Drain kubernetes nodes that are part of an older autoscaling group
#!/bin/bash -e
# As mentioned here: https://thinklumo.com/blue-green-node-deployment-kubernetes-eks-terraform
# Author: Wylie Hobbs - 2018
# usage:
# normal: ./drain_old_nodes.sh
# dry run: ./drain_old_nodes.sh noop
DRY_RUN="$1"
function drain(){
if [ "$DRY_RUN" != "" ]; then
echo "Dry run specified - just echo'ing what would happen..."
echo "kubectl cordon $1"
echo "kubectl drain $1 --ignore-daemonsets=true --delete-local-data --force"
else
kubectl cordon $1
kubectl drain $1 --ignore-daemonsets=true --delete-local-data
fi
if [ $? -eq 0 ]; then
echo "Node $1 drained - sleeping for 60 seconds"
sleep 60
else
echo "Failed to drain $1 - exiting"
exit 1
fi
}
#Get the AGE of the oldest node group to target for draining
AGE=$(kubectl get no --no-headers=true --sort-by=.metadata.creationTimestamp | awk '{print $4}' | head -n 1)
#Not a great way of getting the nodes to drain based on grepping the AGE - this could break if there's a match in the node name"
NODES=$(kubectl get no | awk '{print $4" "$1}' | grep $AGE | awk '{print $2}')
#Confirm the nodes to drain because the above command is weak
echo -e "Going to drain \n${NODES} \n\nWould you like to proceed (Y/N)?"
read proceed
if [ "$proceed" == "Y" ]; then
echo "Waiting 10 seconds to drain nodes, in case you pressed the wrong key..."
sleep 10
if [ "$AGE" != "" ]; then
for NODE in ${NODES[@]}; do
if [ "$NODE" != "" ]; then
drain $NODE
else
echo "Node name is empty. Doing nothing"
fi
done
else
echo "Could not determine age of oldest nodes - exiting"
exit 1
fi
else
echo "You elected to not proceed with the draining - your input was: '$proceed'. Maybe you meant 'Y'? Exiting."
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment