Background:
We have k8s through multiple clusters, such as clusters (cn-k8s) and clusters (jp-k8s), that you need to have a server cluster can simultaneously access two way: config information will be stored into two clusters a file by using context_name to access the cluster. In short, by setting the context to make different kubectl access k8s cluster.kubectl config use-context
Specific steps are as follows:
If you are ready to two cluster configuration file, respectively $HOME/.kube/config1 和
$HOME/.kube/config2
config1 information is as follows
[root@node-01 .kube]# cat $HOME/.kube/config1 apiVersion: v1 kind: Config clusters: - cluster: api-version: v1 certificate-authority-data: xxxxxxx server: "https://172.20.8.113:6443" name: "cn-k8s" contexts: - context: cluster: "cn-k8s" user: "kube-admin-local" name: "cn-k8s" current-context: "cn-k8s" users: - name: "kube-admin-local" user: client-certificate-data: xxxxxx client-key-data: xxxxxx
config2 information is as follows
[root@node-01 .kube]# cat $HOME/.kube/config2 apiVersion: v1 kind: Config clusters: - cluster: api-version: v1 certificate-authority-data: xxxxxx server: "https://172.19.8.113:6443" name: "jp-k8s" contexts: - context: cluster: "jp-k8s" user: "kube-admin-local" name: "jp-k8s" current-context: "jp-k8s" users: - name: "kube-admin-local" user: client-certificate-data: xxxxxx client-key-data: xxxxxx
By config information, you can see two clusters cluster name, context name, and user information.
Configuration file is ready, you can start transfiguration. File synthesis:
cd $HOME/.kube/config
KUBECONFIG=config1:config2 kubectl config view --flatten > $HOME/.kube/config
So how do you use it?
1, see the cluster name and context name
[root@node-01 ~]# kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: https://172.19.8.113:6443 name: cnlocal - cluster: certificate-authority-data: DATA+OMITTED server: https://172.19.8.113:6443 name: jplocal contexts: - context: cluster: cnlocal user: kube-admin-local name: cnlocal - context: cluster: jplocal user: kube-admin-local name: local current-context: jplocal kind: Config preferences: {} users: - name: kube-admin-local user: client-certificate-data: REDACTED client-key-data: REDACTED
2, view the current use of cluster
[root@node-01 ~]# kubectl config current-context jplocal
3. Modify the cluster currently in use
[root@node-01 ~]# kubectl config use-context cnlocal Switched to context "cnlocal".