How to configure Kubectl client in Multi-K8s Cluster Handoff
This article mainly introduces how to configure the Kubectl client in multi-K8s cluster switching, which has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.
Profile location of kubectl
The main location of the kubectl configuration file is $HOME / .kube, and by default we have a configuration file called 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
Multiple profiles and KUBECONFIG variables
By setting the appropriate KUBECONFIG shell variable, we can have multiple configuration files.
For example:
Export KUBECONFIG=$KUBECONFIG:/root/.kube/additional_config
Check the configuration from kubectl
We can check the current configuration (since there is nothing in my Additional_config file, it will be empty-we'll add something later-this is just a file in my KUBECONFIG variable):
[node1 ~] $kubectl config view apiVersion: v1 clusters: [] contexts: [] current-context: "" kind: Config preferences: {} users: []
Or from a specific file-let's look at the default file:
[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
If you do not set the KUBECONFIG variable, all configurations will be obtained from $HOME / .kube / config
Objects in kubectl configuration
Kubectl has the following object types in its configuration file
Cluster-information about the K8s cluster-contains cluster name and connection parameters
Users- information about the users you want to connect to the K8s cluster
Context-three times the cluster / user / namespace
Modify configuration from kubectl
Add Cluster
[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.
New users
[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.
Add context
[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.
Remove cluster / user / context from configuration
Kubectl-- kubeconfig=.kube/additional_config config unset users. Kubectl-- kubeconfig=.kube/additional_config config unset clusters. Kubectl-- kubeconfig=.kube/additional_config config unset contexts.
Sample configuration
[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
Change the environment
To get a list of contexts (you don't need-- kubeconfig, because we have added extra_config to the KUBECONFIG variable):
[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
Set the context:
[node1 ~] $kubectl config use-context simple_app_development Switched to context "simple_app_development".
Get the current context:
[node1 ~] $kubectl config current-context simple_app_development
Thank you for reading this article carefully. I hope the article "how to configure Kubectl clients in multi-K8s cluster switching" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!