Skip to content

Instantly share code, notes, and snippets.

@jamesdube
Created February 5, 2020 09:48
Show Gist options
  • Save jamesdube/55a4ef09e65c355a3ddb4022d8f0d9f2 to your computer and use it in GitHub Desktop.
Save jamesdube/55a4ef09e65c355a3ddb4022d8f0d9f2 to your computer and use it in GitHub Desktop.
Drain Pods on Crashed Node
#!/bin/sh
KUBECTL="/usr/local/bin/kubectl"
# Get only nodes which are not drained yet
NOT_READY_NODES=$($KUBECTL get nodes | grep -P 'NotReady(?!,SchedulingDisabled)' | awk '{print $1}' | xargs echo)
# Get only nodes which are still drained
READY_NODES=$($KUBECTL get nodes | grep '\sReady,SchedulingDisabled' | awk '{print $1}' | xargs echo)
echo "Unready nodes that are undrained: $NOT_READY_NODES"
echo "Ready nodes: $READY_NODES"
for node in $NOT_READY_NODES; do
echo "Node $node not drained yet, draining..."
$KUBECTL drain --ignore-daemonsets --force $node
echo "Done"
done;
for node in $READY_NODES; do
echo "Node $node still drained, uncordoning..."
$KUBECTL uncordon $node
echo "Done"
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment