什么是k8s上下文?kubeconfig配置文件讲解

目录

    • 前言
    • 上下文的概念
    • k8s上下文
    • kubectl命令行工具是如何连接k8s集群的?
    • 图解kubectlconfig配置文件
    • 使用kubectl config命令生成kubeconfig文件
    • 安装kubectl命令行工具
    • kubectl管理多个k8s集群

前言

环境:centos7.9 k8s 1.22.7

上下文的概念

在计算机领域中,开发工程师进行代码开发的时候,我们经常会听到上下这个概念,那么,什么是上下文?
上下文,英文单词是context,释义为:背景,环境,上下文,语境。
context上下文可以被看作是传递信息的桶。 它通常用于传递不一定直接绑定到方法调用的东西,但仍然可以是相关的。
例如,你正在编写C#程序,那么可以将多个mysql的链接信息定义为上下文,这样,当代码里需要更新数据时,就可以通过调用上下文来链接到指定的数据库了。

k8s上下文

在k8s中,上下文(context)是集群信息、用户和命名空间的组合这样一个概念。
我们知道,在k8s集群中,可以有多个用户,而我们最熟悉的就是使用kubectl命令行工具去连接k8s集群,但是kubectl是怎么去连接k8s集群的呢?
答案就是,kubectl命令行工具通过kubeconfig配置文件去连接k8s集群,而这个kubeconfig配置文件(默认是~/.kube/config)其实就是上下文(context),这个kubeconfig配置文件内容其实就包含了集群信息、用户和命名空间等信息,当我们要使用不同的用户链接k8s集群时,我们只需要创建各个用户对应的kubeconfig配置文件,然后通过kubectl命令切换上下文即可,如kubectl config use-context default。这样,不同的用户就通过自己的kubeconfig配置文件连接上了k8s集群。
我们在使用kubectl命令行时,如kubectl get pod 不写命名空间,默认就是default命名空间,那么,这种默认是如何定义的呢?答案就是kubeconfig文件里面的上下文配置。

kubectl命令行工具是如何连接k8s集群的?

kubectl命令行工具是通过kubeconfig配置文件来连接k8s集群的,这个kubeconfig配置文件默认就是/root/.kube/config,在我们使用kubeadm安装k8s集群时,默认自动生成了这个文件。

#查看集群kubeconfig配置文件
[root@matser ~]# ll /root/.kube/config
-rw-------. 1 root root 6293 111 2022 /root/.kube/config
[root@matser ~]# cat  /root/.kube/config
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tL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hLNXlPSEE0a2NiZ21JRnNOWU53b1BzCkI2bEMwOEJ6RVFJREFRQUJvMll3WkRBT0JnTlZIUThCQWY4RUJBTUNBUVl3RWdZRFZSMFRBUUgvQkFnd0JnRUIKL3dJQkFqQWRCZ05WSFE0RUZnUVVsTlNQVTFQL1plQnpnNkN4QS9KcFRFQkpJNDh3SHdZRFZSMGpCQmd3Rm9BVQpsTlNQVTFQL1plQnpnNkN4QS9KcFRFQkpJNDh3RFFZSktvWklodmNOQVFFTEJRQURnZ0VCQUczcndDa2UyNzhxCkJNeVpoKzF0QjJncmU3d1BTTFJGRElnNHR0MHlMcnZGMnMrWVZtazk2SFZ4K3NOM2RpWm1jbXZSdnVpaEFFSU4KSjlwOHVwQ2U1cE15aXhzb0V5MDgrYUxiVjc4V3pwMHZGRVozOVpYbWt1T3haREpRTTk5QU8rYmdOQ0NJdFRPeApzVmJrNCt3SnF2RlpmWlFlTkFXODUzVEFjdjYvbHo3MG5ldDVZN2RjMG00ZGJxUU1uWkVrZzJvd1l1dys2MVBUCmdTd0NIanpMNDdLNytqeFVmZ3UvQTVhVzRqY0ZzUFphZTJrY2xURUc0RSt6QkdXb2l6aS96aFlVODdsdC9ESWwKZGZDQXJTVTg2dk5WSHRSMVl0SGhneHk2TGszWGFhdTZyUXBRQno4Wmt1a0lBWitMdzVSQkV6UVVocmVFeFRTbQpHNmdiaEVHVit1ST0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: https://192.168.118.140:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: cluster-admin
  name: default
current-context: default
kind: Config
preferences: {}
users:
- name: cluster-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQ0akNDQXNxZ0F3SUJBZ0lVQ3pzbmY3d2EzYzMvdU5sS21LaEVaeThWV0I0d0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FERUxNQWtHQTFVRUJoTUNRMDR4RWpBUUJnT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2FvOTFKbkk3UTRSd1JhRGhmZUdjOTlLM3liSFRjRlBIVUNnWUVBcFlPR21weHBpOHZ1bFZXY1VucTIKeDVTRUx0TGsyQk9KcFU5cHo1VEkzTVZ5ZEN4RkVLUmE3cy93cnYzYVF4TmtYbzMrQnk1bmIvSDIrbWxFL242WApNNWxTRE1Fcis1WEZtOGVNQWxQRXVuc05mcWd4YnR2b2VGTGNhamdQZ1hrdUdkbUNtTmd6R08wcm9CUXRRck9wClM0M1o5bncrQVowSkZyNEh3M1FVcnRVQ2dZQU9tS3h2dVFSR3BPM3ZxMVlucVJaV3BxOENBTEE4Q2JmSFp3cDMKV05TaUJZRSttb3dFS21FZnF0VVMrbWxyRXFJNjZSQ3liUUI0OFpveDlIRTBJK252SGFOL1drWjJiQkhlYUJyNApsNzVVcWRPNjVMV2VFZjFReDZkOFBhSnlBRDl1TXk2UkZHUmFWWmc0T0Nhb0NTRVJkSHVPT1hWTWpDUjFSeXQxCldHZmlPUUtCZ0d4S1M0WGM5ZHdRa05obERtejNRTEt6M1JqZCtNNU44UGRWTnJGVHU0VlJhUUlEbzdFY0Fra0gKcjR5R1F6YWREU2xSektJWm1mR2FSam9Rd3ZFTFYxVy90NXBkSnltQXkrT2JhVVhOcDZlb1A2TlJQRlFsOE1qMAo3cC9DQVRUZmMxWWgxSE9CVkd5UHU4YUlHNFNkS1poajJxMzdsTW1KN0lZM01sSDBqNTNiCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
[root@matser ~]#

图解kubectlconfig配置文件

下面借用一张其他楼主分享的截图:
什么是k8s上下文?kubeconfig配置文件讲解_第1张图片
从上面kubectlconfig文件的格式我们可以看得出其格式是数组格式,一个kubectlconfig文件可以配置多个cluster、多个contexts、多个user。
所以也就是说,只要一个kubeconfig文件,kubectl命令行工具就可以管理多个k8s集群。

使用kubectl config命令生成kubeconfig文件

#这里先创建两个命名空间,模拟一个给dev线上环境使用,一个test给测试环境使用
[root@matser ~]# kubectl  create namespace dev
namespace/dev created
[root@matser ~]# kubectl  create namespace test
namespace/test created
[root@matser ~]# kubectl  get ns
NAME              STATUS   AGE
default           Active   233d
dev               Active   6s
test              Active   3s
kube-public       Active   233d
kube-system       Active   233d
[root@matser ~]#

#kubectl  config命令帮助
[root@matser ~]# kubectl  config --help
Modify kubeconfig files using subcommands like "kubectl config set current-context my-context"

 The loading order follows these rules:

  1.  使用--kubeconfig参数指定kubeconfig文件,该参数只能设置一次,并且不会发生合并。
  2.  可以设置$KUBECONFIG环境变量,其值就是绝对路径的kubeconfig文件,如: export KUBECONFIG=/root/.kube/config
  3.  否则,kubectl默认就会读取 ${HOME}/.kube/config文件。
(上面这3点说明,在使用kubectl工具时,可以使用--kubeconfig参数指定kubeconfig文件或者使用环境变量KUBECONFIG,如果这2个都没有指定,
则kubectl默认使用${HOME}/.kube/config文件,所以,我们在kubectl get pod时默认使用的就是${HOME}/.kube/config文件。当然,当年开发
人员要链接k8s集群时,可以先给开发人员创建对应的kubeconfig文件,然后在开发人员的linux账号下的~/.bash_profile配置环境变量KUBECONFIG
即可,这样开发人员默认就是使用他们自己的kubeconfig文件)。

Available Commands:
  current-context 显示当前上下文
  delete-cluster  删除kubeconfig文件中指定的集群
  delete-context  删除kubeconfig文件中指定的context
  delete-user     删除kubeconfig文件中指定的user
  get-clusters    显示kubeconfig文件中定义的集群
  get-contexts    显示一个或多个contexts
  get-users       显示kubeconfig文件中定义的users
  rename-context  重命名kubeconfig文件里的context
  set             在kubecconfig文件中设置一个单独的值
  set-cluster     在kubecconfig中设置集群条目
  set-context     在kubecconfig中设置上下文条目
  set-credentials 在kubecconfig中设置一个用户条目
  unset           取消kubecconfig文件中单个值的设置
  use-context     在kubecconfig文件中设置当前上下文(kubecconfig文件可以有多个上下文)
  view            显示合并的 kubeconfig 配置或一个指定的 kubeconfig 文件

Usage:
  kubectl config SUBCOMMAND [options]

Use "kubectl  --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
[root@matser ~]#
演示示例:
1、查看当前上下文

2、kubectl config set-cluster创建一个集群条目
[root@matser k8s]# kubectl config set-cluster dev-k8s --certificate-authority=/opt/kubernetes/ssl/ca.pem \
--embed-certs=true --server=https://192.168.118.140:6443 --kubeconfig=/root/.kube/config1
Cluster "dev-k8s" set.
[root@matser k8s]# cat /root/.kube/config1		#查看kubeconfig文件,cluster这一项有内容了
apiVersion: v1
clusters:										#cluster这一项有内容了
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR4RENDQXF5Z0F3SUJBZ0lVTVJKL0pIeFlMWjF0dVIwczZlekc4TVBLd2Nnd0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FERUxNQWtHQTFVRUJoTUNRMDR4RWpBUUJnTlZCQWdUQ1NCVGFHVnVlbWhsYmpFUk1BOEdBMVVFQnhNSQpVMmhsYm5wb1pXNHhEREFLQmdOVkJBb1RBMnM0Y3pFUE1BMEdBMVVFQ3hNR1UzbHpkR1Z0TVJNd0VRWURWUVFECkV3cHJkV0psY201bGRHVnpNQjRYRFRJeU1URXdNVEE0TXprd01Gb1hEVEkzTVRBek1UQTRNemt3TUZvd2FERUwKTUFrR0ExVUVCaE1DUTA0eEVqQVFCZ05WQkFnVENTQlRhR1Z1ZW1obGJqRVJNQThHQTFVRUJ4TUlVMmhsYm5wbwpaVzR4RERBS0JnTlZCQW9UQTJzNGN6RVBNQTBHQTFVRUN4TUdVM2x6ZEdWdE1STXdFUVlEVlFRREV3cHJkV0psCmNtNWxkR1Z6TUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF2dE40ZUhTZmlpK3EKbUFKSkg5QmpOeWVBS2tzNk1Mb2o2U0g1TDVTbmZnOWtMWngrYU9xaHdzWXRWWW5IK0h6clRGR25JVUorZTJXbwo0djQ5QmFFblZwMmRhZzRQSkZqSU9VVkMzNEF1YjFhK2MxV3dJWkJrMDlvWUNFWEEwOWVCa1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null					#上下文这一项没有内容
current-context: ""				#当前上下文这一项没有内容
kind: Config
preferences: {}
users: null						#用户信息这一项没有内容

[root@matser k8s]#
2、创建一个用户条目
[root@matser k8s]# kubectl config set-credentials cluster-admin \
  --client-certificate=/opt/TLS/k8s/admin.pem \
  --client-key=/opt/TLS/k8s/admin-key.pem \
  --embed-certs=true \
  --kubeconfig=/root/.kube/config1
User "cluster-admin" set.
[root@matser k8s]# cat /root/.kube/config1
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR4RENDQXF5Z0F3SUJBZ0lVTVJKL0pIeFlMWjF0dVIwczZlekc4TVBLd2Nnd0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FERUxNQWtHQTFVRUJoTUNRMDR4RWpBUUJnTlZCQWdUQ1NCVGFHVnVlbWhsYmpFUk1BOEdBMVVFQnhNSQpVMmhsYm5wb1pXNHhEREFLQmdOVkJBb1RBMnM0Y3pFUE1BMEdBMVVFQ3hNR1UzbHpkR1Z0TVJNd0VRWURWUVFECkV3cHJkV0psY201bGRHVnpNQjRYRFRJeU1URXdNVEE0TXprd01Gb1hEVEkzTVRBek1UQTRNemt3TUZvd2FERUwKTUFrR0ExVUVCaE1DUTA0eEVqQVFCZ05WQkFnVENTQlRhR1Z1ZW1obGJqRVJNQThHQTFVRUJ4TUlVMmhsYm5wbwpaVzR4RERBS0JnTlZCQW9UQTJzNGN6RVBNQTBHQTFVRUN4TUdVM2x6ZEdWdE1STXdFUVlEVlFRREV3cHJkV0psCmNtNWxkR1Z6TUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF2dE40ZUhTZmlpK3EKbUFKSkg5QmpOeWVBS2tzNk1Mb2o2U0g1TDVTbmZnOWtMWngrYU9xaHdzWXRWWW5IK0h6clRGR25JVUorZTJXbwo0djQ5QmFFblZwMmRhZzRQSkZqSU9VVkMzNEF1YjFhK2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: https://192.168.118.140:6443
  name: dev-k8s
contexts: null
current-context: ""
kind: Config
preferences: {}
users:							#现在看到用户信息这一项有内容了
- name: cluster-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQ0akNDQXNxZ0F3SUJBZ0lVQ3pzbmY3d2EzYzMvdU5sS21LaEVaeThWV0I0d0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FERUxNQWtHQTFVRUJoTUNRMDR4RWpBUUJnTlZCQWdUQ1NCVGFHVnVlbWhsYmpFUk1BOEdBMVVFQnhNSQpVMmhsYm5wb1pXNHhEREFLQmdOVkJBb1RBMnM0Y3pFUE1BMEdBMVVFQ3hNR1UzbHpkR1Z0TVJNd0VRWURWUVFECkV3cHJkV0psY201bGRHVnpNQjRYRFRJeU1URXdNVEV3TkRVd01Gb1hEVE15TVRBeU9URXdORFV3TUZvd2JURUwKTUFrR0ExVUVCaE1DUTA0eEVUQVBCZ05WQkFnVENGTm9aVzU2YUdWdU1SRXdEd1lEVlFRSEV3aFRhR1Z1ZW1obApiakVYTUJVR0ExVUVDaE1PYzNsemRHVnRPbTFoYzNSbGNuTXhEekFOQmdOVkJBc1RCbE41YzNSbGJURU9NQXdHCkExVUVBeE1GWVdSdGFXNHdnZ0VpTUEwR0NTcUdTSWIzRFFFQkFRVUFBNElCRHdBd2dnRUtBb0lCQVFEVEc1ODcKemNIa0IvYVh6Zit5MUVCd0QyREVVV1N0aUJKcm0ySTB4cjg5c0l2RjFkbmVyeWJkR1I0OER3N0FFWWhpVUNuTQpXSE9ZYlNkZnlxZFNTa3pmS0xQbytYd0Y2enpTZ2JJTldpRWRWYjVkWlhPenp3ck1BbHZqN0ZrZmQ5Vlh2NzdxCmhWSkkzR0h2SG5pZTdia1N0STY4aG85VkF3RmlzeUNBbllhQUlJS3kvY1k4V3p6Mk0xbmJDRU9nd0x2ZHBQQWoKT1VhZE1jYzFQR1NDTm9GZE1KZ0d1U3J4QXRVOHVqMnpp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g==
[root@matser k8s]#

3、设置上下文
#查看kubectl config set-context命令帮助
Usage:
  kubectl config set-context [NAME | --current] [--cluster=cluster_nickname] [--user=user_nickname]
[--namespace=namespace] [options]
#设置上下文
kubectl config set-context dev \
  --cluster=dev-k8s \
  --user=cluster-admin \
  --kubeconfig=/root/.kube/config1 \
  --namespace=dev					 #这里设置命名空间是dev
Context "dev" created.
[root@matser k8s]# cat /root/.kube/config1
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR4RENDQXF5Z0F3SUJBZ0lVTVJKL0pIeFlMWjF0dVIwczZlekc4TVBLd2Nnd0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FERUxNQWtHQTFVRUJoTUNRMDR4RWpBUUJnTlZCQWdUQ1NCVGFHVnVlbWhsYmpFUk1BOEdBMVVFQnhNSQpVMmhsYm5wb1pXNHhEREFLQmdOVkJBb1RBMnM0Y3pFUE1BMEdBMVVFQ3hNR1UzbHpkR1Z0TVJNd0VRWURWUVFECkV3cHJkV0psY201bGRHVnpNQjRYRFRJeU1URXdNVEE0TXprd01Gb1hEVEkzTVRBek1UQTRNemt3TUZvd2FERUwKTUFrR0ExVUVCaE1DUTA0eEVqQVFCZ05WQkFnVENTQlRhR1Z1ZW1obGJqRVJNQThHQTFVRUJ4TUlVMmhsYm5wbwpaVzR4RERBS0JnTlZCQW9UQTJzNGN6RVBNQTBHQTFVRUN4TUdVM2x6ZEdWdE1STXdFUVlEVlFRREV3cHJkV0psCmNtNWxkR1Z6TUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF2dE40ZUhTZmlpK3EKbUFKSkg5QmpOeWVBS2tzNk1Mb2o2U0g1TDVTbmZnOWtMWngrYU9xaHdzWXRWWW5IK0h6clRGR25JVUorZTJXbwo0djQ5QmFFblZwMmRhZzRQSkZqSU9VVkMzNEF1YjFhK2MxV3dJWkJrMDl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
  name: dev-k8s
contexts:
- context:						#上下文已经有了
    cluster: dev-k8s
    namespace: dev
    user: cluster-admin
  name: dev
current-context: ""				#当前上下文是空的,
kind: Config
preferences: {}
users:
- name: cluster-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQ0akNDQXNxZ0F3SUJBZ0lVQ3pzbmY3d2EzYzMvdU5sS21LaEVaeThWV0I0d0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FERUxNQWtHQTFVRUJoTUNRMDR4RWpBUUJnTlZCQWdUQ1NCVGFHVnVlbWhsYmpFUk1BOEdBMVVFQnhNSQpVMmhsYm5wb1pXNHhEREFLQmdOVkJBb1RBMnM0Y3pFUE1BMEdBMVVFQ3hNR1UzbHpkR1Z0TVJNd0VRWURWUVFECkV3cHJkV0psY201bGRHVnpNQjRYRFRJeU1URXdNVEV3TkRVd01Gb1hEVE15TVRBeU9URXdORFV3TUZvd2JURUwKTUFrR0ExVUVCaE1DUTA0eEVUQVBCZ05WQkFnVENGTm9aVzU2YUdWdU1SRXdEd1lEVlFRSEV3aFRhR1Z1ZW1obApiakVYTUJVR0ExVUVDaE1PYzNsemRHVnRPbTFoYzNSbGNuTXhEekFOQmdOVkJBc1RCbE41YzNSbGJURU9NQXdHCkExVUVBeE1GWVdSdGFXNHdnZ0VpTUEwR0NTcUdTSWIzRFFFQkFRVUFBNElCRHdBd2dnRUtBb0lCQVFEVEc1ODcKemNIa0IvYVh6Zit5MUVCd0QyREVVV1N0aUJKcm0ySTB4cjg5c0l2RjFkbmVyeWJkR1I0OER3N0FFWWhpVUNuTQpXSE9ZYlNkZnlxZFNTa3pmS0xQbytYd0Y2enpTZ2JJTldpRWRWYjVkWlhPenp3ck1BbHZqN0ZrZmQ5Vlh2NzdxCmhWSkkzR0h2SG5pZTdia1N0STY4aG85VkF3RmlzeUNBbllhQUlJS3kvY1k4V3p6Mk0xbmJDRU9nd0x2ZHBQQWoKT1VhZE1jYzFQR1N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EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
[root@matser k8s]#
4、再配置一个上下文,名称叫做test
kubectl config set-context test \
  --cluster=dev-k8s \
  --user=cluster-admin \
  --kubeconfig=/root/.kube/config1 \
  --namespace=test					 #这里设置命名空间是test
 
Context "test" created.
[root@matser k8s]#
[root@matser k8s]#
[root@matser k8s]#
[root@matser k8s]#
[root@matser k8s]#
[root@matser k8s]#
[root@matser k8s]#
[root@matser k8s]# cat /root/.kube/config1
[root@matser k8s]# cat /root/.kube/config1
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR4RENDQXF5Z0F3SUJBZ0lVTVJKL0pIeFlMWjF0dVIwczZlekc4TVBLd2Nnd0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FERUxNQWtHQTFVRUJoTUNRMDR4RWpBUUJnTlZCQWdUQ1NCVGFHVnVlbWhsYmpFUk1BOEdBMVVFQnhNSQpVMmhsYm5wb1pXNHhEREFLQmdOVkJBb1RBMnM0Y3pFUE1BMEdBMVVFQ3hNR1UzbHpkR1Z0TVJNd0VRWURWUVFECkV3cHJkV0psY201bGRHVnpNQjRYRFRJeU1URXdNVEE0TXprd01Gb1hEVEkzTVRBek1UQTRNemt3TUZvd2FERUwKTUFrR0ExVUVCaE1DUTA0eEVqQVFCZ05WQkFnVENTQlRhR1Z1ZW1obGJqRVJNQThHQTFVRUJ4TUlVMmhsYm5wbwpaVzR4RERBS0JnTlZCQW9UQTJzNGN6RVBNQTBHQTFVRUN4TUdVM2x6ZEdWdE1STXdFUVlEVlFRREV3cHJkV0psCmNtNWxkR1Z6TUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF2dE40ZUhTZmlpK3EKbUFKSkg5QmpOeWVBS2tzNk1Mb2o2U0g1TDVTbmZnOWtMWngrYU9xaHdzWXRWWW5IK0h6clRGR25JVUorZTJXbwo0djQ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S0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: https://192.168.118.140:6443
  name: dev-k8s
contexts:				#现在有了2个上下文,一个是dev,一个是test,这两个上下文都是属于dev-k8s集群的
- context:
    cluster: dev-k8s
    namespace: dev
    user: cluster-admin
  name: dev
- context:
    cluster: dev-k8s
    namespace: test
    user: cluster-admin
  name: test
current-context: ""			#当前上下文是空的
kind: Config
preferences: {}
users:
- name: cluster-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQ0akNDQXNxZ0F3SUJBZ0lVQ3pzbmY3d2EzYzMvdU5sS21LaEVaeThWV0I0d0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FERUxNQWtHQTFVRUJoTUNRMDR4RWpBUUJnTlZCQWdUQ1NCVGFHVnVlbWhsYmpFUk1BOEdBMVVFQnhNSQpVMmhsYm5wb1pXNHhEREFLQmdOVkJBb1RBMnM0Y3pFUE1BMEdBMVVFQ3hNR1UzbHpkR1Z0TVJNd0VRWURWUVFECkV3cHJkV0psY201bGRHVnpNQjRYRFRJeU1URXdNVEV3TkRVd01Gb1hEVE15TVRBeU9URXdORFV3TUZvd2JURUwKTUFrR0ExVUVCaE1DUTA0eEVUQVBCZ05WQkFnVENGTm9aVzU2YUdWdU1SRXdEd1lEVlFRSEV3aFRhR1Z1ZW1obApiakVYTUJVR0ExVUVDaE1PYzNsemRHVnRPbTFoYzNSbGNuTXhEekFOQmdOVkJBc1RCbE41YzNSbGJURU9NQXdHCkExVUVBeE1GWVdSdGFXNHdnZ0VpTUEwR0NTcUdTSWIzRFFFQkFRVUFBNElCRHdBd2dnRUtBb0lCQVFEVEc1ODcKemNIa0IvYVh6Zit5MUVCd0QyREVVV1N0aUJKcm0ySTB4cjg5c0l2RjFkbmVyeWJkR1I0OER3N0FFWWhpVUNuTQpXSE9ZYlNkZnlxZFNTa3pmS0xQbytYd0Y2enpTZ2JJTldpRWRWYjVkWlhPenp3ck1BbHZ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y90NXBkSnltQXkrT2JhVVhOcDZlb1A2TlJQRlFsOE1qMAo3cC9DQVRUZmMxWWgxSE9CVkd5UHU4YUlHNFNkS1poajJxMzdsTW1KN0lZM01sSDBqNTNiCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
[root@matser k8s]#

6、切换当前上下文
[root@matser k8s]# kubectl  config use-context dev --kubeconfig=/root/.kube/config1
Switched to context "dev".
[root@matser k8s]#
 [root@matser k8s]# kubectl  get pod --kubeconfig=/root/.kube/config1
No resources found in dev namespace.
[root@matser k8s]# kubectl  config get-contexts --kubeconfig=/root/.kube/config1	#可以看到有两个上下文,*号表示当前上下文
CURRENT   NAME   CLUSTER   AUTHINFO        NAMESPACE
*         dev    dev-k8s   cluster-admin   dev			
          test   dev-k8s   cluster-admin   test
[root@matser k8s]# cat /root/.kube/config1
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR4RENDQXF5Z0F3SUJBZ0lVTVJKL0pIeFlMWjF0dVIwczZlekc4TVBLd2Nnd0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FERUxNQWtHQTFVRUJoTUNRMDR4RWpBUUJnTlZCQWdUQ1NCVGFHVnVlbWhsYmpFUk1BOEdBMVVFQnhNSQpVMmhsYm5wb1pXNHhEREFLQmdOVkJBb1RBMnM0Y3pFUE1BMEdBMVVFQ3hNR1UzbHpkR1Z0TVJNd0VRWURWUVFECkV3cHJkV0psY201bGRHVnpNQjRYRFRJeU1URXdNVEE0TXprd01Gb1hEVEkzTVRBek1UQTRNemt3TUZvd2FERUwKTUFrR0ExVUVCaE1DUTA0eEVqQVFCZ05WQkFnVENTQlRhR1Z1ZW1obGJqRVJNQThHQTFVRUJ4TUlVMmhsYm5wbwpaVzR4RERBS0JnTlZCQW9UQTJzNGN6RVBNQTBHQTFVRUN4TUdVM2x6ZEdWdE1STXdFUVlEVlFRREV3cHJkV0psCmNtNWxk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LNytqeFVmZ3UvQTVhVzRqY0ZzUFphZTJrY2xURUc0RSt6QkdXb2l6aS96aFlVODdsdC9ESWwKZGZDQXJTVTg2dk5WSHRSMVl0SGhneHk2TGszWGFhdTZyUXBRQno4Wmt1a0lBWitMdzVSQkV6UVVocmVFeFRTbQpHNmdiaEVHVit1ST0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: https://192.168.118.140:6443
  name: dev-k8s
contexts:
- context:
    cluster: dev-k8s
    namespace: dev
    user: cluster-admin
  name: dev
- context:
    cluster: dev-k8s
    namespace: test
    user: cluster-admin
  name: test
current-context: dev		#可以看到当前上下文是dev
kind: Config
preferences: {}
users:
- name: cluster-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQ0akNDQXNxZ0F3SUJBZ0lVQ3pzbmY3d2EzYzMvdU5sS21LaEVaeThWV0I0d0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FERUxNQWtHQTFVRUJoTUNRMDR4RWpBUUJnTlZCQWdUQ1NCVGFHVnVlbWhsYmpFUk1BOEdBMVVFQnhNSQpVMmhsYm5wb1pXNHhEREFLQmdOVkJBb1RBMnM0Y3pFUE1BMEdBMVVFQ3hNR1UzbHpkR1Z0TVJNd0VRWURWUVFECkV3cHJkV0psY201bGRHVnpNQjRYRFRJeU1URXdNVEV3TkRVd01Gb1hEVE15TVRBeU9URXdORFV3TUZvd2JURUwKTUFrR0ExVUVCaE1DUTA0eEVUQVBCZ05WQkFnVENGTm9aVzU2YUdWdU1SRXdEd1lEVlFRSEV3aFRhR1Z1ZW1obApiakVYTUJVR0ExVUVDaE1PYzNsemRHVnRPbTFoYzNSbGNuTXhEekFOQmdOVkJBc1RCbE41YzNSbGJURU9NQXdHCkExVUVBeE1GWVdSdGFXNHdnZ0VpTUEwR0NTcUdTSWIzRFFFQkFRVUFBNElCRHdBd2dnRUtBb0lCQVFEVEc1ODcKemNIa0IvYVh6Zit5MUVC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DUjFSeXQxCldHZmlPUUtCZ0d4S1M0WGM5ZHdRa05obERtejNRTEt6M1JqZCtNNU44UGRWTnJGVHU0VlJhUUlEbzdFY0Fra0gKcjR5R1F6YWREU2xSektJWm1mR2FSam9Rd3ZFTFYxVy90NXBkSnltQXkrT2JhVVhOcDZlb1A2TlJQRlFsOE1qMAo3cC9DQVRUZmMxWWgxSE9CVkd5UHU4YUlHNFNkS1poajJxMzdsTW1KN0lZM01sSDBqNTNiCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
[root@matser k8s]#
现在,我们在集群创建资源对应默认就是在当前上下文dev中创建,而当前上下文就是dev命名空间,所以创建的资源都在dev命令空间:
[root@matser k8s]# kubectl  run nginx-dev --image=nginx --kubeconfig=/root/.kube/config1
pod/nginx-dev created
[root@matser k8s]# kubectl  get pod --kubeconfig=/root/.kube/config1
NAME        READY   STATUS              RESTARTS   AGE
nginx-dev   0/1     ContainerCreating   0          11s
[root@matser k8s]# kubectl  get all -n dev --kubeconfig=/root/.kube/config1
NAME            READY   STATUS    RESTARTS   AGE
pod/nginx-dev   1/1     Running   0          36s
[root@matser k8s]#

同理,使用kubectl  config use-context test --kubeconfig=/root/.kube/config1 切换上下文,那么默认就是在test命名空间创建资源对应了。

安装kubectl命令行工具

假设现在已经有2个k8s集群了,需要使用一台单独的服务器安装kubectl工具来管理多个k8s集群。

#下载kubectl命令行工具,下载最新版本
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
#下载指定版本
curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.22.17/bin/linux/amd64/kubectl"

#授权并移动到$PATH目录
chmod a+x kubectl && mv kubectl  /usr/bin/

#查看kubectl 版本
[root@easypack ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.17", GitCommit:"a7736eaf34d823d7652415337ac0ad06db9167fc", GitTreeState:"clean", BuildDate:"2022-12-08T11:47:36Z", GoVersion:"go1.16.15", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?
[root@easypack ~]# 

#查看kubectl目录帮助
[root@easypack ~]# kubectl  --help
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

Basic Commands (Beginner):
  create        Create a resource from a file or from stdin
  expose        Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
  run           Run a particular image on the cluster
  set           Set specific features on objects

Basic Commands (Intermediate):
  explain       Get documentation for a resource
  get           Display one or many resources
  edit          Edit a resource on the server
  delete        Delete resources by file names, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout       Manage the rollout of a resource
  scale         Set a new size for a deployment, replica set, or replication controller
  autoscale     Auto-scale a deployment, replica set, stateful set, or replication controller
.....................

我们知道,kubectl使用kubeconfig文件来链接k8s集群,kubeconfig文件保存了k8s集群的用户信息、集群信息以及上下文等信息。kubectl 通过环境变量$KUBECONFIG、或者使用参数 --kubeconfig=xxxx来指定kubeconfig文件路径,当以上均未设置时,kubectl默认会查找并使用${HOME}/.kube/config 文件。
所以,我们只需要将2个集群的kubeconfig文件合成一个,即可让kubectl管理多个k8s集群:

kubectl管理多个k8s集群

mkdir ${HOME}/.kube/ 
touch ${HOME}/.kube/config
#查看第1台服务器上的kubeconfig文件
cat ${HOME}/.kube/config
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUVHVENDQXdHZ0F3SUJBZ0lDQitNd0RRWUpLb1pJaHZjTkFRRUxCUUF3Z1p3eEN6QUpCZ05WQkFZVEFrTk8KTVJBd0RnWURWUVFJRXdkQ1JVbEtTVTVITVJBd0RnWURWUVFIRXdkQ1JVbEtTVTVITVNZd0pBWURWUVFKRXgweApNSFJvSUZOVVVrVkZWQ3dnVTBoQlRrZEVTU3dnU0VGSlJFbEJUakVQTUEwR0ExVUVFU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kNDc0dBUVVGQndNQk1BOEdBMVVkRXdFQi93UUZNQU1CQWY4d0hRWURWUjBPQkJZRUZMSnpUZFBOZmNjRGFERisKRGU1ckduYmZSdk9OTUEwR0NTcUdTSWIzRFFFQkN3VUFBNElCQVFBYzJ4UzNKWVRjcXRYTk9pTk9MNzdDZWlQVgpOV3pWZkRBTjRFL3c3Nm5xRkxVZHhycmt2N0hRek5KVW5pRDEvZlZHM2FiNTdCTE50b1RoMTh1bHBFVUdTcERhCnp1K1RTK1pMd0pPTytOTFZhTk8zUUZwZFplMndmbCtvRTZSY3RFaFdTREFrOFVjcUNTSy8yZmVoMjhVaWdiTXgKV0NBQ0VMUHpxTjhBZHdpSWJRQVlKb3d3WkVkNTluNWJyMXYyWjgvRWpGMXlzNGlSNG0xdDlpOFdYeldZd1JRcAovK1JiWDZ1clBnaXo2cEZwcFFTa0Iwc0Q4Y1FBU2dKcEl0SUZyaG9reDNxMXF5cHArVVlzWGNubXBxa3JnK1luClFrL0pqMUxSelJKcGNnQ3k1YlpCMGpsN3l4cmNkOVMwVUJ5dXh0ZEthYkNOOWFpZ2lCK0hQaUIyRHhIdwotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0t
    server: https://192.168.244.21:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: admin
  name: default
current-context: default
kind: Config
preferences: {}
users:
- name: admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUVHekNDQXdPZ0F3SUJBZ0lVVUsrNnBCUVgyb2pWL2lIUlZ4MmNhazF6Ry9nd0RRWUpLb1pJaHZjTkFRRUwKQlFBd2dad3hDekFKQmdOVkJBWVRBa05PTVJBd0RnWURWUVFJRXdkQ1JVbEtTVTVITVJBd0RnWURWUVFIRXdkQwpSVWxLU1U1SE1TWXdKQVlEVlFRSkV4MHhNSFJvSUZOVVVrVkZWQ3dnVTBoQlRrZEVTU3dnU0VGSlJFbEJUakVQCk1BMEdBMVVFRVJ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lLT3ZGSGFqdUdSbi80cXBnd0h3WURWUjBqQkJnd0ZvQVVzbk5OMDgxOXh3Tm8KTVg0Tjdtc2FkdDlHODQwd0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFIY0g5UDFFcHNMQXdSNW5DemM2d3dXbwp2STV1UUU4TXZkeXp5N2xJSTRIYko4LzRERlIxb0t0eXlpWm5HeDFOOGxrV0VSRUxGVkh3ZWdNOGJ2TTUvLzYzCnJNR3F1UElWUlo2WXlSQ05hVThFeVROMlBlb2pQVFdGN1lzM1FJb1hDSGFKU2hMVVZodC9DejNjelZKTUxHOFkKNHpQVGlick9DRXNqWEhzamJlZzhpWTc4dElEcW5TWENaTmkzN2JlOWhZV21CN2FLbWNpWktyMnJ2cW1ubjFYeAprNmZ4eGxwWisrQ0FreHZPY0xidDAwV24yUksxWm9QdFhoUk5TbTl0SnJmOEtaSk5tOHA1RnkwMWF5NStGVmQ1Cm9xSmZhVVZQNy9PZG1XVGxydDdITDc5aUdjZGd4YzZkSWlZVkpYMkpMSzVCcDdLbUx5NDNselF5TzduaVRXQT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=

#查看第2台服务器上的kubeconfig文件
[root@iot ~]# cat ${HOME}/.kube/config
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM2VENDQWRHZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQ0FYRFRJek1EWXlOakEwTURBeU1Gb1lEekl4TWpNd05qQXlNRFF3TURJd1dqQVZNUk13RVFZRApWUVFERXdwcmRXSmxjbTVsZEdWek1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBCjZFMmt5VndYdG5HaWJhbndLZ2w3cEhRWVlZMU52Y2hPS3VjVmRNMWU1aXhrOEgrdjcvcEQ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server: https://192.168.244.180:6443
  name: cluster.local
contexts:
- context:
    cluster: cluster.local
    user: kubernetes-admin
  name: [email protected]
current-context: [email protected]
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURGVENDQWYyZ0F3SUJBZ0lJZXBzenBlQkJDVUV3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWdGdzB5TXpBMk1qWXdOREF3TWpCYUdBOHlNVEl6TURZd01qQTBNREF5TVZvdwpOREVYTUJVR0ExVUVDaE1PYzNsemRHVnRPbTFoYzNSbGNuTXhHVEFYQmdOVkJBTVRFR3QxWW1WeWJtVjBaWE10CllXUnRhVzR3Z2dFaU1BMEdDU3FHU0liM0RRRUJBUVVBQTRJQkR3QXdnZ0VLQW9JQkFRQzhkUVczSU5mN1VPMjQKa2J6dlg3RzdGWWtndFA3VVNyN2Jzbnp1Si9lUSs4a3JCOEZ6a3lLNFFPcHlSVmVySVNHd1pkQWhIVHBpRURzcwpZeHZqQ2I3M2w2MTd4R3Byc2lXQklLakRwR1VKcCsxQnp5bUoxckZldVg2WnNoelhNdVFLKzMxYnpHejd2MXh0CmZoZ2svMkxFR0UycGZ5VnQ4U3ZVVGI1Z0pEMVowK2xVbi9LVGtrT0w5eUN4SUNKRU1lZi93Tk5kejNHNXZiZloKNjhUZlBuaHhRMzRwQWcyZy82RWRKLzlhSmhrUG1zUXFiUXhqR1ViMFFTMU5BTUZ0QlJoZ2pGNHFyeEZaY003YwpJQ0FTNWg0blJ2U1UxblJrT3EvTUtTT1pqTzFiNS82VXhyZ1BEMHlRTHFUSkQvZWRaeFVNRGpsZ3ZlNnNRRVhBClhrMGlPTUg3QWdNQkFBR2pTREJHTUE0R0ExVWREd0VCL3dRRUF3SUZvREFUQmdOVkhTVUVEREFLQmdnckJnRUYKQlFjREFqQWZCZ05WSFNNRUdEQVdn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ZTMxLytxYVJHVVVTMUZaMTVJK1hKazV4U3piVjFMVE5hSFRKdllHTktxdUxyQnVtc3l6bitiSFdkRGZzd2RQYgoxQWt2bEJ2OWxxTHJ2S1lGNVFxWGtPY0I0NmJFRCt4SUhVa3hJQU56NVV5TXgwdVNza3VlNGF4ZldPOC9PbFBrCml3bnU3RHpaUnBwcG92d2NGK3lEd1FLQmdHWFhjeTBUWjhERU1TNXRKdkFRajhPaXExV2pENzJMY0hQOXdyck8KMzAzQXJVK1pVTnJWOGJXb095enFETUp2SkxvMGVoeGRXWnZEVk5zbjdyQVV5ZnFlcWl1K1daM3J3ZUxoZ1FzOApTVzJON0c5SmFGOFY0cGNOVktqeFprbE1YLzRuY1FXWTB0NEdDaUxOK29aSUdNcFpzSVNjWXBQM2pKZGZ4R0J5CnByZFJBb0dBVk04ckRneE5Jc2hSN1ZHLzZSUFZWT05lSjdtdDBNR3dyNys1Z21CUnlER0wvb2FFUlZGVG54MXAKRHZMV0pyb0F1ZTJqaThGcHVERjEwRHhCTDlIUFRaVEtpRTBMVEc3bEdCN1UrWWVBa096UkViUWk2UDNhc1JTawo3ZVlMNDNqa3ZVOFpHbHI0MVU3YnJNcEdRR2dmYVdnNit5VmNqZWc4QkJSYmExUm5zeUE9Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==



#现在在新服务器上将2个kubeconfig文件整合到一个文件中
vim ${HOME}/.kube/config
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUVHVENDQXdHZ0F3SUJBZ0lDQitNd0RRWUpLb1pJaHZjTkFRRUxCUUF3Z1p3eEN6QUpCZ05WQkFZVEFrTk8KTVJBd0RnWURWUVFJRXdkQ1JVbEtTVTVITVJBd0RnWURWUVFIRXdkQ1JVbEtTVTVITVNZd0pBWURWUVFKRXgweApNSFJvSUZOVVVrVkZWQ3dnVTBoQlRrZEVTU3dnU0VGSlJFbEJUakV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https://192.168.244.180:6443
  name: test
contexts:
- context:
    cluster: dev
    namespace: textmind
    user: admin
  name: dev-context
- context: 
    cluster: test
    user: kubernetes-admin
  name: test-context
current-context: dev-context
kind: Config
preferences: {}
users:
- name: admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUVHekNDQXdPZ0F3SUJBZ0lVVUsrNnBCUVgyb2pWL2lIUlZ4MmNhazF6Ry9nd0RRWUpLb1pJaHZjTkFRRUwKQlFBd2dad3hDekFKQmdOVkJBWVRBa05PTVJBd0RnWURWUVFJRXdkQ1JVbEtTVTVITVJBd0RnWURWUVFIRXdkQwpSVWxLU1U1SE1TWXdKQVlEVlFRSkV4MHhNSFJvSUZOVVVrVkZWQ3dnVTBoQlRrZEVTU3dnU0VGSlJFbEJUakVQCk1BMEdBMVVFRVJNR01UQXdNRGswTVE0d0RBWURWUVFLRXdWQ1lXbGtkVEVMTUFrR0ExVUVDeE1DUTBFeEV6QVIKQmdOVkJBTVRDa3QxWW1WeWJtVjBaWE13SUJjTk1qTXdOakkxTURZeU1qQXdXaGdQTWpFeU16QTJNREV3TmpJeQpNREJhTUc4eEN6QUpCZ05WQkFZVEFsVlRNUTh3RFFZRFZRUUlFd1pQY21WbmIyNHhFVEFQQmdOVkJBY1RDRkJ2CmNuUnNZVzVrTVJjd0ZRWURWUVFLRXc1emVYTjBaVzA2YldGemRHVnljekVUTUJFR0ExVUVDeE1LUzNWaVpYSnUKWlhSbGN6RU9NQXdHQTFVRUF4TUZZV1J0YVc0d2dnRWlNQTBHQ1NxR1NJYjNEUUVCQVFVQUE0SUJEd0F3Z2dFSwpBb0lCQVFEaXpXaFY0L2JwN2pPTTM2S3Y4REVmcnl3dmtqaHhOMlkrekRLR3lCS0dTd1ZhZ0hGYVJNRVUzTFlFCnVOT2E3d3ltSHRmc0laL3NFalRkNmE0Vmg4eHBOQldwclo3NFNrb25GVU1OTS9reUtjZ3hPaVFlZjhvVU4zenQKVEhGT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s4QWNGN1hDS2tQSGdiRFRRWTBxa0NnWUJWeVN6Zmx5cmE3QnFBTzI3WHJXS3EKZTMxLytxYVJHVVVTMUZaMTVJK1hKazV4U3piVjFMVE5hSFRKdllHTktxdUxyQnVtc3l6bitiSFdkRGZzd2RQYgoxQWt2bEJ2OWxxTHJ2S1lGNVFxWGtPY0I0NmJFRCt4SUhVa3hJQU56NVV5TXgwdVNza3VlNGF4ZldPOC9PbFBrCml3bnU3RHpaUnBwcG92d2NGK3lEd1FLQmdHWFhjeTBUWjhERU1TNXRKdkFRajhPaXExV2pENzJMY0hQOXdyck8KMzAzQXJVK1pVTnJWOGJXb095enFETUp2SkxvMGVoeGRXWnZEVk5zbjdyQVV5ZnFlcWl1K1daM3J3ZUxoZ1FzOApTVzJON0c5SmFGOFY0cGNOVktqeFprbE1YLzRuY1FXWTB0NEdDaUxOK29aSUdNcFpzSVNjWXBQM2pKZGZ4R0J5CnByZFJBb0dBVk04ckRneE5Jc2hSN1ZHLzZSUFZWT05lSjdtdDBNR3dyNys1Z21CUnlER0wvb2FFUlZGVG54MXAKRHZMV0pyb0F1ZTJqaThGcHVERjEwRHhCTDlIUFRaVEtpRTBMVEc3bEdCN1UrWWVBa096UkViUWk2UDNhc1JTawo3ZVlMNDNqa3ZVOFpHbHI0MVU3YnJNcEdRR2dmYVdnNit5VmNqZWc4QkJSYmExUm5zeUE9Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==

#下面开始验证kubectl能不是实现管理多个k8s集群
[root@easypack ~]# kubectl  config get-clusters		#查看集群
NAME
test
dev
[root@easypack ~]# kubectl  config  get-contexts	#查看上下文
CURRENT   NAME           CLUSTER   AUTHINFO           NAMESPACE
*         dev-context    dev       admin              textmind
          test-context   test      kubernetes-admin   
[root@easypack ~]# kubectl  config  get-users		#查看用户
NAME
admin
kubernetes-admin
[root@easypack ~]# 
[root@easypack ~]# kubectl  config current-context	#查看当前上下文
dev-context
[root@easypack ~]# 
[root@easypack ~]# kubectl get pod 				#不写命名空间,默认就是textmind,因为我们上下文里面就是定义textmin命名空间
NAME                                   READY   STATUS      RESTARTS   AGE
textmind-init-wrn2d                    0/1     Completed   0          31h
xmind-gateway-76d64b4c4c-tw5xv         1/1     Running     0          13h
xmind-nginx-6dcd59ddf8-ndm84           1/1     Running     0          13h
[root@easypack ~]# kubectl  get pod -n textmind
NAME                                   READY   STATUS      RESTARTS   AGE
textmind-init-wrn2d                    0/1     Completed   0          31h
xmind-gateway-76d64b4c4c-tw5xv         1/1     Running     0          13h
xmind-nginx-6dcd59ddf8-ndm84           1/1     Running     0          13h
[root@easypack ~]# 

#切换上下文,即切换集群环境
[root@easypack ~]# kubectl  config use-context test-context		#切换上下文
Switched to context "test-context".
[root@easypack ~]# kubectl  config current-context				#查看当前上下文
test-context
[root@easypack ~]# kubectl  config get-contexts					#可以看到当前上下文前面有星号
CURRENT   NAME           CLUSTER   AUTHINFO           NAMESPACE
          dev-context    dev       admin              textmind
*         test-context   test      kubernetes-admin   
[root@easypack ~]# 
[root@easypack ~]# kubectl  get pod 			#因为我们上下文里面没指定命名空间,所以默认是default命名空间,
NAME                                                        READY   STATUS    RESTARTS   AGE
kube-prometheus-stack-grafana-74f6bbd969-p5lmr              4/4     Running   0          12h
kube-prometheus-stack-kube-state-metrics-56dbf9db46-bhvpj   1/1     Running   0          12h
kube-prometheus-stack-operator-6db5f56678-fcwnh             1/1     Running   0          12h
kube-prometheus-stack-prometheus-adapter-58cd6cd7f5-x5tgc   1/1     Running   0          12h
kube-prometheus-stack-prometheus-node-exporter-frjln        1/1     Running   0          12h
kube-prometheus-stack-prometheus-node-exporter-k8k9w        1/1     Running   0          12h
kubernetes-dashboard-5c8b6bc8bc-pm7j6                       1/1     Running   0          12h
[root@easypack ~]# kubectl  get pod -n default
NAME                                                        READY   STATUS    RESTARTS   AGE
kube-prometheus-stack-grafana-74f6bbd969-p5lmr              4/4     Running   0          12h
kube-prometheus-stack-kube-state-metrics-56dbf9db46-bhvpj   1/1     Running   0          12h
kube-prometheus-stack-operator-6db5f56678-fcwnh             1/1     Running   0          12h
kube-prometheus-stack-prometheus-adapter-58cd6cd7f5-x5tgc   1/1     Running   0          12h
kube-prometheus-stack-prometheus-node-exporter-frjln        1/1     Running   0          12h
kube-prometheus-stack-prometheus-node-exporter-k8k9w        1/1     Running   0          12h
kubernetes-dashboard-5c8b6bc8bc-pm7j6                       1/1     Running   0          12h
[root@easypack ~]# 

以上就通过kubectl管理多个k8集群。这里,我们使用的k8s集群的admin管理员账号,后面将讲如何使用自己创建的k8s用户账号。
从上面的上下文中我们可以指定,上下文其实关联了用户、集群、空间等信息,所以通过切换上下文就可以切换不同的k8s集群,当然,如果你创建了2个上下文,上下文配置的是同一个k8s集群,只是用户或命名空间不同。那么其实就是通过不同的用户账号登录同一个k8s集群而已。

你可能感兴趣的:(kubernetes,kubernetes,容器,云原生)