Skip to content

Instantly share code, notes, and snippets.

@superseb
Last active March 13, 2024 09:22
Show Gist options
  • Star 42 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save superseb/06539c6dcd377e118d72bfefdd444f81 to your computer and use it in GitHub Desktop.
Save superseb/06539c6dcd377e118d72bfefdd444f81 to your computer and use it in GitHub Desktop.
Extended Rancher 2 cleanup (backup your data, use at your own risk)
#!/bin/sh
# Backup your data
# Use at your own risk
# Usage ./extended-cleanup-rancher2.sh
# Include clearing all iptables: ./extended-cleanup-rancher2.sh flush
docker rm -f $(docker ps -qa)
docker rmi -f $(docker images -q)
docker volume rm $(docker volume ls -q)
for mount in $(mount | grep tmpfs | grep '/var/lib/kubelet' | awk '{ print $3 }') /var/lib/kubelet /var/lib/rancher; do umount $mount; done
cleanupdirs="/etc/ceph /etc/cni /etc/kubernetes /opt/cni /opt/rke /run/secrets/kubernetes.io /run/calico /run/flannel /var/lib/calico /var/lib/etcd /var/lib/cni /var/lib/kubelet /var/lib/rancher/rke/log /var/log/containers /var/log/pods /var/run/calico"
for dir in $cleanupdirs; do
echo "Removing $dir"
rm -rf $dir
done
cleanupinterfaces="flannel.1 cni0 tunl0"
for interface in $cleanupinterfaces; do
echo "Deleting $interface"
ip link delete $interface
done
if [ "$1" = "flush" ]; then
echo "Parameter flush found, flushing all iptables"
iptables -F -t nat
iptables -X -t nat
iptables -F -t mangle
iptables -X -t mangle
iptables -F
iptables -X
/etc/init.d/docker restart
else
echo "Parameter flush not found, iptables not cleaned"
fi
@panho66
Copy link

panho66 commented Oct 23, 2018

may be rm -rf /var/lib/rancher ???

ls -asrlt /var/lib/rancher
total 20K
4.0K drwxr-xr-x 3 root root 4.0K Oct 11 15:23 rke/
4.0K drwxr-xr-x. 47 root root 4.0K Oct 11 15:24 ../
4.0K drwxr-xr-x 2 root root 4.0K Oct 18 11:34 log-volumes/
4.0K drwxr-xr-x 4 root root 4.0K Oct 18 11:34 fluentd/
4.0K drwxr-xr-x 5 root root 4.0K Oct 18 11:34 ./

@suvl
Copy link

suvl commented Jan 28, 2019

in case the pods mount some ceph filesystem path, this script was not umounting them. this might be true for other storage classes. I've updated the script to take the ceph type into consideration, check my fork.

@beastofbobmin
Copy link

Careful with this as if the umount fails then it can potentially remove all the data in the Persistent Volume shares, IE ceph/NFS etc, probably worth adding some form of sanity check to make sure that the mount points inside /var/lib/kubelet have been successfully unmounted before performing an rm -rf on the directory

@pomazanbohdan
Copy link

rm -f /var/lib/containerd/io.containerd.metadata.v1.bolt/meta.db

@paraita
Copy link

paraita commented Jan 31, 2020

Your script is so useful man, thanks for sharing :)

@moisei
Copy link

moisei commented Jun 14, 2020

some error checks + adopt to bash @ ubuntu

#!/bin/bash
# Backup your data
# Use at your own risk
# Usage ./extended-cleanup-rancher2.sh
# Include clearing all iptables: ./extended-cleanup-rancher2.sh flush
containers=$(docker ps -qa)
[[ ! -z "$containers" ]] && docker rm -f $containers
images=$(docker images -q)
[[ ! -z "$images" ]] && docker rmi -f $images
volumes=$(docker volume ls -q)
[[ ! -z "$volumes" ]] && docker volume rm $volumes
for mount in $(mount | grep '/var/lib/kubelet' | awk '{ print $3 }') /var/lib/kubelet /var/lib/rancher; do umount $mount; done
cleanupdirs="/etc/ceph /etc/cni /etc/kubernetes /opt/cni /opt/rke /run/secrets/kubernetes.io /run/calico /run/flannel /var/lib/calico /var/lib/etcd /var/lib/cni /var/lib/kubelet /var/lib/rancher/rke/log /var/log/containers /var/log/pods /var/run/calico"
for dir in $cleanupdirs; do
  echo "Removing $dir"
  rm -rf $dir
done
cleanupinterfaces="flannel.1 cni0 tunl0"
for interface in $cleanupinterfaces; do
  echo "Deleting $interface"
  ip link delete $interface
done
if [ "$1" = "flush" ]; then
  echo "Parameter flush found, flushing all iptables"
  iptables -F -t nat
  iptables -X -t nat
  iptables -F -t mangle
  iptables -X -t mangle
  iptables -F
  iptables -X
  service docker restart
else
  echo "Parameter flush not found, iptables not cleaned"
fi

@harridu
Copy link

harridu commented Jul 3, 2020

I highly appreciate you script, but there is one severe problem making it (almost) unusable for me: It removes unrelated docker containers and images.

Somewhere I read the recommendation, to use the "legacy" iptables command instead of the version based on nftables, so I tried. Apparently these tables were never cleaned up. After running the script with "flush" iptables-save showed me a message "Warning: iptables-legacy tables present, use iptables-legacy-save to see them". Very hard to detect. Maybe it would be helpful to flush these tables as well?
(https://v1-15.docs.kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/)

@YungHsing
Copy link

Your shared shell script is effective. I was stuck on this issue for a long time and even asking GPT couldn't solve it. Thank you for sharing.
非常謝謝你~好用!

@burhan-b
Copy link

Thank you @superseb. Your script worked well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment