How to Keep Your KubeConfig File Tidy

In partnership with

TechOps Examples

Hey — It's Govardhana MK 👋

Welcome to another technical edition.

Every Tuesday – You’ll receive a free edition with a byte-size use case, remote job opportunities, top news, tools, and articles.

Every Thursday and Saturday – You’ll receive a special edition with a deep dive use case, remote job opportunities and articles.

👋 Before we begin, I’m happy to partner with PerfectScale to bring you a practical Kubernetes Optimization Playbook.

Most K8s clusters are overbuilt and under-optimized.

That’s why in the K8s Optimization Playbook, we built a 6-step framework to guide you through cleanup and scale-up:

1. Patch up the holes (misconfig kills performance)

2. Trim the fat (right-size your pods)

3. Fill the bins (bin-packing = $$$)

4. Pick the smart metal (use better instance types)

5. Automate relentlessly (autoscalers, VPA, Karpenter)

6. Optimize in motion (monitor trends, not guesses)

BONUS: at the end, there’s  a Production-Ready Optimization Assessment you’ll want to run your cluster through.

IN TODAY'S EDITION

🧠 Use Case
  • How to Keep Your KubeConfig File Tidy

👀 Remote Jobs

📚️ Resources

👋 I built this newsletter to ~55,000 subscribers. It makes USD $12,000 MRR and sends 150,000 emails every week.

I can confidently say that without BEEHIIV, this wouldn’t have been this easy.

Growth tools, ad recommendations, automations, website design — they’ve covered everything you need to run hassle free.

You can (easily) launch a newsletter too

This newsletter you couldn’t wait to open? It runs on beehiiv — the absolute best platform for email newsletters.

Our editor makes your content look like Picasso in the inbox. Your website? Beautiful and ready to capture subscribers on day one.

And when it’s time to monetize, you don’t need to duct-tape a dozen tools together. Paid subscriptions, referrals, and a (super easy-to-use) global ad network — it’s all built in.

beehiiv isn’t just the best choice. It’s the only choice that makes sense.

🧠 USE CASE

How to Keep Your KubeConfig File Tidy

As someone who works with multiple Kubernetes clusters, managing the KubeConfig file quickly becomes difficult. Common issues include:

  • Old clusters, users, and contexts staying in the config even after cluster deletion.

  • Manual cleanups becoming tedious and error-prone.

  • Slow and confusing environment switching due to too much clutter.

Over time, this leads to a bloated KubeConfig file, making it harder to manage clusters.

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).

Users: Stores credentials (tokens or certificates) for authenticating the clusters.

Contexts: Links a user to a specific cluster, helping you switch between environments.

Current Context: Specifies which user-cluster combination is currently active.

current-context: techopsexamples-context

Managing the KubeConfig File with Kubectl

You can use kubectl to manage the KubeConfig file.

Here are some useful commands:

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

KubeConfig Bloat Problem

Creating many short-lived clusters bloats your KubeConfig file with old data.

References to deleted clusters, unused users, and irrelevant contexts remain, making it harder to manage necessary configurations.

Existing Solutions

There are a few ways to keep your KubeConfig file tidy, but they have limitations:

  • Manual Edits: You can remove entries, but it's slow and error-prone.

  • Splitting Files: Organizes configurations but complicates switching between them.

  • Custom Scripts: Automates cleanup, but requires regular updates and may not adapt to changing setups.

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).


Sample cleanup summary:

I hope this edition helped you learn something new.

🔴 Get my DevOps & Kubernetes ebooks! (free for Premium Club and Personal Tier newsletter subscribers)

Looking to promote your company, product, service, or event to 54,000+ DevOps and Cloud Professionals? Let's work together.