Kubernetes access multiple cluster with KUBECONFIG variable on Windows

1/2/2020

I am a newbie in kubernetes clustering therefore it might be a simple question. So I installed minikube on my local machine and it creates a config file into $HOME/.kube/config. When I run kubectl config view command on Power Shell I can able to view the credentials. I already set a KUBECONFIG env variable and path is $HOME/.kube/config.

So the question is, in our company, we have an onpremise kubernetes cluster and I had a 2 different kubeconfig files per cluster. (one for stage one for production). What is the best way to define these kubeconfig files as an env variable to access these clusters?

What I tried, I put these files into $HOME/.kube directory and at the present I have three files such as config , c1.kubeconfig,c2.kubeconfig Then I updated my environment variable like $HOME/.kube/config:$HOME/.kube/c1.kubeconfig:$HOME/.kube/c2.kubeconfig. But this way I can not able to access c1 or c2 cluster.

-- semural
kubernetes

2 Answers

1/2/2020

Also explore kubectxwin power tools for better and faster context switch on windows

Git link for reference as below

https://github.com/thomasliddledba/kubectxwin

(This is Windows Version of the powerful tool kubectx https://github.com/ahmetb/kubectx#installation)

Download Exe from this link

Use in Power shell

PS C:\Users\DT> .\kubectxwin.exe ls
* kubernetes-admin@kubernetes

PS C:\Users\DT> .\kubectxwin.exe set kubernetes-admin@kubernetes
Switched to context "kubernetes-admin@kubernetes".

Example : Rename Context using the utility

PS C:\Users\DT> .\kubectxwin.exe rn kubernetes-admin@kubernetes kubeadmin
Context "kubernetes-admin@kubernetes" renamed to "kubeadmin".

PS C:\Users\DT> .\kubectxwin.exe ls
* kubeadmin
-- DT.
Source: StackOverflow

1/2/2020

When you use KUBECONFIG environment variable you can only point it to one kubeconfig file and not to a series of kubeconfig files. You need to merge all the individual kubeconfig files into one kubeconfig file and store it as $HOME/.kube/config and then select a cluster using context flag of kubectl.

cp $HOME/.kube/config $HOME/.kube/config.backup.$(date +%Y-%m-%d.%H:%M:%S)
KUBECONFIG=$HOME/.kube/config:file2:file3 kubectl config view --merge --flatten > \
~/.kube/merged_kubeconfig && mv ~/.kube/merged_kubeconfig ~/.kube/config
kubectl get pods --context=cluster-1
kubectl get pods --context=cluster-2

official docs here

-- Arghya Sadhu
Source: StackOverflow