KubeConfig Cleanup Automation with Kubetidy
- RNREDDY

- Sep 10
- 2 min read

KubeConfig Cleanup Automation with Kubetidy
If you’ve been working with Kubernetes for a while, your KubeConfig file has likely become an unmanageable mess.
Old clusters that no longer exist, stale user credentials, and outdated contexts make navigating and managing clusters frustrating.
Every kubectl get pods or kubectl config use-context cascades the clutter.
What is a KubeConfig File?
A KubeConfig file holds information about clusters, users, and contexts, allowing Kubernetes to manage connections and enable easy interaction across environments.
Breakdown of a KubeConfig File
Clusters: Contains the details of Kubernetes clusters, such as the API server endpoint and the cluster's Certificate Authority (CA).
clusters:
- name: techopsexamples-cluster
cluster:
server: https://k8s.techopsexamples.com
certificate-authority-data: Cluster CA
Users: Stores credentials (tokens or certificates) for authenticating the clusters.
users:
- name: techopsexamples-user
user:
token: abc123tokenxyz
Contexts: Links a user to a specific cluster, helping you switch between environments.
contexts:
- name: techopsexamples-context
context:
cluster: techopsexamples-cluster
user: techopsexamples-user
Some teams resort to manually editing KubeConfig, using:
kubectl config delete-cluster <cluster-name>
kubectl config delete-user <user-name>
kubectl config delete-context <context-name>
kubectl config unset contexts.<context-name>
View the KubeConfig:
kubectl config view
Switch to a different context:
kubectl config use-context techopsexamples-context
Add a new cluster:
kubectl config set-cluster techopsexamples-cluster --server=https://techopsexamples.cluster.com
Add a new user:
kubectl config set-credentials techopsexamples-user --token=abc123tokenxyz
Imagine you're managing multiple clusters across environments (dev, staging, production), and some temporary clusters for testing. Over time, your ~/.kube/config file bloats with expired entries, causing:
Increased risk of misconfigurations
Accidental interactions with deprecated clusters
Confusion when switching between active clusters
Delays in troubleshooting due to cluttered contexts
Better Solution:
KubeTidy, a tool built to automatically remove outdated clusters, users, and contexts from your KubeConfig file.
KubeTidy keeps only relevant entries, simplifying management, and backs up your file automatically.
It works on PowerShell (Windows/Linux/macOS) or as a krew plugin with Krew (Linux/macOS).




Comments