当前位置:网站首页>Configuring multi cluster management using kubectl

Configuring multi cluster management using kubectl

2022-06-23 03:18:00 genesis

Everybody knows kubectl Mainly used for management kubernetes colony , But when there are multiple sets in our environment kubernetes When the cluster , How to manage , The following is a list of the configuration kubeconfig To achieve kubectl Methods of managing multiple clusters .

One 、 Use kubeclt Merge multiple config

It is assumed that the current client has configured the access credentials of a cluster , namely ~/.kube/config It already contains a set of cluster access credentials , Then we can upload the access credentials of another cluster to /tmp, And named it test-config, Then execute the following command to merge the... Of multiple clusters config.

#KUBECONFIG=~/.kube/config:/tmp/test-config kubectl config view --merge --flatten >> ~/.kube/config
#export KUBECONFIG=~/.kube/config

have access to kubectl config view View merged kubeconfig Configuration situation , Under normal circumstances , Two cluster credential information can be displayed .

explain : There is a small problem in the test , The above command does not add >> ~/.kube/config when , You can output the access credentials of two clusters , Under normal circumstances, you should use

> ~/.kube/config Overwrite the output results with config in , After the actual execution is completed ,config Just keep test-config Access credentials for , So instead of using >> ~/.kube/config, Append the access credentials of the two clusters to config In file , But at the same time, the previous access credentials still exist .

Two 、 Will be multiple config Configure to environment variables

We upload the access credentials of another cluster to ~/.kube, And named it test-config, Then execute the following command :

#echo KUBECONFIG=/root/.kube/config:/root/.kube/test-config ~/.bash_profile
#source ~/.bash_profile

perform kubectl config view You can also see kubeconfig Contains access credentials for both clusters .

3、 ... and 、 Manual configuration

The manual method is cumbersome , Just list them , I want to observe config Detailed format of , It's good for you kubeconfig Have a clearer understanding , The following for config Detailed format of .

# When an access credential #
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: ***
    server: https://192.168.*.*:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: [email protected]
current-context: [email protected]
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: ***
    client-key-data: ***


# Two access credentials #
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: ***
    server: https://42.194.*.*:443/
  name: cls-ec6ymsmo
- cluster:
    certificate-authority-data: ***
    server: https://192.168.*.*:6443
  name: kubernetes
contexts:
- context:
    cluster: cls-ec6ymsmo
    user: "10000******"
  name: cls-ec6ymsmo-10000******
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: [email protected]
current-context: [email protected]
kind: Config
preferences: {}
users:
- name: "100006621061"
  user:
    client-certificate-data: ***
    client-key-data: ***
- name: kubernetes-admin
  user:
    client-certificate-data: ***
    client-key-data: ***

You can clearly see , We can also manually cluster another set of cluster、context、user Paste it into... According to the format requirements config that will do .

Four 、 summary

I prefer the second of the three configuration methods , When you need to manage multiple clusters, the configuration is also relatively simple , It is worth noting that the first configuration method is used > ~/.kube/config The previous access credentials will be overwritten , In this case, the cluster's /etc/kubernetes/admin.conf Make a new copy of the document .

After configuring the multi cluster access credentials , You can use the following commands to view and switch multi cluster information .

kubectl config get-contexts     # View cluster information ,* Indicates the current working environment .
kubectl config use-context [email protected]   # Switch working environment to [email protected]
kubectl config --help   # See more config Use command 
原网站

版权声明
本文为[genesis]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201211557470775.html