简体   繁体   English

Kubernetes 在 Windows 上使用 KUBECONFIG 变量访问多个集群

[英]Kubernetes access multiple cluster with KUBECONFIG variable on Windows

I am a newbie in kubernetes clustering therefore it might be a simple question.我是 kubernetes 集群的新手,因此这可能是一个简单的问题。 So I installed minikube on my local machine and it creates a config file into $HOME/.kube/config.所以我在我的本地机器上安装了 minikube,它在 $HOME/.kube/config 中创建了一个配置文件。 When I run kubectl config view command on Power Shell I can able to view the credentials.当我在 Power Shell 上运行kubectl config view命令时,我可以查看凭据。 I already set a KUBECONFIG env variable and path is $HOME/.kube/config.我已经设置了一个 KUBECONFIG 环境变量,路径是$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.所以问题是,在我们公司,我们有一个内部部署的 kubernetes 集群,我每个集群有 2 个不同的 kubeconfig 文件。 (one for stage one for production). (一个用于第一阶段的生产)。 What is the best way to define these kubeconfig files as an env variable to access these clusters?将这些 kubeconfig 文件定义为访问这些集群的环境变量的最佳方法是什么?

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 .我尝试了什么,我将这些文件放入$HOME/.kube目录中,目前我有config , c1.kubeconfig,c2.kubeconfig三个文件然后我更新了我的环境变量,如$HOME/.kube/config:$HOME/.kube/c1.kubeconfig:$HOME/.kube/c2.kubeconfig But this way I can not able to access c1 or c2 cluster.但是这样我就无法访问 c1 或 c2 集群。

When you use KUBECONFIG environment variable you can only point it to one kubeconfig file and not to a series of kubeconfig files.当您使用 KUBECONFIG 环境变量时,您只能将其指向一个 kubeconfig 文件,而不能指向一系列 kubeconfig 文件。 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.您需要将所有单独的 kubeconfig 文件合并为一个 kubeconfig 文件并将其存储为 $HOME/.kube/config,然后使用 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 官方文档在这里

Also explore kubectxwin power tools for better and faster context switch on windows还可以探索 kubectxwin 强大的工具,以便在 Windows 上更好更快地进行上下文切换

Git link for reference as below供参考的 Git 链接如下

https://github.com/thomasliddledba/kubectxwin https://github.com/thomasliddledba/kubectxwin

(This is Windows Version of the powerful tool kubectx https://github.com/ahmetb/kubectx#installation ) (这是强大的工具 kubectx https://github.com/ahmetb/kubectx#installation 的Windows 版本)

Download Exe from this link从此 链接下载 Exe

Use in Power shell在 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM