0

我需要访问几个 Kubernetes 集群。对于他们每个人,我都有一个 kubeconfig yaml 文件,例如kubeconfig-cluster1.yamlkubeconfig-cluster2.yaml.

如何在这些配置之间轻松切换?我的意思是,没有KUBECONFIG手动将环境变量设置为这些文件之一?

4

1 回答 1

1

您可以在KUBECONFIG 环境变量中声明所有上下文:

KUBECONFIG 环境变量包含一个 kubeconfig 文件列表。对于 Linux 和 Mac,列表以冒号分隔。对于 Windows,该列表以分号分隔。

要根据 kubeconfig 文件自动检测上下文,假设它们都位于~/.kube文件夹中,并将它们作为冒号分隔的列表分配给KUBECONFIG环境变量,您可以将脚本添加到您的~/.bashrcor ~/.zshrc

# Autodetect kubeconfig files to enable switching between them with kubectx
export KUBECONFIG=`ls -1 ~/.kube/kubeconfig-* | paste -sd ":" -`

然后,要在这些 kubectl 上下文之间切换(使用自动完成功能!),请查看kubectx实用程序。kubectx README 页面包含安装说明。

$ kubectx cluster1
Switched to context "cluster1".
$ kubectx cluster2
Switched to context "cluster2".
于 2020-02-12T10:48:44.850 回答