1. Overview
In this article, we’ll discuss how to completely remove Kubernetes from a Linux machine. We’ll see a step-by-step approach to removing Kubernetes packages, cluster configurations, firewall tables and rules, and Docker.
2. Uninstalling Kubernetes
In addition to the dependencies, Kubernetes creates various files in several places on the file system. It includes log files, config files, service and daemon files, etc.
In this section, we discuss a step-by-step approach to completely removing Kubernetes from a Linux machine.
2.1. kubeadm reset
kubeadm (Kubernetes Admin) is a command that lets us bootstrap Kubernetes clusters. When we install Kubernetes, we typically run kubeadm init to create a master node. It creates the relevant cluster config files, generate certificates, initializes key-value stores, etc.
In contrast, we can reverse these changes by using the reset command:
$ kubeadm reset
It effectively resets a node back to its original state.
2.2. Uninstall Relevant Packages
The next step is to uninstall the Kubernetes package installed by a package manager:
# Debian and Ubuntu
$ sudo apt-get purge kubeadm kubectl kubelet kubernetes-cni kube*
# Fedora and Red Hat
$ sudo dnf remove kubeadm kubectl kubelet kubernetes-cni kube*
Afterward, we’ll remove additional unused dependencies:
# Debian and Ubuntu
$ sudo apt autoremove
# Fedora and Red Hat
$ sudo dnf autoremove
2.3. Remove Related Files and Directories
For the most part, kubeadm reset takes care of deleting the relevant files. However, just to be on the safe side, we’ll remove some of the Kubernetes-specific files that might be left out:
$ rm -rf ~/.kube
$ rm -rf /etc/cni /etc/kubernetes rm -f /etc/apparmor.d/docker /etc/systemd/system/etcd*
$ rm -rf /var/lib/dockershim /var/lib/etcd /var/lib/kubelet \
/var/lib/etcd2/ /var/run/kubernetes
2.4. Clear out the Firewall Tables and Rules
Usually, kubeadm reset clears the iptables rules. However, as a precaution, we’ll reset them manually. We start with flushing and deleting the filter table:
$ iptables -F && iptables -X
Next, we flush and delete the NAT (Network Address Translation) table:
$ iptables -t nat -F && iptables -t nat -X
Then, we flush and remove the chains and rules in the raw table:
$ iptables -t raw -F && iptables -t raw -X
Finally, we remove the chains and rules in the mangle table:
$ iptables -t mangle -F && iptables -t mangle -X
2.5. Optional: Docker
We can remove the Docker containers, images, and the docker group as well. However, it’s optional:
$ docker image prune -a
It removes all the unused Docker images that aren’t associated with any containers. Afterward, we restart the Docker service:
$ sudo systemctl restart docker
Next, we can uninstall Docker if we need to:
# Debian and Ubuntu
$ sudo apt purge docker-engine docker docker.io docker-ce docker-ce-cli containerd containerd.io runc --allow-change-held-packages
# Fedora and Red Hat
$ sudo dnf remove docker-ce docker-ce-cli containerd.io
As usual, we remove any unused dependencies as well:
# Debian and Ubuntu
$ sudo apt autoremove
# Fedora and Red Hat
$ sudo dnf autoremove
Finally, let’s remove the docker group as well:
$ sudo groupdel docker
2.6. Reload Systemd Manager
As a final step, we’ll reload the systemd manager for any systemd changes to take effect:
$ sudo systemctl reload-daemon
3. Conclusion
For the most part, kubeadm reset makes the uninstallation process easier. It removes most of the Kubernetes-specific vague files and configurations. Besides, its behavior is consistent across different Linux distributions. However, we still need to remove some of the files and directories ourselves, which we discussed in the tutorial.