Created
July 19, 2019 08:34
-
-
Save eshleebien/7214a2e0afe4f0698ba31e5009392b5b to your computer and use it in GitHub Desktop.
kubectl drain using kubernetes api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| HOSTNAME=`curl -s http://169.254.169.254/latest/meta-data/local-hostname` | |
| SERVER=$1 | |
| TOKEN=$2 | |
| curl -s --insecure -k -v -H "Accept: application/json" -H "Authorization: Bearer $TOKEN" -XPATCH -d '{"spec":{"unschedulable":true}}' -H "Content-Type: application/merge-patch+json" $SERVER/api/v1/nodes/$HOSTNAME | |
| for each in $(curl -s -k -H "Authorization: Bearer $TOKEN" --insecure -XGET $SERVER/api/v1/namespaces | jq -r '.items[].metadata.name'); | |
| do arr=($(curl -s -k -H "Authorization: Bearer $TOKEN" --insecure -XGET $SERVER/api/v1/namespaces/$each/pods | jq --arg node $HOSTNAME -r '.items[] | select(.spec.nodeName == $node) | .metadata.name')); | |
| for i in ${arr[@]}; | |
| do curl -s --insecure -k -H "Authorization: Bearer $TOKEN" -XDELETE $SERVER/api/v1/namespaces/$each/pods/$i | grep "nodeName"; | |
| done | |
| done | |
| # Sleep 40 seconds because of default 30 seconds graceful period | |
| sleep 40 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This cordons the node and drain its pods
Usage: