Configure kubectl for multiple clusters

Page content

I use a multiple Kubernetes clusters on a daily basis, so keeping my configs organized is important to don’t confuse myself.

kubectl looks at an environment variable called KUBECONFIG to hold a colon-separated list of paths to configuration files, so I can use multiple cluster config files.

Download kubernetes config

scp root@DEV_SERVER:/etc/kubernetes/admin.conf ~/.kube/dev-config
scp root@TST_SERVER:/etc/kubernetes/admin.conf ~/.kube/tst-config
scp root@UAT_SERVER:/etc/kubernetes/admin.conf ~/.kube/uat-config
scp root@PROD_SERVER:/etc/kubernetes/admin.conf ~/.kube/prod-config

Edit config files

nano ~/.kube/dev-config
...
- cluster:
    server: https://1.1.1.1:6443
  name: dev-config
...
contexts:
- context:
    cluster: dev-config
    user: dev-admin
  name: dev-config
...
users:
- name: dev-admin
...

Use config files in KUBECONFIG variable

nano ~/.bashrc
export KUBECONFIG=$HOME/.kube/dev-config:$HOME/.kube/tst-config:$HOME/.kube/uat-config:$HOME/.kube/prod-config

echo $KUBECONFIG

/home/ME/.kube/dev-config:/home/ME/.kube/tst-config:/home/ME/.kube/uat-config:/home/ME/.kube/prod-config

source ~/.bashrc

Use clusters with kubectl

# get the current context
kubectl config current-context

# use a different context
kubectl config use-context work-dev