I recently found myself typing a lot of keystrokes just to switch between the GKE clusters I’m currently using. The kubectl command line tool currently does not make it as easy as I would like, so I wrote my own tool.
The fact that GKE context names are long made me seek an alternative way.
There has been discussions to support
environment variables. However these proposals are not accepted yet.
Some people use multiple
KUBE_CONFIG directories to switch between
multiple configuration files, which sounds like a directory management overhead.
In my daily developer workflow, I switch back and forth between directories
cd - command. Similarly I switch between Git branches using the
checkout - command. So why don’t we have this pattern for kubectl?
So I implemented kubectx. It’s a simple bash script but it does what I need. Here is what kubectx has to offer:
kubectx remembers your context switches, you can switch to the previous contexts with:
kubectx can create aliases for your long context names:
and then switch to a context easily:
Not to mention, there is bash/zsh completion out of the box. It can autocomplete the context name with the Tab key. See a demo below.
I created a simple Homebrew tap to install on mac OS. This installs the symlink and the bash/zsh completion script automatically:
brew tap ahmetb/kubectx https://github.com/ahmetb/kubectx.git brew install kubectx
See the repository for up to date instructions as these instructions are subject to change and I probably won’t remember to update this article
Ideally, I want to see this functionality in
kubectl command and make
kubectx obsolete . I love switching back and forth between
kubectx - everyday.
I’m sure I’d love to have an
KUBE_CONTEXT environment variable, too.
If you like this idea, make sure you support the proposal.
kubectx can help facilitate the conversation about this
topic further. Until then,
kubectx will stay as part of my daily
Don’t want to use kubectx? You can still create this alias in your .bashrc/.zshrc that gives you similar functionality:
alias kubectx="kubectl config use-context"
If you installed kubectl shell completion, this alias can make your day easier. (I have a ton of other kubectl aliases I should share some day.)