DEV Community

Cover image for Using kubectl to manage multi clusters
Khoi Doan (Osbkca)
Khoi Doan (Osbkca)

Posted on

Using kubectl to manage multi clusters

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:

kubectl config view
Enter fullscreen mode Exit fullscreen mode

Result:

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
Enter fullscreen mode Exit fullscreen mode

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.

export KUBECONFIG=~/.kube/config:~/.kube/cluster-second.config
kubectl config view --flatten > ~/.kube/config_temp
mv ~/.kube/config_temp ~/.kube/config
Enter fullscreen mode Exit fullscreen mode

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

CURRENT   NAME              CLUSTER            AUTHINFO   NAMESPACE
*         cluster-one.me    cluster-one.me     osbkca
          cluster-second.me cluster-sencond.me osbkca
Enter fullscreen mode Exit fullscreen mode

You can switch context of kubectl with the command kubeclt config use-context cluster-sencond.me

Wooow! Enjoy it. More info

Top comments (0)