正如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
,是由Kubernetes / Google Cloud Platform开发人员体验团队的Googler名为“ Ahmet Alp Balkan”创建的,该团队构建了这样的工具。我强烈推荐它。
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