如何在gcloud和minikube之间切换kubectl集群

265

我已经在两个不同的环境中成功使用 Kubernetes,一个是本地环境(MacBook 上运行 minikube),另一个是在 Google 的容器引擎上(GCE,即 Google Cloud 上的 Kubernetes)。我使用 MacBook 或本地环境来开发和测试 YAML 文件,完成后尝试在 GCE 上运行。

目前我需要分别在每个环境中工作:我需要在本地环境中编辑 YAML 文件,准备好之后,(git)克隆到 GCE 环境中再进行使用/部署。这是一个有些繁琐的过程。

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

15个回答

533

您可以使用以下命令在本地(minikube)和gcloud之间切换:

kubectl config use-context CONTEXT_NAME

要列出所有上下文,请使用:

kubectl config get-contexts

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


12
你如何将它添加到kubectl上下文中?有没有一些gcloud命令可以做到这一点?找到了:$ gcloud container clusters get-credentials $CLUSTER_NAME - Justin Thomas
2
以上内容没有显示给我原始上下文,但grep 'name:' ~/.kube/config可以。它是minikube,所以我可以使用kubectl config use-context minikube切换回它。 - Jordan Morris
1
你可以使用 "minikube start --keep-context" 命令创建上下文。 - Aleksey Kanaev

59

列表上下文

kubectl config get-contexts

切换上下文

kubectl config set current-context MY-CONTEXT

27

使用 kubectx 可以更快捷地执行标准 kubectl 命令:

  • 列出上下文:kubectx
    • 等同于 kubectl config get-contexts
  • 切换上下文(到 foo):kubectx foo
    • 等同于 kubectl config use-context foo

在 macOS 上安装:brew install kubectx

kubectx 还包括一个类似的工具,用于切换命名空间,叫做 kubens

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

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


9

如果你正在寻找Mac上基于GUI的解决方案,并且已经安装了Docker桌面版,那么你可以使用Docker菜单栏图标。在这里,你可以找到“Kubernetes”菜单,其中列出了kubeconfig中所有的上下文,并且可以轻松地在它们之间切换。


这更像是一条注释而不是一个答案。 - coderpc
1
这是一个很棒的答案(特别是结合 Justin Thomas 在被接受的答案中的评论中提到的 gcloud container clusters get-credentials $CLUSTER_NAME)。 - thebjorn

7
获取所有上下文
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>

5

最新的答案在这里:

一种简单的切换kubectl上下文的方法:

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

kubectl top nodes --context=context02name

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

3
切换/读取/操作不同的 Kubernetes 环境 (也称为 Kubernetes 上下文) 的规范答案,就像 Mark 提到的那样,是使用 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,它由一位名叫“Ahmet Alp Balkan”的谷歌开发人员创建,他在 Kubernetes / Google Cloud 平台开发经验团队中构建此类工具。我强烈推荐使用它。
链接: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

3

2

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

1

请注意最新的(docker 19.03)docker context命令

Ajeet Singh Raina在“Docker 19.03.0 Pre-Release: 快速上下文切换,无根Docker,Swarm服务的Sysctl支持”中进行了说明。

Context Switching

一个上下文基本上是您用于访问特定群集的配置。例如,在我的情况下,我有4个不同的群集-混合了Swarm和Kubernetes在本地和远程运行。假设我在桌面机器上运行默认群集,在Google Cloud Platform上运行2个节点Swarm群集,在Play with Docker playground上运行5个节点群集以及在Minikube上运行单节点Kubernetes群集,并且我需要经常访问它们。使用docker context CLI,我可以轻松地在几秒钟内从一个群集(可能是我的开发群集)切换到测试或生产群集。
$ 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
NAME                DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT                 ORCHESTRATOR
default *           Current DOCKER_HOST based configuration   unix:///var/run/docker.sock   https://127.0.0.1:16443 (default)   swarm
swarm-context1 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接