Wrangle your Kubernetes contexts

Greg Swallow
1 min readMay 21, 2020

Perhaps many of you who read this quick little blurb will say, “yeah, duh” but until now I have been struggling with an unwieldy $HOME/.kube/config file. Even with the kubectx and kubens tools, I feel like I have to sort through a lot of oddly-named contexts to get logged into the right Kubernetes cluster. Right now I have access to 16 different Kubernetes clusters. I just culled 25 or so out of my $HOME/.kube/config file as my services for a client have come to an end.

Here’s a simple trick for managing all of these contexts. Stuff this into your .zshrc (or .bash_profile) file:

Load kubernetes contexts from multiple files

Create a $HOME/.kube/config.d directory and store each Kubernetes context in its own file. Running Kubernetes in the cloud? Fetch credentials mostly easily (get on this, Google):

Obtain kubeconfigs into discrete files