Created
August 21, 2020 15:20
-
-
Save lorello/1013ee28405b14f7f5897294149e4356 to your computer and use it in GitHub Desktop.
Docker swarm dead workers cleanup
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
| #!/bin/bash | |
| # | |
| # This script clean a docker swarm cluster from all the dead worker nodes | |
| # | |
| # docker node ls output example | |
| # ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION | |
| #y4000************e1uv5vdj * boat-manager-c240 Ready Active Reachable 19.03.11 | |
| #wlqc4************zywu7z90 boat-manager-cb78 Ready Active Reachable 19.03.8 | |
| #i9fz8************rkmn98yh boat-manager-e102 Ready Active Leader 19.03.8 | |
| #wgrj7************rj8sglzg boat-tank01-9a5b Down Active 19.03.9 | |
| #5thbe************8bt8c7pn boat-tank01-20df Ready Active 19.03.9 | |
| #xyvyo************rqwvnbpc boat-worker-0e4a Down Active 19.03.8 | |
| set -e | |
| # exclude managers and get only Down workers nodes | |
| docker node ls | egrep -v '(Leader|Reachable)' | grep Down | while read line; do | |
| id=$(echo $line | awk '{ print $1 }') | |
| hostname=$(echo $line | awk '{ print $2 }') | |
| status=$(echo $line | awk '{ print $3 }') | |
| availability=$(echo $line | awk '{ print $4 }') | |
| if [[ $availability == 'Active' && $status == 'Down' ]]; then | |
| echo "$hostname results Active but is Down, drop it" | |
| docker node rm $id | |
| fi | |
| done | |
| # vim: autoindent tabstop=2 shiftwidth=2 expandtab softtabstop=2 filetype=yaml fileencoding=utf-8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment