跳转至

使用kubeconfig管理多集群方法

一、 k8s集群部署

准备多套k8s集群,可以使用kubeadm部署多套k8s集群。

# wget  https://github.com/labring/sealos/releases/download/v4.1.7/sealos_4.1.7_linux_amd64.tar.gz  && \
    tar -zxvf sealos_4.1.7_linux_amd64.tar.gz sealos &&  chmod +x sealos && mv sealos /usr/bin
[root@k8s-master01 ~]# cat sealos-install.sh
sealos run labring/kubernetes:v1.25.0 labring/helm:v3.8.2 labring/calico:v3.24.1 \
     --masters 192.168.10.140 \
     --nodes 192.168.10.141,192.168.10.142 -p centos
[root@k8s-master01 ~]# cat sealos-install.sh
sealos run labring/kubernetes:v1.25.0 labring/helm:v3.8.2 labring/calico:v3.24.1 \
     --masters 192.168.10.160 \
     --nodes 192.168.10.161,192.168.10.162 -p centos

二、K8S多集群管理

2.1 获取多集群kubeconfig文件

创建文件存储目录
[root@k8s-master01 ~]# mkdir kubeconfigdir
获取第一个集群kubeconfig文件
[root@k8s-master01 ~]# cp /root/.kube/config kubeconfigdir/config1
获取第二个集群kubeconfig文件
[root@k8s-master01 ~]# scp 192.168.10.160:/root/.kube/config kubeconfigdir/config2

2.2 合并多集群kubeconfig文件

可以选择手动合并,也可以选择自动全并。本案例为手动合并。

自动合并命令:# KUBECONFIG=./config1:./config2 kubectl config view --flatten > ./configX,前提需要修改config文件。

合并后kubeconfig文件如下

[root@k8s-master01 kubeconfigdir]# cat config12
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvakNDQWVhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJek1ERXdOVEExTWpJek1sb1hEVE16TURFd01qQTFNakl6TWxvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2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nFNQlVHQTFVZEVRUU8KTUF5Q0NtdDFZbVZ5Ym1WMFpYTXdEUVlKS29aSWh2Y05BUUVMQlFBRGdnRUJBS3lYMmJ4d2cySVYxUEE0OThaNApaU290VFljNFZMc3hvdUNwNUY0U2dGSVIrc0pEWVc5R1RTRFpXbW9aczlQaE4vOFpEdDFQVjJMdU1waG00YStkCm83OW9PME10OHhvMGNsdGQzaUZXWkNRK0lzcHJoNUVTd0VuSmdCYmpDWGxObCs4T256VUJaQVRtZWw4dnY4LysKektITDdTNE03K1NOclN4ZGE1VHkwU2M4Mi8wS0RoUmZDMWtOZkhFMStuNWw4d2JVa1BmNDJkanY1eEFac2pQUwo1d1VTOCs4QXBQM0ZWNkNhRmJBdUlRbXFUL1pDdG9GYUZVVGxwUEFnRjBPa1AzelVXaTBrQzR3eG5vUVRXaW4yCnhEYWkvdmRPU1Qyd2FnU3dKOXRZd3ZsQmg4ZmQwdmM3bDlvMGREeklBYlh4OTBvdkZ0bzVCZ0VsT0FrQ3hTTisKc0E0PQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
    server: https://192.168.10.160:6443
  name: member2
contexts:
- context:
    cluster: member1
    user: member1-admin
  name: member1
- context:
    cluster: member2
    user: member2-admin
  name: member2
current-context: member1
kind: Config
preferences: {}
users:
- name: member1-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURJVENDQWdtZ0F3SUJBZ0lJR3ljSnc3LytXNGt3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TXpBeE1EVXdOVEl5TXpKYUZ3MHlOREF4TURVd05USXlNelJhTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJD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Vmk3VDU1ZWIzRVdRbElsdExKUTdQK1R3YnBheUFMamZqcDVRTGtZUEcvY1UwNnAycFhXOHU0L3dsUk5YbWVmCklkRGFpOTJPNzltOG4rZ1d3b1JzL3g1b21XamE4azkxR1NuV3RPVUNnWUVBa3dIWDJtU1RVbmJGRFR6UWdwYUsKeDA4aDZjM2ZTZFRxcjRrRWN6Zno5ampzUjIvOE4rWHNPc2luRTF2VU9wV2g5OEh3VEoyeW51bjJQZS8xdTluaAprcUZ2YUx2MHdleDQzdFVmeUtVNzRHUStZNHkrVTBmMVJ5VEsrUVVCU1Y2YmtvdW1NNXl4SzhPbDQ4cmtVa2FyCmhDTHN0bHRpK1dsYVZiNjYzSjFhR1lFQ2dZQjU4MWR2OTZrMTkrcG11akk1Ly9LSUk5bmNHQ2p0OEhTMm1DOTgKU0RkYktCM05VRVhOS0FoMWdJL1hUb2p4MVJ2WHQ4T2tFM1BPeFBYTEhOc1oxVHBaSEc2djNhYVBab3JuTUhmNwovRHllVWdoU2VtWkIxQ2d0Y2ErWUNGM1JiZzZ4OXBSVTRWTDBzZVRWM0NTQWFrSjdzY1FhMlZNbWg4WW9BSzI0ClRoanBmUUtCZ0FSUWRsTTZWbVVDTE9iZTgvTFNCckxjbEg1enFwdkx2M2VEeDhuN1BKdTl5aC9jbnAvaFd0ZU0Kd1cyaTUzUTVNUnFManJ5Zk4ra0E0c3BTVk45cTVHVnRYSWE5M1JkOENsc045UEMyMG1OK0pYOUhaUThLb015MQpVNmMybVlyVFhpYm9yNVBPNlB0aUdRaTNkREZWVDhzTnhZd0FzenliankxYjZUQmVDc3lMCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
上述kubeconfig文件中clusternameusernamecontextname都可以根据企业实际情况进行替换
查看可管理的集群
[root@k8s-master01 kubeconfigdir]# kubectl --kubeconfig=config12 config get-clusters
NAME
member2
member1
查看可管理的集群上下文
[root@k8s-master01 kubeconfigdir]# kubectl --kubeconfig=config12 config get-contexts
CURRENT   NAME      CLUSTER   AUTHINFO        NAMESPACE
          member1   member1   member1-admin
*         member2   member2   member2-admin
查看当前上下文
[root@k8s-master01 kubeconfigdir]# kubectl --kubeconfig=config12 config current-context
member2
切换上下文
[root@k8s-master01 kubeconfigdir]# kubectl --kubeconfig=config12 config use-context member1
Switched to context "member1".
查看切换后当前上下文
[root@k8s-master01 kubeconfigdir]# kubectl --kubeconfig=config12 config current-context
member1

2.3 使用合并后kubeconfig管理多集群

查看member1集群kube-system命名空间中pod信息

[root@k8s-master01 ~]# kubectl --kubeconfig /root/kubeconfigdir/config12 config use-context member1
Switched to context "member1".

[root@k8s-master01 ~]# kubectl --kubeconfig /root/kubeconfigdir/config12 config current-context
member1

[root@k8s-master01 ~]# kubectl --kubeconfig /root/kubeconfigdir/config12 get pods -n kube-system
NAME                                   READY   STATUS    RESTARTS      AGE
coredns-57575c5f89-4nl9k               1/1     Running   3 (39h ago)   45h
coredns-57575c5f89-9hjsn               1/1     Running   3 (39h ago)   45h
etcd-k8s-master01                      1/1     Running   3 (39h ago)   45h
kube-apiserver-k8s-master01            1/1     Running   5 (39h ago)   45h
kube-controller-manager-k8s-master01   1/1     Running   3 (39h ago)   45h
kube-proxy-4wp45                       1/1     Running   3 (39h ago)   45h
kube-proxy-pxz6p                       1/1     Running   3 (39h ago)   45h
kube-proxy-wvx9g                       1/1     Running   3 (39h ago)   45h
kube-scheduler-k8s-master01            1/1     Running   3 (39h ago)   45h
查看member2集群kube-system命名空间中pod信息

[root@k8s-master01 ~]# kubectl --kubeconfig /root/kubeconfigdir/config12 config use-context member2
Switched to context "member2".

[root@k8s-master01 ~]# kubectl --kubeconfig /root/kubeconfigdir/config12 config current-context
member2

[root@k8s-master01 ~]# kubectl --kubeconfig /root/kubeconfigdir/config12 get pods -n kube-system
NAME                                   READY   STATUS    RESTARTS      AGE
coredns-57575c5f89-782l5               1/1     Running   3 (14h ago)   45h
coredns-57575c5f89-9tjcn               1/1     Running   3 (14h ago)   45h
etcd-k8s-master01                      1/1     Running   3 (14h ago)   45h
kube-apiserver-k8s-master01            1/1     Running   4 (12m ago)   45h
kube-controller-manager-k8s-master01   1/1     Running   3 (14h ago)   45h
kube-proxy-5nglp                       1/1     Running   3 (14h ago)   45h
kube-proxy-d7q6c                       1/1     Running   3 (14h ago)   45h
kube-proxy-dhr9d                       1/1     Running   3 (14h ago)   45h
kube-scheduler-k8s-master01            1/1     Running   3 (14h ago)   45h

2.4 kubectx

2.4.1 下载

image-20230413124940258

image-20230413124955713

image-20230413125024563

image-20230413125115514

# wget https://github.com/ahmetb/kubectx/releases/download/v0.9.4/kubectx

2.4.2 安装及使用

# ls
kubectx

# chmod +x kubectx

# mv kubectx /usr/local/bin/
# cp configX /root/.kube/config
# kubectx
member1
member2
如果默认在member1集群使用下面命令则会自动切换到member2
# kubectx -

Switched to context "member2".
再次使用kubectx将切换到member1
# kubectx -

Switched to context "member1".

三、使用kubeconfig实现在不同k8s集群中部署应用

3.1 在member1集群中部署应用

查看部署描述文件
[root@k8s-master01 nginxdir]# cat n1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx1                    # deployment名
spec:
  replicas: 2                                   # 副本集,deployment里使用了replicaset
  selector:
    matchLabels:
      app: nginx1                                # 匹配的pod标签,表示deployment和rs控制器控制带有此标签的pod
  template:                                         # 代表pod的配置模板
    metadata:
      labels:
        app: nginx1                              # pod的标签
    spec:
      containers:                               # 以下为pod里的容器定义
      - name: nginx1
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
确认所在目录
[root@k8s-master01 nginxdir]# pwd
/root/nginxdir

确认文件是否存在
[root@k8s-master01 nginxdir]# ls
n1.yaml

查看当前上下文
[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 config current-context
member2

切换当前上下文
[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 config use-context member1
Switched to context "member1".

部署nginx应用
[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 apply -f n1.yaml
deployment.apps/nginx1 created

查看部署的nginx对应的pod
[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 get pods
NAME                                     READY   STATUS              RESTARTS      AGE
nginx1-7bff55d4bd-nwztt                  1/1     Running             0             13s
nginx1-7bff55d4bd-rvhwp                  0/1     ContainerCreating   0             13s

切换上下文
[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 config use-context member2
Switched to context "member2".

确认member2集群中是否有部署nginx应用结果没有
[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 get pods

3.2 在member2集群中部署应用

查看部署描述文件
[root@k8s-master01 nginxdir]# cat n1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx1                    # deployment名
spec:
  replicas: 2                                   # 副本集,deployment里使用了replicaset
  selector:
    matchLabels:
      app: nginx1                                # 匹配的pod标签,表示deployment和rs控制器控制带有此标签的pod
  template:                                         # 代表pod的配置模板
    metadata:
      labels:
        app: nginx1                              # pod的标签
    spec:
      containers:                               # 以下为pod里的容器定义
      - name: nginx1
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
切换上下文
[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 config use-context member2
Switched to context "member2".

确认当前上下文
[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 config current-context
member2

执行nginx应用部署
[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 apply -f n1.yaml
deployment.apps/nginx1 created

确认nginx应用部署对应的pod是否运行结果运行
[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 get pods
NAME                                     READY   STATUS    RESTARTS      AGE
nfs-client-provisioner-dfd8488d7-rtmdj   1/1     Running   3 (14h ago)   45h
nginx1-7bff55d4bd-25vkz                  1/1     Running   0             8s
nginx1-7bff55d4bd-lfprt                  1/1     Running   0             8s
删除不同集群中已部署的应用

删除member2集群中的应用
[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 config use-context member2
Switched to context "member2".

[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 delete -f n1.yaml
deployment.apps "nginx1" deleted


删除member1集群中的应用
[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 config use-context member1
Switched to context "member1".

[root@k8s-master01 nginxdir]# kubectl --kubeconfig /root/kubeconfigdir/config12 delete -f n1.yaml
deployment.apps "nginx1" deleted

四、 持久化存储准备

提前准备好NFS服务,主要为后续k8s联邦做准备。

下载NFS做为后端持久化存储部署文件
# for file in class.yaml deployment.yaml rbac.yaml  ; do wget https://raw.githubusercontent.com/kubernetes-incubator/external-storage/master/nfs-client/deploy/$file ; done
替换的镜像registry.cn-beijing.aliyuncs.com/pylixm/nfs-subdir-external-provisioner:v4.0.0
本次NFS服务器IP地址及共享目录
192.168.10.166
/netshare