【问题标题】:How do I switch between different Kubernetes contexts described in distinct kubeconfig yaml files fast?如何在不同 kubeconfig yaml 文件中描述的不同 Kubernetes 上下文之间快速切换?
【发布时间】:2020-02-12 10:48:44
【问题描述】:

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

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

【问题讨论】:

标签: kubectl context-switch kubeconfig


【解决方案1】:

您可以在KUBECONFIG environment variable 中声明所有上下文:

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

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

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

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

$ kubectx cluster1
Switched to context "cluster1".
$ kubectx cluster2
Switched to context "cluster2".

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-04-22
  • 2021-04-29
  • 2020-01-12
  • 1970-01-01
  • 2016-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多