Configure Access to Multiple Clusters in Kubernetes

Sanket NankarSanket Nankar
2 min read

URL: https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/

Create a directory named config-exercise.

In your config-exercise directory, create a file named config-demo with this content:

apiVersion: v1
kind: Config
preferences: {}

clusters:
- cluster:
  name: dev
- cluster:
  name: test

users:
- name: developer
- name: tester

contexts:
- context:
  name: dev-frontend
- context:
  name: dev-qa
- context:
  name: test-expt

πŸ“Œ 1. Add clusters

kubectl config --kubeconfig=config-demo set-cluster dev \
  --server=https://1.2.3.4 \
  --certificate-authority=fake-ca-file

kubectl config --kubeconfig=config-demo set-cluster test \
  --server=https://5.6.7.8 \
  --insecure-skip-tls-verify

πŸ“Œ 2. Add users

kubectl config --kubeconfig=config-demo set-credentials developer \
  --client-certificate=fake-cert-file \
  --client-key=fake-key-seefile

kubectl config --kubeconfig=config-demo set-credentials tester \
  --username=test-user \
  --password=test@123

πŸ“Œ 3. Add contexts

kubectl config --kubeconfig=config-demo set-context dev-frontend \
  --cluster=dev \
  --user=developer \
  --namespace=dev-ns

kubectl config --kubeconfig=config-demo set-context dev-qa \
  --cluster=dev \
  --user=tester \
  --namespace=qa-ns

kubectl config --kubeconfig=config-demo set-context test-expt \
  --cluster=test \
  --user=tester \
  --namespace=expt-ns

πŸ“Œ 4. Delete entries

# Delete a user
kubectl config --kubeconfig=config-demo unset users.<name>

# Delete a cluster
kubectl config --kubeconfig=config-demo unset clusters.<name>

# Delete a context
kubectl config --kubeconfig=config-demo unset contexts.<name>

πŸ“Œ 5. View configuration

# View entire config file
kubectl config --kubeconfig=config-demo view

# View only the current context
kubectl config --kubeconfig=config-demo view --minify

The output shows the two clusters, two users, and three contexts:

apiVersion: v1
clusters:
- cluster:
    certificate-authority: fake-ca-file
    server: https://1.2.3.4
  name: dev
- cluster:
    insecure-skip-tls-verify: true
    server: https://5.6.7.8
  name: test
contexts:
- context:
    cluster: dev
    namespace: dev-ns
    user: developer
  name: dev-frontend
- context:
    cluster: dev
    namespace: qa-ns
    user: developer
  name: dev-qa
- context:
    cluster: test
    namespace: expt-ns
    user: tester
  name: test-exp
current-context: ""
kind: Config
preferences: {}
users:
- name: developer
  user:
    client-certificate: fake-cert-file
    client-key: fake-key-file
- name: tester
  user:
    password: test
    username: test@123

πŸ“Œ 6. Switch context

# Use dev context
kubectl config --kubeconfig=config-demo use-context dev-frontend

# Switch to test context
kubectl config --kubeconfig=config-demo use-context dev-qa

# Switch to expt context
kubectl config --kubeconfig=config-demo use-context test-expt

Set the KUBECONFIG environment variable

Workflow to temporarily switch Kubeconfigs and restore the original:

1. Save your current $KUBECONFIG

export KUBECONFIG_SAVED="$KUBECONFIG"

2. Point to a different kubeconfig

(for example, a test cluster config file):

export KUBECONFIG=$HOME/.kube/test-cluster-config

Now kubectl will use only that config.

3. Merge another config temporarily

If you want to combine your current with another:

export KUBECONFIG="${KUBECONFIG}:${HOME}/.kube/another-config"

Now kubectl sees both.

4. Check which contexts are available

kubectl config get-contexts

5. Restore your original configuration

When you’re done testing:

export KUBECONFIG="$KUBECONFIG_SAVED"

βœ… This way you can safely switch between clusters without losing your original kubeconfig setup.

0
Subscribe to my newsletter

Read articles from Sanket Nankar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sanket Nankar
Sanket Nankar