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
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
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.