Skip to content

Instantly share code, notes, and snippets.

@drubin
Last active May 3, 2022 14:21
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drubin/c3e2131ada657b6bcb4f1ac64d789c87 to your computer and use it in GitHub Desktop.
Save drubin/c3e2131ada657b6bcb4f1ac64d789c87 to your computer and use it in GitHub Desktop.
Cordon and Drain GKE Node pools
#!/bin/bash -e
# Cordons and drains a node pool
display_usage() {
echo "Cordons and drains a nodepool"
echo -e "\nUsage:\n ./cordon-drain-pool [nodepool-name] "
echo -e " ./cordon-drain-pool pool-1 \n"
}
if [ $# -le 1 ]
then
display_usage
exit 1
fi
NODE_POOL=$1
NODES=$(kubectl get nodes -l cloud.google.com/gke-nodepool="${NODE_POOL}" -o=name)
echo "WARNING: The following script will cordon and then drain nodes from ${NODE_POOL}"
echo "The following nodes will be cordoned & drained"
echo ""
for node in $NODES; do
echo "$node"
done
echo ""
while true; do
read -p "Do you wish to continue? " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit 1;;
* ) echo "Please answer yes or no.";;
esac
done
echo "First cordoning nodes"
for node in $NODES; do
kubectl cordon "${node}";
done
echo "Cordoning finished"
echo "Waiting for 2 seconds to allow CLI output to update"
sleep 2
echo "Draining nodes"
for node in $NODES; do
kubectl drain "${node}" --force=true --delete-local-data=true --ignore-daemonsets;
done
@Sliuza
Copy link

Sliuza commented May 3, 2022

This is script only work for me changing -le in line 9 for -lt. Besides that, good job!

@drubin
Copy link
Author

drubin commented May 3, 2022

hi @Sliuza awesome :)

From the docs

num1 -le num2                   checks if 1st number is less than or equal to 2nd number
num1 -lt num2                   checks if 1st  number  is less than 2nd number

I am not sure why it wouldn't work for you but great stuff :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment