我有Kubernetes在两个不同的环境中工作得很好,即在我的本地环境(MacBook运行minikube)和谷歌的容器引擎(GCE, Kubernetes在谷歌云上)。我使用MacBook/local环境来开发和测试我的YAML文件,然后,完成后,在GCE上尝试它们。
目前我需要单独使用每个环境:我需要在我的本地环境中编辑YAML文件,当准备就绪时,(git)将它们克隆到GCE环境中,然后使用/部署它们。这是一个有点麻烦的过程。
理想情况下,我想从我的Macbook使用kubectl来轻松地在本地minikube或GCE Kubernetes环境之间切换,并轻松地确定在哪里使用YAML文件。有没有一种简单的方法来切换上下文来做到这一点?
标准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/
请查看最新的(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
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