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

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

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


当前回答

请查看最新的(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

其他回答

如果你正在寻找一种在不同上下文之间切换的简单方法,也许这将会有所帮助。

我从前面提到的kubectx和kswitch脚本中得到了灵感,我可以将它们推荐给大多数用例。它们帮助解决切换任务,但在~/.kube/config的一些更大或更少的标准配置上对我造成了破坏。所以我创建了一个sys-exec调用包装器和一个kubectl的简写。

如果你不带参数地调用k,你会看到一个被拦截的切换上下文的提示。

Switch kubectl to a different context/cluster/namespace.
Found following options to select from:
 >>> context: [1] franz
 >>> context: [2] gke_foo_us-central1-a_live-v1
 >>> context: [3] minikube
 --> new num [?/q]:

更进一步,k继续作为一个简写。以下是等价的:

kubectl get pods --all-namespaces
k get pods -A
k p -A

我使用kubeswitch(免责声明:这个工具是我写的),它可以像kubectx一样使用,但它是为大量kubecconfig文件设计的。 如果您必须处理数百或数千个kubecconfig文件,这个工具可能对您有用,否则kubectx或kubectl配置use-context可能就足够了。

例如,它增加了从保险库读取、搜索时热重载以及加速后续搜索的索引等功能。

你可以在这里安装。

编辑:现在还包括直接支持GKE。因此,您可以使用和发现kubecconfig文件,而无需手动下载和更新它们。

标准kubectl命令的快捷方式是使用kubectx:

列表上下文:kubectx 等价于kubectl config get-contexts 切换上下文(到foo): kubectx foo 相当于kubectl config use-context foo

在macOS上安装:brew install kubectx

kubectx包还包括一个用于切换名称空间的类似工具,称为kubens。

如果您经常在多个上下文中和名称空间中工作,这两个工具非常方便。

更多信息:https://ahmet.im/blog/kubectx/

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

kubectl config use-context CONTEXT_NAME

列出所有上下文:

Kubectl配置get-contexts

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

请查看最新的(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