我有Kubernetes在两个不同的环境中工作得很好,即在我的本地环境(MacBook运行minikube)和谷歌的容器引擎(GCE, Kubernetes在谷歌云上)。我使用MacBook/local环境来开发和测试我的YAML文件,然后,完成后,在GCE上尝试它们。

目前我需要单独使用每个环境:我需要在我的本地环境中编辑YAML文件,当准备就绪时,(git)将它们克隆到GCE环境中,然后使用/部署它们。这是一个有点麻烦的过程。

理想情况下,我想从我的Macbook使用kubectl来轻松地在本地minikube或GCE Kubernetes环境之间切换,并轻松地确定在哪里使用YAML文件。有没有一种简单的方法来切换上下文来做到这一点?


当前回答

切换/读取/操作不同的kubernetes环境(又名kubernetes上下文)的标准答案是,正如Mark提到的,使用kubectl配置,如下所示:

$ 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 /谷歌云平台开发人员体验团队的“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

其他回答

上下文列表

kubectl config get-contexts

上下文切换

kubectl config set current-context MY-CONTEXT

最新的2020年答案在这里,

一个简单的方法切换kubectl上下文,

kubectl top nodes **--context=**context01name

kubectl top nodes --context=context02name

您还可以将上下文名称存储为env like context01name = gke_ $ {GOOGLE_CLOUD_PROJECT} _us-central1-a_standard-cluster-1

请查看最新的(docker 19.03) docker context命令。

Ajeet Singh Raina)在“Docker 19.03.0预发布版:快速上下文切换,无根Docker, Sysctl对Swarm服务的支持”中阐述了这一点。

A context is essentially the configuration that you use to access a particular cluster. Say, for example, in my particular case, I have 4 different clusters – mix of Swarm and Kubernetes running locally and remotely. Assume that I have a default cluster running on my Desktop machine , 2 node Swarm Cluster running on Google Cloud Platform, 5-Node Cluster running on Play with Docker playground and a single-node Kubernetes cluster running on Minikube and that I need to access pretty regularly. Using docker context CLI I can easily switch from one cluster(which could be my development cluster) to test to production cluster in seconds.

$ sudo docker context --help
Usage:  docker context COMMAND
Manage contexts
Commands:
  create      Create a context
  export      Export a context to a tar or kubeconfig file
  import      Import a context from a tar file
  inspect     Display detailed information on one or more contexts
  ls          List contexts
  rm          Remove one or more contexts
  update      Update a context
  use         Set the current docker context
Run 'docker context COMMAND --help' for more information on a command.

例如:

[:)Captain'sBay=>sudo docker context ls . 名称描述docker端点kubernetes端点编排器 unix:///var/run/docker. xmlSock https://127.0.0.1:16443(默认)swarm swarm-context1

你可以从本地(minikube)切换到gcloud,然后返回:

kubectl config use-context CONTEXT_NAME

列出所有上下文:

Kubectl配置get-contexts

您可以为local和gcloud创建不同的环境,并将其放在单独的yaml文件中。

了解所有的背景

C:\Users\arun>kubectl config get-contexts

获取当前上下文

C:\Users\arun>kubectl config current-context

切换上下文

C:\Users\arun>kubectl config use-context <any context name from above list>