Profile PictureKhoi Doan (Osbkca)
Open menu

Using kubectl to manage multi clusters

December 8, 2022

(Updated on December 16, 2022)

article cover

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:

SHELL
kubectl config view

Result:

YAML
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://192.168.1.50:6443
name: cluster-one.me
contexts:
- context:
cluster: cluster-one.me
user: khoidn
name: cluster-one.me
current-context: cluster-one.me
kind: Config
preferences: {}
users:
- name: osbkca
user:
client-certificate-data: REDACTED
client-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.

SHELL
export KUBECONFIG=~/.kube/config:~/.kube/cluster-second.config
kubectl config view --flatten > ~/.kube/config_temp
mv ~/.kube/config_temp ~/.kube/config

Now you run kubectl config get-contexts to see the differences.

SHELL
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* cluster-one.me cluster-one.me osbkca
cluster-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.

👍LIKE
❤️LOVE
👏CLAP
🎉PARTY

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!