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:
  name: dev-config
- context:
    cluster: dev-config
    user: dev-admin
  name: dev-config
- 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



source ~/.bashrc

Use clusters with kubectl

# get the current context
kubectl config current-context

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