config_view.md 3.3 KB
Newer Older
F
feilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
# 使用 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 集群上下文
F
feilong 已提交
78
![](https://gitcode.net/csdn/skill_tree_cloud_native/-/raw/master/data/1.云原生初阶/2.容器编排(学习环境 k8s)/4.kubectl查看和切换context/img/config_get.png)
F
feilong 已提交
79 80

其次,通过`kubectl get nodes`查看当前 kubectl 连接的集群`kind-hello-k8s`的节点列表:
F
feilong 已提交
81
![](https://gitcode.net/csdn/skill_tree_cloud_native/-/raw/master/data/1.云原生初阶/2.容器编排(学习环境 k8s)/4.kubectl查看和切换context/img/kubectl_get_nodes.png)
F
feilong 已提交
82 83

现在,通过`kubectl config use-context kind-test` 切换连接的 k8s 集群,然后再次查看信息
F
feilong 已提交
84
![](https://gitcode.net/csdn/skill_tree_cloud_native/-/raw/master/data/1.云原生初阶/2.容器编排(学习环境 k8s)/4.kubectl查看和切换context/img/kubectl_switch_context.png)
F
feilong 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

以下说法错误的是?

## 答案

kubectl 只能连接最后一次创建的 k8s 集群

## 选项

### A

kubectl 可以切换连接的集群上下文

### B

kubectl 查看当前连接的 k8s 集群节点信息

### C

kubectl 可以查看环境里装的不同的集群信息