Kubernetes configure kubectl to use multiple clusters

First of all, you need to have kubectl CLI in your host machine. So, follow the below steps.

Install with Chocolatey on Windows

choco install kubernetes-cli

Install with Homebrew on MacOS

brew install kubernetes-cli

Install with Kubernetes Official Documentation(with Curl)

Follow this link https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-on-macos

Test to ensure the version you installed

kubectl version

Checking kubectl config

When you run below command, you will see output as shown in the following screenshot. I didn’t configure yet so, it shows an empty config.

kubectl config view

Creating ~/.kube/config file

In order to connect a Kubernetes cluster from your host machine, there should be $HOME/.kube/config (default location) file in your local machine if you didn’t set KUBECONFIG environment variable. So, run the below command to create that file in your local machine. 🙂

mkdir -p $HOME/.kube && nano $HOME/.kube/config

Now we need to copy /etc/kubernetes/admin.conf or $HOME/.kube/config or $HOME/admin.conf file content (both files should be same) from Kubernetes master node terminal as show in the following screenshot

After copying Kubernetes kubectl config from master node, paste in your host machine kubectl config file ($HOME/.kube/config) like below screenshot and save.

Connecting multiple Kubernetes cluster with kubeconfig

If you have multiple Kubernetes cluster config file in your host machine, then you can connect different Kubernetes clusters with the following commands.

kubectl get nodes --kubeconfig $HOME/admin_buildServerCluster.conf
kubectl get nodes --kubeconfig $HOME/admin_backEndCluster.conf
kubectl get nodes --kubeconfig $HOME/admin_gatewayCluster.conf

Connecting multiple Kubernetes cluster with server

If you don’t have kubectl config files in your host machine, then you can use server option of kubectl command as well.

kubectl get nodes --server=https://192.168.33.10:6443
kubectl get nodes --server=https://192.172.44.58:6443

Leave a Reply

Your email address will not be published. Required fields are marked *