DEV Community

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

Posted on

3 2

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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay