Quick-tip: using kubectl when you manage multiple Kubernetes clusters

Kubectl has this great feature of ‘contexts’ which allow you to switch between clusters or even namespaces within a cluster. But how do you go about adding contexts to your version of kubectl?

I have used the KUBECONFIG environment variable to specify the location of a custom kubeconfig.yaml file; what I recently discovered was that you can specify multiple locations to kubeconfig files in the same KUBECONFIG environment variable. Each path is that of the configuration file (not the containing folder) and should be separated by a colon (:). When you do this, kubectl will automatically merge these as multiple contexts.

Now you are empowered by being able to use the kubectl config context functions to switch between clusters without worrying where each of the configuration files are located. I even use a special line in my .bashrc to compile a series of configuration files into one KUBECONFIG environment variable.

If you find the kubectl config context functions to be cumbersome and tiring to write, there is an app for that; or actually: a piece of bash script. Ahmed Alp Balkan wrote a tool called kubectx and kubens with which you can easily switch between contexts, or between namespaces within the same context. Gone are the days when you had to provide --namespace each time you invoke kubectl.

On MacOSX you can install this tool using brew using the command `brew install kubectx`.

In the end, if you work with multiple Kubernetes clusters I can recommend taking the time to set up a central location for your configuration files, automatically set their paths in the KUBECONFIG environment variable and use kubectx to quickly switch between them.

One thought on “Quick-tip: using kubectl when you manage multiple Kubernetes clusters

  1. HI..

    can you please give me the special line which you use in .bashrc to compile a series of configuration files into one KUBECONFIG environment variable

Comments are closed.