šŸ¶
Kubernetes

Completely Uninstall Kubernetes: A Step-by-Step Guide

By Jan on 02/10/2025

Learn how to completely uninstall and remove Kubernetes from your system, including all components and configurations.

Completely Uninstall Kubernetes: A Step-by-Step Guide

Table of Contents

Introduction

This guide provides a step-by-step process to completely remove a Kubernetes cluster from your system. This process is useful for starting fresh with a new cluster or troubleshooting persistent issues. Remember to execute these commands on every node in your cluster.

Step-by-Step Guide

  1. Reset kubeadm (on control plane node):

    sudo kubeadm reset
  2. Remove Kubernetes packages:

    sudo apt-get purge kubeadm kubectl kubelet kubernetes-cni kube* 
    sudo apt-get autoremove
  3. Remove Kubernetes configuration files:

    sudo rm -rf ~/.kube
    sudo rm -rf /etc/kubernetes
    sudo rm -rf /var/lib/etcd
  4. Remove CNI networking configuration:

    sudo rm -rf /etc/cni
  5. Restart Docker:

    sudo systemctl restart docker
  6. (Optional) Remove Docker images:

    docker system prune -a

Note: These steps should be performed on all nodes in your Kubernetes cluster.

Code Example

This Bash script completely removes a Kubernetes cluster from a set of nodes. It first resets kubeadm on the control plane node. Then, it removes Kubernetes packages, configuration files, and data directories from all nodes. It also removes the CNI networking configuration and restarts Docker on all nodes. Finally, it provides an optional command to remove all Docker images. Before running this script, ensure you have backups and replace placeholders with actual node information.

#!/bin/bash

# Define a function to execute a command on all nodes
run_on_all_nodes() {
  command=$1
  for node in $(kubectl get nodes -o wide | awk '{print $6}' | tail -n +2); do
    echo "Running '$command' on node: $node"
    ssh $node "$command"
  done
}

# Reset kubeadm on control plane node
sudo kubeadm reset

# Remove Kubernetes packages and configuration files
run_on_all_nodes 'sudo apt-get purge kubeadm kubectl kubelet kubernetes-cni kube* && sudo apt-get autoremove'
run_on_all_nodes 'sudo rm -rf ~/.kube /etc/kubernetes /var/lib/etcd'

# Remove CNI networking configuration
run_on_all_nodes 'sudo rm -rf /etc/cni'

# Restart Docker
run_on_all_nodes 'sudo systemctl restart docker'

# (Optional) Remove Docker images
run_on_all_nodes 'docker system prune -a'

echo "Kubernetes cluster reset complete."

Explanation:

  1. run_on_all_nodes() function: This function takes a command as input and executes it on all nodes in the cluster. It retrieves the node names using kubectl get nodes and iterates through them, executing the command on each node using ssh.
  2. Reset kubeadm: This step is only executed on the control plane node.
  3. Remove Kubernetes packages and configuration files: These commands are executed on all nodes to remove Kubernetes-related packages, configuration files, and data directories.
  4. Remove CNI networking configuration: This command removes the CNI configuration directory on all nodes.
  5. Restart Docker: This restarts the Docker daemon on all nodes to ensure a clean state.
  6. (Optional) Remove Docker images: This command removes all Docker images, which can be useful for freeing up disk space.

Before running this script:

  • Replace $(kubectl get nodes -o wide | awk '{print $6}' | tail -n +2) with the actual IP addresses or hostnames of your Kubernetes nodes.
  • Ensure you have SSH access to all nodes in your cluster.
  • This script will completely remove your Kubernetes cluster. Make sure you have a backup of any important data before proceeding.

This script provides a more automated way to reset your Kubernetes cluster by executing the necessary commands on all nodes.

Additional Notes

General:

  • Backup: Always back up important data before running this script, as it will irreversibly remove your Kubernetes cluster.
  • Network: This script assumes all nodes can communicate via SSH. Ensure your network configuration allows this.
  • Cloud Environments: This script is primarily for bare-metal or virtualized environments. Cloud providers often have their own tools for Kubernetes cluster deletion.
  • Persistent Volumes: This script does not remove Persistent Volumes. You'll need to delete them manually if required.

Script Improvements:

  • Error Handling: Implement error handling to stop the script if a command fails on any node.
  • User Input: Prompt the user for confirmation before executing potentially destructive commands.
  • Node Roles: Modify the script to differentiate between control plane and worker nodes for more targeted actions.
  • Package Manager: Adapt the script for different package managers like yum for RHEL-based systems.

Troubleshooting:

  • SSH Issues: Verify SSH keys are properly configured between nodes.
  • Command Failures: Check the output of each command for errors and address them accordingly.
  • Leftover Resources: Manually inspect the system for any remaining Kubernetes-related files or processes after running the script.

Alternatives:

  • Kubespray/Ansible: Consider using automation tools like Kubespray or Ansible for more robust and configurable cluster deployments and removals.

Remember: This script is a starting point. Adapt and enhance it based on your specific environment and requirements.

Summary

This guide provides a step-by-step process to fully remove a Kubernetes cluster from your system. Remember to perform these steps on every node in your cluster.

Steps:

  1. Reset kubeadm: Use sudo kubeadm reset to reset the Kubernetes control plane on the master node.
  2. Purge Kubernetes packages: Remove all Kubernetes-related packages using sudo apt-get purge followed by a list of package names (e.g., kubeadm, kubelet). Clean up residual packages with sudo apt-get autoremove.
  3. Delete Kubernetes configurations: Remove configuration files and directories, including user-specific configurations (~/.kube), system-wide configurations (/etc/kubernetes), and etcd data (/var/lib/etcd).
  4. Remove CNI networking: Delete the CNI configuration directory (/etc/cni).
  5. Restart Docker: Use sudo systemctl restart docker to restart the Docker service.
  6. (Optional) Clean up Docker images: Execute docker system prune -a to remove all unused Docker images, networks, and containers.

Caution: This process will completely erase your Kubernetes cluster and related data. Ensure you have backups if necessary before proceeding.

Conclusion

By following these steps, you can ensure a clean removal of your Kubernetes cluster, preparing your system for a fresh installation or a different configuration. Remember to adapt the commands and paths based on your specific environment and always prioritize data backups before proceeding with the uninstallation.

References

Were You Able to Follow the Instructions?

šŸ˜Love it!
šŸ˜ŠYes
šŸ˜Meh-gical
šŸ˜žNo
šŸ¤®Clickbait