前言

随着 Kubernetes 越来越流行,不管大公司还是小公司都往 Kubernetes 迁移,每个公司最少有两套集群(测试和生产),但是多个集群就有多个 Kubeconfig 用户授权文件。虽然官方文档中有介绍多个 Kubeconfig 文件合并成一个 Kubeconfig,但是对于一些新手来说,看得不是很明白。如果需要从本地计算机连接访问 Kubernetes 集群,需要下载 kubectl 和获取集群访问凭证,但如果有多个 k8s 集群通过 2 个神器就可以做到轻松切换不同的 kubeconfig 环境,这里推荐 kubecmk8slens

更新历史

2022 年 05 月 18 日 - 初稿

阅读原文 - https://wsgzao.github.io/post/kubeconfig/


安装 kubectl

https://kubernetes.io/docs/tasks/tools/

https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/

1
2
3
4
5
6
7
8
9
curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl


# If you do not have root access on the target system, you can still install kubectl to the ~/.local/bin directory:
chmod +x kubectl
mkdir -p ~/.local/bin
mv ./kubectl ~/.local/bin/kubectl
# and then append (or prepend) ~/.local/bin to $PATH

kubeconfig

https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/

Kubeconfig 用途

kubectl 命令行工具通过 kubeconfig 文件的配置来选择集群以及集群 API Server 通信的所有信息。kubeconfig 文件用来保存关于集群用户、命名空间和身份验证机制的信息。默认情况下 kubectl 读取 $HOME/.kube/config 文件,也可以通过设置环境变量 KUBECONFIG 或者 –kubeconfig 指定其他的配置文件。

Kubeconfig 文件结构

kubeconfig 文件主要由下面几部分构成:

  • 集群参数
  • 用户参数
  • 上下文参数
  • 当前上下文
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: v1
kind: Config
preferences: {}

clusters: # 集群参数
- cluster:
name: {cluster-name}

users: # 用户参数
- name: {user-name}

contexts: # 上下文参数
- context:
cluster: {cluster-name}
user: {user-name}
name: kubernetes # 集群上下文名称
current-context: kubernetes # 当前上下文

kubecm

https://github.com/sunny0826/kubecm
https://kubecm.cloud/#/zh-cn/install

通过 kubecm 工具合并多个 kubeconfig 文件

kubecm 安装

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
# install kubecm
# https://github.com/sunny0826/kubecm/releases
export VERSION="0.16.4"

# linux x86_64
curl -Lo kubecm.tar.gz https://github.com/sunny0826/kubecm/releases/download/v${VERSION}/kubecm_${VERSION}_Linux_x86_64.tar.gz
# macos
curl -Lo kubecm.tar.gz https://github.com/sunny0826/kubecm/releases/download/v${VERSION}/kubecm_${VERSION}_Darwin_x86_64.tar.gz
brew install kubecm

# windows
curl -Lo kubecm.tar.gz https://github.com/sunny0826/kubecm/releases/download/v${VERSION}/kubecm_${VERSION}_Windows_x86_64.tar.gz

# linux & macos
tar -zxvf kubecm.tar.gz kubecm
cd kubecm
sudo mv kubecm /usr/local/bin/

# merge and generate kubeconfig
# https://kubecm.cloud/#/en-us/cli/kubecm_add

# 把需要合并的 Kubeconfig 文件放到 kubeconfig 目录下,执行命令后会在当前路径下产生一个新的 kubeconfig 文件
kubecm merge -f kubeconfig

# replace $HOME/.kube/config
kubecm merge -f kubeconfig -c

# switch k8s cluster
kubecm switch

# list kubeconfig
kubecm list



Manage your kubeconfig more easily.


██ ██ ██ ██ ██████ ███████ ██████ ███ ███
██ ██ ██ ██ ██ ██ ██ ██ ████ ████
█████ ██ ██ ██████ █████ ██ ██ ████ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██████ ██████ ███████ ██████ ██ ██

Tips Find more information at: https://kubecm.cloud

Usage:
kubecm [command]

Available Commands:
add Add KubeConfig to $HOME/.kube/config
alias Generate alias for all contexts
clear Clear lapsed context, cluster and user
completion Generates bash/zsh completion scripts
create Create new KubeConfig(experiment)
delete Delete the specified context from the kubeconfig
help Help about any command
list List KubeConfig
merge Merge the KubeConfig files in the specified directory
namespace Switch or change namespace interactively
rename Rename the contexts of kubeconfig
switch Switch Kube Context interactively
version Print version info

Flags:
--config string path of kubeconfig (default "$HOME/.kube/config")
-h, --help help for kubecm

Use "kubecm [command] --help" for more information about a command.

k8slens

GUI 界面切换 kubeconfig 就更强大了,自己探索吧

https://k8slens.dev/
https://docs.k8slens.dev/main/

Lens - The Kubernetes IDE

Lens - The Kubernetes IDE (“Lens IDE”) is a distribution of the OpenLens repository with Team Lens specific customizations released under a traditional EULA.

Lens IDE provides the full situational awareness for everything that runs in Kubernetes. It’s lowering the barrier of entry for people just getting started and radically improving productivity for people with more experience.

Lens IDE a standalone application for MacOS, Windows and Linux operating systems. You can download it free of charge for Windows, MacOS, and Linux from Lens IDE website.

文章目录
  1. 1. 前言
  2. 2. 更新历史
  3. 3. 安装 kubectl
  4. 4. kubeconfig
  5. 5. kubecm
  6. 6. k8slens