Skip to content

Instantly share code, notes, and snippets.

@juliohm1978
Last active March 13, 2024 14:12
Show Gist options
  • Save juliohm1978/fcfd21b26f9431c01978 to your computer and use it in GitHub Desktop.
Save juliohm1978/fcfd21b26f9431c01978 to your computer and use it in GitHub Desktop.
Drains a Kubernetes node using "rollout restart" instead "kubectl drain". See comments for motiviation and usage.
#!/bin/bash
NODE_NAME=$1
ROLLOUT_CMD=$2
if [[ "$NODE_NAME" == "" ]]; then
echo "
USAGE: ./drain.sh <NODE_NAME>
Drains a node from its Deployments/Stateful set pods.
Examples:
# List Deploy/Sts objects that have pods running on the given node
./drain.sh NODE_NAME
# Perform the actual rollout restart
./drain.sh NODE_NAME restart
Pre-requisite: Kubernetes 1.15.x or newer.
"
exit 1
fi
function rollout_restart() {
## Object types to be processed
## This could be deployment ou satefulset.
OBJTYPE=$1
## Loop through all objects of type $OBJTYPE.
## Splits their names and namespaces.
for dp in $(kubectl get $OBJTYPE -A --no-headers | awk '{print $1 "|" $2}'); do
NAMESPACE=$(echo $dp | sed 's/|.*//')
DEPLOY=$(echo $dp | sed 's/.*|//')
## For each Deploy/Sts, acquire the SELECTOR used to select its pods.
SELECTOR=$(kubectl get $OBJTYPE --no-headers -owide -n $NAMESPACE $DEPLOY -owide | awk '{print $8}')
## Using SELECTOR, list all pods from the Deploy/Sts running in the target node
PODLIST=$(kubectl get pod -owide --no-headers -n $NAMESPACE -l "$SELECTOR" | grep $NODE_NAME | awk '{print $1}')
## If we have pods running in NODE_NAME, act accordingly
if [[ "$PODLIST" != "" ]]; then
echo "=== $OBJTYPE $NAMESPACE/$DEPLOY ==="
echo $PODLIST | sed 's/ /\n/g'
if [[ "$ROLLOUT_CMD" != "" ]]; then
echo ">> Rollout restart..."
set -x
kubectl rollout $ROLLOUT_CMD -n $NAMESPACE $OBJTYPE/$DEPLOY
set +x
fi
echo
fi
done
}
if [[ "$ROLLOUT_CMD" == "restart" ]]; then
set -x
kubectl cordon $NODE_NAME
set +x
fi
rollout_restart deploy
rollout_restart sts
@DominicWatson
Copy link

This is brilliant and perfect for our setup. Thank you @juliohm1978 !

Is there a typo on line 61 though? kubetl should be kubectl (I changed to kubectl locally and working fine).

@juliohm1978
Copy link
Author

Yes, thanks!
Fixed line 61 👍

@YLcoding
Copy link

Brilliant.
You should make a PR to make this available in k8s API server!

@s7an-it
Copy link

s7an-it commented Apr 7, 2023

Great, was wondering the same thing and this confirmed it, how come doesn't exist as functionality.

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