You can declare all contexts in the KUBECONFIG environment variable:
The KUBECONFIG environment variable holds a list of kubeconfig files. For Linux and Mac, the list is colon-delimited. For Windows, the list is semicolon-delimited.
To autodetect the contexts based on the kubeconfig files, assuming they're all located in the ~/.kube
folder, and assign them as a colon-separated list to the KUBECONFIG
environment variable, you could add a script to your ~/.bashrc
or ~/.zshrc
:
# Autodetect kubeconfig files to enable switching between them with kubectx
export KUBECONFIG=`ls -1 ~/.kube/kubeconfig-* | paste -sd ":" -`
Then, to switch between these kubectl contexts (with autocomplete!), have a look at the kubectx utility.
The kubectx README page contains installation instructions.
$ kubectx cluster1
Switched to context "cluster1".
$ kubectx cluster2
Switched to context "cluster2".