I am working with multiple Kubernetes clusters at Azure, so I need to change quickly from one cluster to another without having various files at my path C:\Users\username\.kube
, because I have to rename or replace the file when I wish to change to other.
I suggest that you use the following tools and tricks:
asdf
to manage multiple kubectl
versionsKUBECONFIG
env var to change between multiple kubeconfig
fileskube-ps1
to keep track of your current context/namespacekubectx
and kubens
to change fast between clusters/namespacesTake a look at this article, it explains how to accomplish this: Using different kubectl versions with multiple Kubernetes clusters
I also recommend this read: Mastering the KUBECONFIG file
I recommend you check out this tool: kubectxwin
This is the Windows version of the kubectx
tool which is the go-to for many to quickly change between clusters and namespaces within clusters.
You need to have all your kubernetes config files.
1.- Create a config
file in the path C:\Users\username\.kube
2.- Get the data from every config
file. For instance, 3 files one per environment (dev, qa, prod) so let's merge into one
Your file must looks like this:
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: some_authority_01
server: some_server_url_01
name: some_cluster_name_01
- cluster:
certificate-authority-data: some_authority_02
server: some_server_url_02
name: some_cluster_name_02
- cluster:
certificate-authority-data: some_authority_03
server: some_server_url_03
name: some_cluster_name_03
contexts:
- context:
cluster: some_cluster_name_01
user: some_user_01
name: some_cluster_name_01
- context:
cluster: some_cluster_name_02
user: some_user_02
name: some_cluster_name_02
- context:
cluster: some_cluster_name_03
user: some_user_03
name: some_cluster_name_03
current-context: some_cluster_name_01
kind: Config
preferences: {}
users:
- name: some_user_01
user:
client-certificate-data: some_certificate_01
client-key-data: some_key_01
- name: some_user_02
user:
client-certificate-data: some_certificate_02
client-key-data: some_key_02
- name: some_user_02
user:
client-certificate-data: some_certificate_03
client-key-data: some_key_03
Note: the value of the current-context may vary, it isn't necessary that be the first cluster.
3.- Add shortcuts for Windows 10 for changing kubernetes context quicky 3.1.- Create a file called Microsoft.PowerShell_profile.ps1
in the path C:\Users\username\Documents\WindowsPowerShell
3.2 Copy this data into the file that was recently created
function See-Contexts{kubectl config get-contexts}
Set-Alias -Name seec -Value See-Contexts
function change-context-01 { kubectl config use-context some_cluster_name_01}
Set-Alias -Name ctx01 -Value change-context-01
function change-context-02 { kubectl config use-context some_cluster_name_02}
Set-Alias -Name ctx01 -Value change-context-02
function change-context-03 { kubectl config use-context some_cluster_name_03}
Set-Alias -Name ctx01 -Value change-context-03
3.3.- Search PowerShell at search bar in in Windows and open the option RUN ISE as Administrator
and open the file Microsoft.PowerShell_profile.ps1 and run the file.
With this solution you can easily change from kubernetes cluster using a shortcut. For example if you wanna change to the cluster some_cluster_name_01 you only need to type ctx01
. This is useful when we have multiple kubernetes clusters.