我有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

其他回答

为不同的环境克隆YAML文件绝对是理想的。你要做的是模板化你的YAML文件——通过提取不同环境的参数。

当然,您可以使用一些模板引擎,分离YAML中的值,并为特定环境生成YAML。但如果你采用掌舵图,这是很容易做到的。要查看一些示例图表,请访问这个Github回购的稳定目录

以Wordpress图表为例,你可以在两个环境中使用两个不同的命令:

戴夫:

头盔安装——name dev-release——set \ wordpressUsername = dev_admin \ wordpressPassword = dev_password \ mariadb。mariadbRootPassword = dev_secretpassword \ 稳定/ wordpress

不过,在CLI上传递这些值并不是必须的,可以将这些值存储在一个名为aptly values的文件中。Yml,你可以为不同的环境有不同的文件

在转换为Helm图表标准时,您需要做一些工作,但这些努力是值得的。

TL;DR:我创建了一个GUI来通过AppleScript切换Kubernetes上下文。我通过shift-cmd-x激活它。

我也有同样的问题。这是一个痛苦的切换上下文的命令行。我使用FastScripts设置了一个键组合(shift-cmd-x)来运行以下AppleScript(放在这个目录:$(HOME)/Library/Scripts/Applications/Terminal)。

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

do shell script "/usr/local/bin/kubectl config current-context"
set curcontext to result

do shell script "/usr/local/bin/kubectl config get-contexts -o name"
set contexts to paragraphs of result

choose from list contexts with prompt "Select Context:" with title "K8s Context Selector" default items {curcontext}
set scriptArguments to item 1 of result

do shell script "/usr/local/bin/kubectl config use-context " & scriptArguments

display dialog "Switched to " & scriptArguments buttons {"ok"} default button 1

了解所有的背景

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>

最新的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