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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

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

Okay