Configure Access to Multiple Cluster using KUBE_CONFIG

Simple way on making access to multiple k8s cluster

Scenario

We have multiple K8s cluster:

  1. A cluster in an on-premise environment (Microk8S)

  2. A cluster on the Cloud environment (AWS)

  3. A cluster on the Cloud environment (Digital Ocean)

You may create multiple config files under the ~/.kube/ directory.

Example:

┌─[admin@my-dev-svr] - [~/.kube] - [Mon May 22, 21:50]
└─[$]> tree
.
├── aws-cluster-01
├── do-k8s-01-kubeconfig
└── [email protected]

0 directories, 3 files
┌─[admin@my-dev-svr] - [~/.kube] - [Mon May 22, 21:50]
└─[$]>

As you can see on the example above, aws-cluster-01 is the config files for cluster that hosted on AWS, do-k8s-01-kubeconfig is for the cluster access on the Digital Ocean, while the last one [email protected] is MicroK8s cluster that reside on the on-premise environment.

Then, you have to modify .bash_profile or .zshrc to combine all the access config into one liner KUBE_CONFIG environment variable like the following:

#On Prem K8s
export CONFIG_MICROK8S=$HOME/.kube/[email protected]

#Cloud K8s
export CONFIG_DO=$HOME/.kube/do-k8s-01-kubeconfig
export CONFIG_AWS=$HOME/.kube/aws-cluster-01

#Merge all the configs
export KUBECONFIG=$CONFIG_MICROK8S:$CONFIG_DO:$CONFIG_AWS

Finally, test the connectivity.

Gist Source: https://gist.github.com/malikperang/82db3f21ac0fde6ab1752ce329f782e5

Last updated