This is my trick for configuring and switching between multiple kubernetes clusters. We’ll use myCluster
as an example cluster name.
-
Have all of the kubectl acess files (YAML files) added into
~/.kube
, and namedmyCluster.config
. -
Ensure that each of them have a readable and rememberable context name:
apiVersion: v1 clusters: - cluster: certificate-authority-data: [REDACTED] server: [REDACTED] name: [REDACTED] contexts: - context: cluster: [REDACTED] user: [REDACTED] name: myCluster current-context: myCluster kind: Config preferences: {} users: - name: [REDACTED] user: token: [REDACTED]
-
Ensure that when initializing your console, you reference those files and add them to the
KUBECONFIG
variable. I do this through a local-only file that I source from my dotFiles. Of course, this can be done programatically but you might not want access to clusters to be added dynamically to your console. Up to you.export KUBECONFIG=$KUBECONFIG:~/.kube/cluster1.config:~/.kube/myCluster.config
-
Setup a quick alias just to switch between clusters. I have this setup from my dotFiles.
alias k='kubectl' alias kswitch='k config use-context'
-
Now just load a console and you can do:
kswitch cluster1 # Switched to context "cluster1". kswitch myCluster # Switched to context "myCluster".