Background
As someone who juggles multiple Kubernetes clusters daily, both local and remote, I’ve often found managing the ~/.kube/config
file to be a mess.
When you work with multiple clusters, it’s common to copy individual kubeconfig files, merge them manually into your local ~/.kube/config
, and clean them up when no longer needed. Add the occasional overwrite mishaps, and you’ll quickly want a better way to manage multiple kubeconfig files.
Sure, tools like kubectx
and kubens
help with Kubernetes context and namespace switching. You can find more about these great tools on their GitHub repo. But they assume you’ve already handled the messy kubeconfig merging part.
That’s why I built a lightweight Bash script and called it kubeconfig-manager.sh
, which I alias as kcfg
. This little tool keeps my kubeconfig life sane.
What the Script Does
The kcfg
script makes working with multiple Kubernetes clusters a breeze. With a few simple commands, I can:
- Copy a remote kubeconfig via SCP, fix the 127.0.0.1 issue, rename the context, and merge it automatically.
- Add an existing kubeconfig file into my main
~/.kube/config
. - Remove a context (cluster, user, and context) from
~/.kube/config
. - Export a specific context from the merged kubeconfig to a separate file.
- List all current contexts.
- Automatically keep only the last 5 kubeconfig backups to avoid clutter.
The script outputs are clean, colorized, and informative — just the way I like them.
Prerequisites
In addition to having kubectl
installed, make sure you have the following tools:
|
|
You’ll also want to install kubectx
and kubens
. Check the GitHub link above for platform-specific instructions. SSH access to your remote clusters is required as well.
Setup Instructions
- Create the kube directory
|
|
- Save the script
Save the script as ~/.kube/kubeconfig-manager.sh
and make it executable. You can grab the latest version of the script from my repo.
|
|
- Add alias
Add the following line to your ~/.bash_aliases
:
|
|
Make sure your ~/.bashrc
sources it:
|
|
Then reload your shell:
|
|
- Optional: My Handy Aliases
|
|
How to Use kcfg
Here are common usage scenarios for kcfg
. You can always run the command without parameters to see all supported options:
|
|
And you’ll get this output:
|
|
Copy kubeconfig from a remote server
I run this command when I have a new cluster that I need to add to my local ~/.kube/config
file.
|
|
This:
- Copies the config from
server01
- Backs up the current config
- Replaces
127.0.0.1
withserver01
- Renames the cluster/context to
k3s-test
- Merges it into
~/.kube/config
- Merges it into
This is the sample output in the terminal:
|
|
Add a local kubeconfig file
This is the scenario when I already have individual config yaml file and just need to add the context to the local ~/.kube/config
file.
|
|
With the sample terminal output like this:
|
|
Remove a context
This deletes the context, user, and cluster from my main local ~/.kube/config
file.
|
|
And the sample terminal output:
|
|
List all contexts
This command lists all the context from my local ~/.kube/config
file.
|
|
With sample terminal output:
|
|
Export a single context
Sometimes I need to export specific cluster context as an individual config file.
|
|
This creates a new file:
|
|
Backup Cleanup
After multiple imports, my ~/.kube
folder looks like this:
|
|
The script automatically keeps only the last 5 backups to keep the folder clean.
Final Thoughts
This script isn’t a replacement for kubectl
, kubectx
, or kubeadm
, but it bridges the gap in kubeconfig management when dealing with multiple Kubernetes clusters. It’s helped me streamline my local environment, avoid mistakes, and work faster.
You can grab the latest version of the script at my GitHub repo.