Using kubectl to manage multi clusters
December 8, 2022
•
(Updated on December 16, 2022)
The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs. For more information including a complete list of kubectl operations, see the kubectl documentation.
In this tutorial, I want to share with you how to connect to multi Kubernetes Cluster by using kubectl on a computer.
For configuration, kubectl looks for a file named config in the $HOME/.kube directory. To show current configuration, you run command:
SHELLkubectl config view
Result:
YAMLapiVersion: v1clusters:- cluster:certificate-authority-data: DATA+OMITTEDserver: https://192.168.1.50:6443name: cluster-one.mecontexts:- context:cluster: cluster-one.meuser: khoidnname: cluster-one.mecurrent-context: cluster-one.mekind: Configpreferences: {}users:- name: osbkcauser:client-certificate-data: REDACTEDclient-key-data: REDACTED
In above configuration, we have one cluster cluster-one.me. If you have another cluster cluster-second.me with file config at ~/.kube/cluster-second.config . Use the following commands to merge configuration files.
SHELLexport KUBECONFIG=~/.kube/config:~/.kube/cluster-second.configkubectl config view --flatten > ~/.kube/config_tempmv ~/.kube/config_temp ~/.kube/config
Now you run kubectl config get-contexts to see the differences.
SHELLCURRENT NAME CLUSTER AUTHINFO NAMESPACE* cluster-one.me cluster-one.me osbkcacluster-second.me cluster-sencond.me osbkca
You can switch context of kubectl with the command kubeclt config use-context cluster-sencond.me
Wooow! Enjoy it.
Updates delivered to your inbox!
A periodic update about my life, recent blog posts, how-tos, and discoveries.
As a thank you, I'll also send you a Free CSS tutorial!
No spam - unsubscribe at any time!
More articles
If you enjoyed this article, you'll find these insightful too!