正如 Mark 提到的,切换/读取/操作不同 kubernetes 环境(又名 kubernetes 上下文)的规范答案是使用kubectl config
,见下文:
$ kubectl config
Modify kubeconfig files using subcommands like "kubectl config set current-context my-context"
Available Commands:
current-context Displays the current-context
delete-cluster Delete the specified cluster from the kubeconfig
delete-context Delete the specified context from the kubeconfig
get-clusters Display clusters defined in the kubeconfig
get-contexts Describe one or many contexts
rename-context Renames a context from the kubeconfig file.
set Sets an individual value in a kubeconfig file
set-cluster Sets a cluster entry in kubeconfig
set-context Sets a context entry in kubeconfig
set-credentials Sets a user entry in kubeconfig
unset Unsets an individual value in a kubeconfig file
use-context Sets the current-context in a kubeconfig file
view Display merged kubeconfig settings or a specified kubeconfig file
Usage:
kubectl config SUBCOMMAND [options]
在后台,有一个~/.kube/config
YAML 文件存储所有可用的上下文及其对应的凭据和每个上下文的端点。
正如您可能已经知道的那样,现成的 Kubectl 并不容易管理不同的 kubernetes 上下文。与其滚动您自己的脚本来管理所有这些,更好的方法是使用一个名为 的成熟工具kubectx
,该工具由名为“Ahmet Alp Balkan”的 Google 员工创建,他在 Kubernetes / Google Cloud Platform 开发人员体验团队中构建这样的工具。我强烈推荐它。
https://github.com/ahmetb/kubectx
$ kctx --help
USAGE:
kubectx : list the contexts
kubectx <NAME> : switch to context <NAME>
kubectx - : switch to the previous context
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME>
kubectx -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
(this command won't delete the user/cluster entry
that is used by the context)
kubectx -h,--help : show this message