# 使用 kubectl 查看和切换 context 前面的节,我们分别使用 minikube 和 kind 创建了不同名字的 k8s 集群,kubectl 是和哪个集群通讯的呢?可以通过以下命令先看下全局信息 ```bash kubectl config view ``` 可以看到输出了很多有用的信息: ```yaml apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: https://127.0.0.1:60712 name: kind-hello-k8s - cluster: certificate-authority-data: DATA+OMITTED server: https://127.0.0.1:50225 name: kind-test - cluster: certificate-authority: /Users/feilong/.minikube/ca.crt extensions: - extension: last-update: Wed, 16 Mar 2022 19:13:01 CST provider: minikube.sigs.k8s.io version: v1.25.2 name: cluster_info server: https://127.0.0.1:49371 name: minikube contexts: - context: cluster: kind-hello-k8s user: kind-hello-k8s name: kind-hello-k8s - context: cluster: kind-test user: kind-test name: kind-test - context: cluster: minikube extensions: - extension: last-update: Wed, 16 Mar 2022 19:13:01 CST provider: minikube.sigs.k8s.io version: v1.25.2 name: context_info namespace: default user: minikube name: minikube current-context: kind-hello-k8s kind: Config preferences: {} users: - name: kind-hello-k8s user: client-certificate-data: REDACTED client-key-data: REDACTED - name: kind-test user: client-certificate-data: REDACTED client-key-data: REDACTED - name: minikube user: client-certificate: /Users/feilong/.minikube/profiles/minikube/client.crt client-key: /Users/feilong/.minikube/profiles/minikube/client.key ``` 主要包括这几类信息: * `clusters` 列出了所有的 k8s 集群,例如这里有两个kind创建的集群:`kind-hello-k8s` 和 `kind-test` 以及一个 minikube 创建的集群`minikube` * `context` 列出了每个 k8s 集群对应的上下文信息 * `users` 列出了每个 k8s 集群的用户信息 * `current-context` 指定了当前 kubectl 连接的集群上下文,这里是 `kind-hello-k8s` 首先,通过`kubectl config get-contexts` 查看下有哪些集群上下文,通过`kubectl config current-context` 确认当前 k8s 集群上下文 ![](https://gitcode.net/csdn/skill_tree_cloud_native/-/raw/master/data/1.云原生初阶/2.容器编排(学习环境 k8s)/4.kubectl查看和切换context/img/config_get.png) 其次,通过`kubectl get nodes`查看当前 kubectl 连接的集群`kind-hello-k8s`的节点列表: ![](https://gitcode.net/csdn/skill_tree_cloud_native/-/raw/master/data/1.云原生初阶/2.容器编排(学习环境 k8s)/4.kubectl查看和切换context/img/kubectl_get_nodes.png) 现在,通过`kubectl config use-context kind-test` 切换连接的 k8s 集群,然后再次查看信息 ![](https://gitcode.net/csdn/skill_tree_cloud_native/-/raw/master/data/1.云原生初阶/2.容器编排(学习环境 k8s)/4.kubectl查看和切换context/img/kubectl_switch_context.png) 以下说法错误的是? ## 答案 kubectl 只能连接最后一次创建的 k8s 集群 ## 选项 ### A kubectl 可以切换连接的集群上下文 ### B kubectl 查看当前连接的 k8s 集群节点信息 ### C kubectl 可以查看环境里装的不同的集群信息