Skip to content

Instantly share code, notes, and snippets.

@lorello
Created August 21, 2020 15:20
Show Gist options
  • Select an option

  • Save lorello/1013ee28405b14f7f5897294149e4356 to your computer and use it in GitHub Desktop.

Select an option

Save lorello/1013ee28405b14f7f5897294149e4356 to your computer and use it in GitHub Desktop.
Docker swarm dead workers cleanup
#!/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