Kubectl多集群配置
# Kubectl多集群配置
# 前言
- Kubectl 是一个命令行接口,用于对 Kubernetes 集群运行命令。
- Kubectl 在
$HOME/.kube
目录中寻找一个名为 config 的文件。 - 你可以通过设置环境变量 KUBECONFIG 或设置 --kubeconfig 参数指定其它 kubeconfig 文件。
# 基本配置
- 当集群初始化成功后,要使 kubectl 适用于非 root 用户,则需要运行以下命令,这些命令也是 kubeadm init输出的一部分:
- 即创建.kube目录 -> 拷贝配置文件 -> 用户授权
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
- 当然,如果你是 root 用户,则可以直接添加环境变量:
$ export KUBECONFIG=/etc/kubernetes/admin.conf
# 多集群配置
# 查看kubeconfig
同样需要新建.kube
文件夹,拷贝不同集群的config,例如重命名为config-ceph
和config-kubevirt
,分别查看配置文件。
需要对相应的参数进行修改:
[root@jenkins .kube]# cat config
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSU******
server: https://10.88.88.147:6443
name: kubernetes #这里需要修改
contexts:
- context:
cluster: kubernetes #这里需要修改
user: kubernetes-admin #这里需要修改
name: kubernetes-admin@kubernetes #这里需要修改
current-context: kubernetes-admin@kubernetes #这里需要修改
kind: Config
preferences: {}
users:
- name: kubernetes-admin #这里需要修改
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZ******
client-key-data: LS0tLS1CRUdJTiBSU0E******
# 修改kubeconfig
两份配置文件中context、cluster、users
名称必须不一样(同集群设置不同环境除外),否则调用将会出错,所以我们可以适当修改配置文件。
如下是第一份kubevirt集群的:
[root@k8s-master:~/.kube] # cat config-kubevirt
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJ******
server: https://10.11.121.113:6443
name: k8s-kubevirt
contexts:
- context:
cluster: k8s-kubevirt
user: k8s-kubevirt
name: k8s-kubevirt
current-context: k8s-kubevirt
kind: Config
preferences: {}
users:
- name: k8s-kubevirt
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJU******
client-key-data: LS0tLS1CRUdJTiBSU0EgUFJJ******
如下是ceph集群的:
[root@k8s-master01 .kube]# cat config-ceph
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiB******
server: https://10.11.121.117:6443
name: k8s-ceph
contexts:
- context:
cluster: k8s-ceph
user: k8s-ceph
name: k8s-ceph
current-context: k8s-ceph
kind: Config
preferences: {}
users:
- name: k8s-ceph
user:
client-certificate-data: LS0tLS1CRUdJ******
client-key-data: LS0tLS1CRUdJTiBSU0******
# 生成kubeconfig
执行以下命令生成新的config文件即可
[root@k8s-master:~/.kube] # KUBECONFIG=config-ymt:config-jhmy kubectl config view --flatten > $HOME/.kube/config
[root@k8s-master:~/.kube] # kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://10.11.121.117:6443
name: k8s-ceph
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://10.11.121.113:6443
name: k8s-kubevirt
contexts:
- context:
cluster: k8s-ceph
user: k8s-ceph
name: k8s-ceph
- context:
cluster: k8s-kubevirt
user: k8s-kubevirt
name: k8s-kubevirt
current-context: k8s-ceph
kind: Config
preferences: {}
users:
- name: k8s-ceph
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
- name: k8s-kubevirt
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
# 测试集群切换
这里使用上下文的方式
切换kubernetes集群,查看当前的集群节点如下:
[root@k8s-master:~/.kube] # kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane,master 9h v1.22.1
k8s-node Ready <none> 9h v1.22.1
[root@k8s-master:~/.kube] # kubectl config current-context
k8s-kubevirt
使用上下文切换集群并且查看是否已经切换成功。
[root@k8s-master:~/.kube] # kubectl config use-context k8s-ceph
Switched to context "k8s-ceph".
[root@k8s-master:~/.kube] # kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master01 Ready control-plane,master 7h54m v1.22.1
k8s-worker01 Ready <none> 7h53m v1.22.1
k8s-worker02 Ready <none> 7h53m v1.22.1
上次更新: 2023/11/28, 22:03:59