Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Richard-Barrett/40f03bf783d61b4b6b6b9fd1718082ba to your computer and use it in GitHub Desktop.
Save Richard-Barrett/40f03bf783d61b4b6b6b9fd1718082ba to your computer and use it in GitHub Desktop.
Automatic Linux Worker MKE Node Cleanup Script
#!/bin/bash
# ===============================================
# Created by: Richard Barrett
# Date Created: 02/27/2020
# Purpose: Auto Interactive Worker Node Cleanup
# Company: Mirantis
# ===============================================
# Documentation:
# ============================================================================================================================================
# https://mirantis.my.salesforce.com/articles/How_To/How-to-Clean-Up-A-Docker-Enterprise-MKE-Linux-Worker-Node?popup=false&id=kA32S000000PfQF
# ============================================================================================================================================
#set -e
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
# System Variables
# ================
DATE="$(date +'%Y%-m%d')"
VAR_LIB_DOCKER="/var/lib/docker"
RUN_DOCKER="/run/docker"
ETC_CNI="/etc/cni"
ETC_CONTAINERD="/etc/containerd"
OPT_CNI="/opt/cni"
OPT_CONTAINERD="/opt/containerd"
VAR_LIB_CONTAINERD="/var/lib/containerd"
VAR_LIB_KUBELET="/var/lib/kubelet"
VAR_LIB_DOCKERENGINE="/var/lib/docker-engine"
VAR_LOG_CONTAINERS="/var/log/containers"
LIST_VAR_LIB_KUBELET=$(ls -d "/var/lib/kubelet/"*.*)
while true; do
read -p "Do you want to proceed with the clean up of this worker node (yes/no)?" yn
case $yn in
[Yy]* ) echo "============================================================================"; \
echo " Cleaning up Worker Node..."; \
echo "============================================================================"; \
# Clean up worker node
echo "Leaving Swarm...";
docker swarm leave --force;
echo "Stopping All Docker Containers...";
docker stop $(docker ps -aq);
echo "Removing All Docker Components...";
docker rm $(docker ps -aq);
docker volume rm $(docker volume ls);
docker service rm $(docker service ls -q);
echo "Perform Docker System Prune...";
docker system prune -af;
echo "Stopping Docker and Containerd...";
systemctl stop docker;
systemctl stop containerd;
echo "Removing Docker Filesystem...";
# Remove Docker File System and Docker Mounts
rm -rf $VAR_LIB_DOCKER
echo "Unmounting /run/docker/netns/default...";
umount -f $RUN_DOCKER/netns/default
rm -rf $RUN_DOCKER
rm -rf $ETC_CNI
rm -rf $ETC_CONTAINERD
rm -rf $OPT_CNI
rm -rf $OPT_CONTAINERD
rm -rf $VAR_LIB_CONTAINERD
echo "Unmounting All Kubelet Pods Running As Containers...";
for i in $($LIST_VAR_LIB_KUBELET); \
do unmount -f $i; \
done;
rm -rf $VAR_LIB_KUBELET
rm -rf $VAR_LIB_DOCKERENGINE
rm -rf $VAR_LOG_CONTAINERS
echo "Filesystem has been deleted, you do not need to reboot...";
echo "Starting Docker Daemon...";
systemctl start docker;
echo "============================================================================"; \
echo " Worker Node Has Been Cleaned, Proceed to Rejoin Cluster..."; \
echo "============================================================================"; \
break;; \
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
while true; do
read -p "Is this cluster airgapped (no/yes)?" ny
case $ny in
[Nn]* ) echo "================================================"; \
echo " Pull Missing Images"; \
echo "================================================"; \
echo "Pulling Missing Images..."
docker container run --rm -it \
--name ucp \
-v /var/run/docker.sock:/var/run/docker.sock mirantis/ucp \
images --pull missing;
echo "Missing Images Have Been Pulled..."
break;; \
[Yy]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment