Using multiple versions of kubectl on macOS

When you work with multiple clients, or maybe with one company that has clusters in different versions of Kubernetes, you need to have a kubectl (Kubernetes client) binary on your Mac that is not necessarily the only that brew package manager offers (which is usually the latest). Kubernetes has a lee way of one version back and forth between the kubernetes server side and the kubernetes client side, but the 3 month release cycle is quite aggressive for stable companies. So how can you keep multiple versions of kubectl and switch to the one you need on demand? Enter asdf. asdf is like nvm, or pyenv, or rbenv, it’s a version manager. Unlike the others though, it is extendable which is why there is a kubectl plugin.

How to get it working?

  1. Uninstall any kubectl that you have installed
  2. Install asdf using brew: brew install asdf
  3. Export the shims to your path: export PATH=$PATH:$HOME/.asdf/shims in .bash_profile/.bashrc
  4. Install the plugin: asdf plugin-add kubectl
  5. Install whichever versions you need. For example: asdf install kubectl 1.13.4 or asdf install kubectl 1.15.0
  6. Set the default system version to one of them: asdf global kubectl 1.13.4
  7. Check kubectl’s active version: kubectl version
  8. Now go to a folder of a project that uses a different version of kubectl and set the local version to that: asdf local kubectl 1.15.0
  9. Check locally: kubectl version
  10. You’re done!

The way it does it is with a file called .tool-versions in the relevant path.

~ $ cat .tool-versions
kubectl 1.15.0

Which means that you can also use asdf to control the versions of other tools simultaneously, like ruby for example!

Want to play around more? Here is the commands list

I hope this helps!