kubectl管理多个集群配置

2022-01-07 09:36:02 浏览数 (1)

kubectl管理多个集群配置

需求描述:

在一台机器上通过kubectl管理多个Kubernetes集群。

操作过程:将各集群的kubectl config文件中的证书内容转换,通过命令创建config文件;通过上下文切换使用不同集群。

代码语言:javascript复制
root@hello:~/.kube# ll
total 44
drwxr-xr-x 3 root root 4096 Jan 6 16:23 ./
drwx------ 21 root root 4096 Jan 6 16:22 ../
drwxr-x--- 4 root root 4096 Jan 6 14:50 cache/
-rw-r--r-- 1 root root 6252 Jan 6 16:21 config1
-rw-r--r-- 1 root root 6254 Jan 6 16:22 config2
root@hello:~/.kube#
root@hello:~/.kube#
复制代码

准备配置文件

代码语言:javascript复制
root@hello:~/.kube# cat config1
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR1RENDQXFDZ0F3SUJBZ0lVSlF3R05rQS9BaGxLYVpEcS9oaVpQNStteVJ3d0RRWUpLb1pJaHZjTkFRRUwKQlFBd1lURUxNQWtHQTFVRUJoTUNRMDR4RVRBUEJnTlZCQWdUQ0VoaGJtZGFhRzkxTVFzd0NRWURWUVFIRXdKWQpVekVNTUFvR0ExVUVDaE1EYXpoek1ROHdEUVlEVlFRTEV3WlRlWE4wWlcweEV6QVJCZ05WQkFNVENtdDFZbVZ5CmJtVjBaWE13SUJjTk1qRXhNakF6TURJME1EQXdXaGdQTWpFeU1URXhNRGt3TWpRd01EQmFNR0V4Q3pBSkJnTlYKQkFZVEFrTk9NUkV3RHdZRFZRUUlFd2hJWVc1bldtaHZkVEVMTUFrR0ExVUVCeE1DV0ZNeEREQUtCZ05WQkFvVApBMnM0Y3pFUE1BMEdBMVVFQ3hNR1UzbHpkR1Z0TVJNd0VRWURWUVFERXdwcmRXSmxjbTVsZEdWek1JSUJJakFOCkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQXRvcisyNkVLY2VRZGE5eDZodXRoL0h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lU1pEd0hpYnY4V3AwRi8ranI1N2Q1K0p0cXgrOTlBSXZiUlM5U1JLMmc9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
    server: https://192.168.1.11:6443
  name: cluster1
contexts:
- context:
    cluster: cluster1
    user: dev-admin
  name: context-cluster1
current-context: context-cluster1
kind: Config
preferences: {}
users:
- name: dev-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQxekNDQXIrZ0F3SUJBZ0lVVFprVnpuSFYxMStjdVRWSnNqWHpUVDVOZHQ4d0RRWUpLb1pJaHZjTkFRRUwKQlFBd1lURUxNQWtHQTFVRUJoTUNRMDR4RVRBUEJnTlZCQWdUQ0VoaGJtZGFhRzkxTVFzd0NRWURWUVFIRXdKWQpVekVNTUFvR0ExVUVDaE1EYXpoek1ROHdEUVlEVlFRTEV3WlRlWE4wWlcweEV6QVJCZ05WQkFNVENtdDFZbVZ5CmJtVjBaWE13SUJjTk1qRXhNakF6TURJME1EQXdXaGdQTWpBM01URXhNakV3TWpRd01EQmFNR2N4Q3pBSkJnTlYKQkFZVEFrTk9NUkV3RHdZRFZRUUlFd2hJWVc1bldtaHZkVEVMTUFrR0ExVUVCeE1DV0ZNeEZ6QVZCZ05WQkFvVApEbk41YzNSbGJUcHRZWE4wWlhKek1ROHdEUVlEVlFRTEV3WlRlWE4wWlcweERqQU1CZ05WQkFNVEJXRmtiV2x1Ck1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBbXNKUHBvdEcyNVE4bExyNC9NK3MKdVYzdWduQU14ZWRKYldFQmcxem81UGtyVW8wTUpDUEkyMTgrby9yTHh2eWJ1SlJKRm5qZlJMWlBZNmYrTGZTKwpJQmppbHJQN3J2OHdLMTh1V0EvNVdoWWNQeUZZYTZKeTVRM1RFdkZBYkdLVU5FUjBiWUhNOXdmTGJhVWNmdGkyCnI5dEd5TFVPYzBpemJ5QkFPZFU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h2aWNGWUxtME50bFlUM3pUZjVjVC9kRGJSdWhSRFF2clkKaVpnUHo0ZHg4YTZibFA3SkRmeTZMWTVXZmtBMFAxdWVtS05wR29pK1BHRDRFbGluRWd1aW9tbUtsWEowZ1pZTQpiNHNBbzJlWGY1ZGxQdkZxK0lJbzlYeWVzSGR1RDFWQ3dpRitudUZ4QmdlNnI2elQ4ZGFaL2NLMGZRSURBUUFCCm8yWXdaREFPQmdOVkhROEJBZjhFQkFNQ0FRWXdFZ1lEVlIwVEFRSC9CQWd3QmdFQi93SUJBakFkQmdOVkhRNEUKRmdRVTRhZlRpTE9MNVV1TUcrOS95ZXZGUVMxWkJEMHdId1lEVlIwakJCZ3dGb0FVNGFmVGlMT0w1VXVNRys5Lwp5ZXZGUVMxWkJEMHdEUVlKS29aSWh2Y05BUUVMQlFBRGdnRUJBRHQ1SlpqZjRoRS9VQVRCc3ZMUXphOFY5bkR4ClVZdStpaFVVTWRDenJQSkV0WXlMYWFQZXppenBicWFpU3YrblBoR2UxSndXMThIZmlsS0dsOTlCVENkc3VHSjUKVzhVTCtHbHdvVHZSRjNaK0F6M3NQL3dBelB1Vk5ORzZwVkJkanNSbDhhN29UMWV0RjM0UUovWWtOVFR4M1JrQQpRZjhqYXdZams3Wi9pL0VqM3hmd0FxRkhzT1Q2MjlXMnc0VU9SaVZBeHZuc2czWUozZ3RyVFRBK2hkVGhUWDViCkxpQjN5ZFFPUWNRTUE0SU9CeG0vdkRrR3lGMVBBL1BjMTFWTDBjZktrK25WY3J4RHAxS2JtVXhmNkhua3JqWlkKaGpNWEFqRDJIL0MzT2JjQUFMNXF6aUNKU1pDM0xDMVNwNEtKUGdMZVJPc1haZWpzUXRTRWN6YWhTRE09Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
    server: https://192.168.1.12:6443
  name: cluster2
contexts:
- context:
    cluster: cluster2
    user: test-admin
  name: context-cluster2
current-context: context-cluster2
kind: Config
preferences: {}
users:
- name: test-admin
  user:
    client-certificate-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ajJhcjF0amg2am54Q3ZrSkFYYkxGOEdzalE0OGVlV2V2S2VMUgpkZERSMTVxY1hJQ09nTUJBR2tJQmxQMC85bXBpMmZDbUozb1VCSURPVUFta3pmMkthSVVJb3VKWGlsK3BTYXRGCjBFTitsK1hucDFWWGJnVi9ia093cG1ZdHpxUHVQamdBWkFETmxpTUNnWUJ3dGpMK1RHY1NIU1VHczdLYjg1QkoKZElDMi9QMXVwMG90NzhDN2drSGZrQ3F4NUZXUzNaREdHZTI1OFplL3ZyWDJ0NklhQjIzcFBPYUh4ODhBVFVscQpMdGxJNTA4bXFabDVUc2R0YnFvdjlYdTRqRlg3ZlRWMCtFYWk3d0JxTDNxRjh0a1YxL1BsNGRacjkvQUVNbDNqCmY1U0wwclBmL2lBb0s1YVdlWDYyZlFLQmdHT3NEN1FtQklqbzVEVXV4UjU5ZWlRYnRuanFDZWFpaTBvQ2FHZzQKR2IxZXc5eWxFRHU5RkcwM3BsbjZlNFdXTStPbzJsdVNqd0xpcFNIWThpdTN4RnFidUJVQiszb294dVRSVDZLVgprUUJsU2syemhWbEIrZ1d0U1d5LzlhVmp2NHJiWGhMNEVPdEtNS3NGWHFkWTMxK09EbWJEcEd6QjUzQmxEdE1HCmk5TVBBb0dCQUl4dndic2t6dUFhWHJNbTJoaXdwTVNscElLWjhZY2lUUjhtWHFHMTV6NGt2Wi9Hcmd4SzR4a3YKRjdmRlRtQzlqSFJzS0lqZ28rNkoxVG5QalVKeUlJdTVPWWVJL2RvelBPakllczE2NkZRbThSY3hTdGExOU91dAp1N3BOSjhlRHdVYmJKbG01WTdOOFc5cmJyRzh3VkZvRDhmSU5yZXBSTVo2elFLeEVmQUdHCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
root@hello:~/.kube# 
root@hello:~/.kube# 
root@hello:~/.kube#
复制代码

修改配置中:

代码语言:javascript复制
#配置1
- cluster:
    server: https://192.168.1.11:6443
  name: cluster1
  
contexts:
- context:
    cluster: cluster1
    user: dev-admin
  name: context-cluster1

users:
- name: dev-admin


#配置2
- cluster:
    server: https://192.168.1.12:6443
  name: cluster2
  
contexts:
- context:
    cluster: cluster2
    user: test-admin
  name: context-cluster2

users:
- name: test-admin
复制代码

写入配置文件

代码语言:javascript复制
root@hello:~/.kube# KUBECONFIG=config1:config2 kubectl config view --flatten > $HOME/.kube/config
root@hello:~/.kube# 
root@hello:~/.kube#
复制代码

测试配置

代码语言:javascript复制
root@hello:~/.kube# kubectl config get-contexts
CURRENT   NAME               CLUSTER    AUTHINFO     NAMESPACE
*         context-cluster1   cluster1   dev-admin    
          context-cluster2   cluster2   test-admin   
root@hello:~/.kube# 
root@hello:~/.kube# kubectl config current-context
context-cluster1
root@hello:~/.kube# 
root@hello:~/.kube# kubectl get node
NAME           STATUS   ROLES    AGE   VERSION
192.168.1.11   Ready    master   34d   v1.22.2
root@hello:~/.kube# 
root@hello:~/.kube# kubectl config use-context context-cluster2
Switched to context "context-cluster2".
root@hello:~/.kube# 
root@hello:~/.kube# kubectl get node
NAME           STATUS                     ROLES    AGE   VERSION
192.168.1.12   Ready,SchedulingDisabled   master   34d   v1.22.2
192.168.1.13   Ready                      node     34d   v1.22.2
192.168.1.14   Ready                      node     34d   v1.22.2
root@hello:~/.kube#
复制代码

附录

代码语言:javascript复制
current-context 显示 current_context
delete-cluster  删除 kubeconfig 文件中指定的集群
delete-context  删除 kubeconfig 文件中指定的 context
get-clusters    显示 kubeconfig 文件中定义的集群
get-contexts    描述一个或多个 contexts
rename-context  Renames a context from the kubeconfig file.
set             设置 kubeconfig 文件中的一个单个值
set-cluster     设置 kubeconfig 文件中的一个集群条目
set-context     设置 kubeconfig 文件中的一个 context 条目
set-credentials 设置 kubeconfig 文件中的一个用户条目
unset           取消设置 kubeconfig 文件中的一个单个值
use-context     设置 kubeconfig 文件中的当前上下文
view            显示合并的 kubeconfig 配置或一个指定的 kubeconfig 文件
复制代码

blog.csdn.net/qq_33921750

my.oschina.net/u/3981543

www.zhihu.com/people/chen…

segmentfault.com/u/hppyvyv6/…

juejin.cn/user/331578…

space.bilibili.com/352476552/a…

cloud.tencent.com/developer/c…

www.jianshu.com/u/0f894314a…

www.toutiao.com/c/user/toke…

知乎、CSDN、开源中国、思否、掘金、哔哩哔哩、腾讯云、简书、今日头条

0 人点赞