多K8s集群切换中Kubectl客户端如何配置

博主:xiaoweixiaowei 2023-01-08 条评论

这篇文章主要介绍了多K8s集群切换中Kubectl客户端如何配置,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

kubectl的配置文件位置

kubectl配置文件的主要位置是$ HOME / .kube,默认情况下,我们有一个名为config的配置文件:

[node1 ~]$ cd $HOME/.kube [node1 .kube]$ ls -lah total 4.0K drwxr-xr-x 1 root root 37 Mar 12 20:48 . dr-xr-x— 1 root root 19 Nov 29 11:46 .. drwxr-x— 3 root root 23 Mar 12 20:48 cache lrwxrwxrwx 1 root root 26 Nov 29 11:46 config -> /etc/kubernetes/admin.conf drwxr-x— 3 root root 4.0K Mar 12 20:48 http-cache

多个配置文件和KUBECONFIG变量

通过设置适当的KUBECONFIG shell变量,我们可以拥有多个配置文件。

例如:

export KUBECONFIG=$KUBECONFIG:/root/.kube/additional_config

从kubectl检查配置

我们可以检查当前的配置(由于我的Additional_config文件中没有任何内容,因此将为空-我们稍后将添加一些内容-这只是我的KUBECONFIG变量中的文件):

[node1 ~]$ kubectl config view apiVersion: v1 clusters: [] contexts: [] current-context: "" kind: Config preferences: {} users: []

或来自特定文件-让我们看看默认文件:

[node1 ~]$ kubectl config –kubeconfig=.kube/config view apiVersion: v1 clusters:

  • cluster: certificate-authority-data: DATA+OMITTED server: https://192.168.0.38: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: REDACTED client-key-data: REDACTED

如果您未设置KUBECONFIG变量,则所有配置都将从$ HOME / .kube / config中获取

kubectl配置中的对象

kubectl在其配置文件中具有以下对象类型

集群-有关K8s集群的信息-包含群集名称和连接参数

users-有关您要连接到K8s集群的用户的信息

上下文-集群/用户/命名空间的三倍

从kubectl修改配置

添加集群

多K8s集群切换中Kubectl客户端如何配置

[node1 ~]$ kubectl config –kubeconfig=.kube/additional_config set-cluster dev –server=https://192.168.1.100 –certificate-authority=fake-ca Cluster "dev" set. [node1 ~]$ kubectl config –kubeconfig=.kube/additional_config set-cluster prod –server=https://10.1.1.100 –certificate-authority=fake-ca Cluster "prod" set.

新增使用者

[node1 ~]$ kubectl config –kubeconfig=.kube/additional_config set-credentials developer –client-certificate=fake-cert-file –client-key=fake-key-seefile User "developer" set. [node1 ~]$ kubectl config –kubeconfig=.kube/additional_config set-credentials prod_admin –client-certificate=fake-cert-file –client-key=fake-key-seefile User "prod_admin" set.

添加上下文

[node1 ~]$ kubectl config –kubeconfig=.kube/additional_config set-context simple_app_development –cluster=dev –namespace=simple_app_dev –user=developer Context "simple_app_development" created. [node1 ~]$ kubectl config –kubeconfig=.kube/additional_config set-context simple_app_production –cluster=prod –namespace=simple_app_prod –user=prod_admin Context "simple_app_production" created.

从配置中删除集群/用户/上下文

kubectl –kubeconfig=.kube/additional_config config unset users.<name> kubectl –kubeconfig=.kube/additional_config config unset clusters.<name> kubectl –kubeconfig=.kube/additional_config config unset contexts.<name>

样本配置

[node1 ~]$ kubectl config –kubeconfig=.kube/additional_config view apiVersion: v1 clusters:

  • cluster: certificate-authority: /root/fake-ca server: https://192.168.1.100 name: dev

  • cluster: certificate-authority: /root/fake-ca server: https://10.1.1.100 name: prod contexts:

  • context: cluster: dev namespace: simple_app_dev user: developer name: simple_app_development

  • context: cluster: prod namespace: simple_app_prod user: prod_admin name: simple_app_production current-context: "" kind: Config preferences: {} users:

  • name: developer user: client-certificate: /root/fake-cert-file client-key: /root/fake-key-seefile

  • name: prod_admin user: client-certificate: /root/fake-cert-file client-key: /root/fake-key-seefile

改变环境

要获取上下文列表(不需要–kubeconfig,因为我们已将extra_config添加到KUBECONFIG变量中):

[node1 ~]$ kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE simple_app_development dev developer simple_app_dev simple_app_production prod prod_admin simple_app_prod

设置上下文:

[node1 ~]$ kubectl config use-context simple_app_development Switched to context "simple_app_development".

获取当前上下文:

[node1 ~]$ kubectl config current-context simple_app_development                               

感谢你能够认真阅读完这篇文章,希望小编分享的“多K8s集群切换中Kubectl客户端如何配置”这篇文章对大家有帮助,同时也希望大家多多支持云,关注云行业资讯频道,更多相关知识等着你来学习!

The End

发布于:2023-01-08,除非注明,否则均为 主机评测原创文章,转载请注明出处。