|NO.Z.00382|——————————|CloudNative|——|KuberNetes&CI/CD.V20|——|Jenkins.v08|kubeconfig多集群配置.v02|
一、查看k8s集群配置
### --- 查看kubeconfig文件配置信息
[root@k8s-master01 ~]# cat ~/.kube/config
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUd****************
server: https://192.168.1.11:6443
name: kubernetes # 保存了一个集群,名称就是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: LS0tLS1CRUdJ****************
client-key-data: LS0tLS1CRUdJTiBSU0Eg****************
二、新添加k8s集群:test
### --- 新添加一个k8s集群:test
~~~ # 查看集群地址
[root@k8s-master01 pki]# pwd
/etc/kubernetes/pki
[root@k8s-master01 pki]# kubectl cluster-info
Kubernetes control plane is running at https://192.168.1.11:6443
CoreDNS is running at https://192.168.1.11:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
~~~ # 拷贝kubeconfig文件到当前目录下
[root@k8s-master01 pki]# cp ~/.kube/config ./multi-cluster.yaml
~~~ # 创建一个k8s集群test自定义一个kubeconfig文件,创建多个集群
[root@k8s-master01 pki]# kubectl config set-cluster test --certificate-authority=ca.pem --embed-certs=true --server=https://192.168.1.11:6443 --kubeconfig=multi-cluster.yaml
Cluster "test" set.
### --- 为test集群配置用户
~~~ # 为k8s-test集群设置用户
~~~ 集群、用户的名称不可以设置为一样的,设置为一样的,会把之前的覆盖掉
[root@k8s-master01 pki]# kubectl config set-credentials test-admin --client-certificate=admin.pem --client-key=admin-key.pem --embed-certs=true --kubeconfig=multi-cluster.yaml
User "test-admin" set.
### --- 为test集群设置上下文
~~~ # 为k8s-test集群设置上下文
[root@k8s-master01 pki]# kubectl config set-context test --cluster=test --user=test-admin --kubeconfig=multi-cluster.yaml
Context "test" created.
### --- 查看test集群创建结果
[root@k8s-master01 pki]# cat multi-cluster.yaml
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZ
server: https://192.168.1.11:6443
name: kubernetes # 第一次创建的k8s集群:kubernetes
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJ
server: https://192.168.1.11:6443
name: test # 新创建的k8s集群
contexts:
- context:
cluster: kubernetes
user: kubernetes-admin
name: kubernetes-admin@kubernetes
- context:
cluster: test
user: test-admin
name: test
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ
client-key-data: LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLR
- name: test-admin
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ
client-key-data: LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLR
### --- kubernetes和test这2个集群之间切换
~~~ # 切换到test集群下
[root@k8s-master01 pki]# kubectl --kubeconfig=multi-cluster.yaml config use-context test
Switched to context "test".
~~~ # 查看当前集群下的pod状态
~~~ --kubeconfig=multi-cluster.yaml若是不添加这个参数的话,默认读的是 ~/.kube/config这个文件
[root@k8s-master01 pki]# kubectl get po --kubeconfig=multi-cluster.yaml
~~~ # 切换到默认kubernetes集群下
~~~ 默认集群没有设置Context;所以不能切换
[root@k8s-master01 pki]# kubectl --kubeconfig=multi-cluster.yaml config use-context kubernetes
error: no context exists with the name: "kubernetes"
三、新添加k8s集群:uat
### --- 新添加一个集群为uat
~~~ # 创建一个k8s集群test自定义一个kubeconfig文件,创建多个集群
[root@k8s-master01 pki]# kubectl config set-cluster uat --certificate-authority=ca.pem --embed-certs=true --server=https://192.168.1.11:6443 --kubeconfig=multi-cluster.yaml
Cluster "uat" set.
### --- 为uat集群配置用户
~~~ # 为k8s-test集群设置用户
~~~ 集群、用户的名称不可以设置为一样的,设置为一样的,会把之前的覆盖掉
[root@k8s-master01 pki]# kubectl config set-credentials uat-admin --client-certificate=admin.pem --client-key=admin-key.pem --embed-certs=true --kubeconfig=multi-cluster.yaml
User "uat-admin" set.
### --- 为uat集群设置上下文
~~~ # 为k8s-test集群设置上下文
[root@k8s-master01 pki]# [root@k8s-master01 pki]# kubectl config set-context uat --cluster=uat --user=uat-admin --kubeconfig=multi-cluster.yaml
Context "uat" created.
四、集群之间切换:kubernetes、test、uat、
### --- 集群之间切换
~~~ # 切换到uat集群下
[root@k8s-master01 pki]# kubectl --kubeconfig=multi-cluster.yaml config use-context uat
Switched to context "uat".
~~~ # 切换到test集群下
[root@k8s-master01 pki]# kubectl --kubeconfig=multi-cluster.yaml config use-context test
Switched to context "test".
五、创建集群管理:secrets
### --- 创建集群管理:secrets
~~~ # 切换到test集群下
[root@k8s-master01 pki]# kubectl --kubeconfig=multi-cluster.yaml config use-context test
Switched to context "test".
~~~ # 创建secret
~~~ 这个secret会挂载到kubectl下的root下的kubeconfig目录下,用于管理多集群
[root@k8s-master01 pki]# kubectl create secret generic multi-kube-config --from-file=multi-cluster.yaml
secret/multi-kube-config created
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通