使用kubecm管理多集群kubeconfig
简介
如果需要使用 Kubectl 管理多个集群,通常做法是提取 Kubeconfig 中各个字段的内容,将其合并到 Kubectl 所在设备的 Kubeconfig 文件中,但该方式操作繁琐且容易出错。
借助 kubecm 工具,可以更简单高效的将多个集群访问凭证合并添加到 kubeconfig 中
安装
下载 二进制 文件
export VERSION=0.27.1
wget https://github.com/sunny0826/kubecm/releases/download/v${VERSION}/kubecm_v${VERSION}_Linux_x86_64.tar.gz
解压缩
tar xf kubecm_v${VERSION}_Linux_x86_64.tar.gz
移动二进制文件
mv kubecm /usr/local/bin
使用
配置命令自动补全
- bash
- zsh
kubecm completion bash > /etc/bash_completion.d/kubecm
kubecm completion zsh > "${fpath[1]}/_kubecm"
添加集群
:::caution注意
在执行添加操作的时候,.kube
目录下 config
文件必须存在,文件内容可以为空
:::
添加第一个集群
$ kubecm add -cf prod-config --context-name prod
Add Context: prod
「prod-config」 write successful!
+------------+---------+---------------+-----------------------+-----------------------------+--------------+
| CURRENT | NAME | CLUSTER | USER | SERVER | Namespace |
+============+=========+===============+=======================+=============================+==============+
| * | prod | kubernetes | 299665908796239593 | https://10.251.8.25:6443 | default |
+------------+---------+---------------+-----------------------+-----------------------------+--------------+
参数说明
参数 | 说明 |
---|---|
-c | 覆盖本地kubecconfig文件 |
-f | 指定要合并的文件 |
--context-name | 指定上下文名称 |
添加第二个集群
$ kubecm add -cf pre-config --context-name pre
Add Context: pre
「pre-config」 write successful!
+------------+---------+--------------------------+----------------------------------+------------------------------+--------------+
| CURRENT | NAME | CLUSTER | USER | SERVER | Namespace |
+============+=========+==========================+==================================+==============================+==============+
| | pre | kubernetes-695252f699 | 299665608709939513-695252f699 | https://10.251.8.25:6443 | default |
+------------+---------+--------------------------+----------------------------------+------------------------------+--------------+
| * | prod | kubernetes | 299665608709939513 | https://10.250.8.20:6443 | default |
+------------+---------+--------------------------+----------------------------------+------------------------------+--------------+
查看集群
$ kubecm ls
+------------+---------+--------------------------+----------------------------------+------------------------------+--------------+
| CURRENT | NAME | CLUSTER | USER | SERVER | Namespace |
+============+=========+==========================+==================================+==============================+==============+
| | pre | kubernetes-695252f699 | 299665608709939513-695252f699 | https://10.251.8.25:6443 | default |
+------------+---------+--------------------------+----------------------------------+------------------------------+--------------+
| * | prod | kubernetes | 299665608709939513 | https://10.250.8.20:6443 | default |
+------------+---------+--------------------------+----------------------------------+------------------------------+--------------+
Cluster check succeeded!
Kubernetes version v1.20.11-aliyun.1
Kubernetes master is running at https://10.250.8.20:6443
[Summary] Namespace: 10 Node: 2 Pod: 52
切换集群
:::tip说明
执行 kubecm switch
可以进行交互式选择
:::
切换集群到pre
kubecm switch pre
删除集群
$ kubecm delete qa
Context Delete:「qa」
「/Users/pptfz/.kube/config」 write successful!
+------------+---------+--------------------------+----------------------------------+------------------------------+--------------+
| CURRENT | NAME | CLUSTER | USER | SERVER | Namespace |
+============+=========+==========================+==================================+==============================+==============+
| | pre | kubernetes-695252f699 | 299665608709939513-695252f699 | https://10.251.8.25:6443 | default |
+------------+---------+--------------------------+----------------------------------+------------------------------+--------------+
| | prod | kubernetes | 299665608709939513 | https://10.250.8.20:6443 | default |
+------------+---------+--------------------------+----------------------------------+------------------------------+--------------+