Skip to main content

Advanced Usage of kubeconfig

Updated at: 2025-11-19 14:08:25

A kubeconfig file is used by Kubernetes (K8s) to configure cluster access. It contains connection information, authentication credentials, and context definitions. The following sections describe several advanced use cases for kubeconfig files.

Configure Multiple Clusters

You can configure multiple clusters, users, and contexts within a single kubeconfig file, enabling easy switching between different clusters.

Example:

apiVersion: v1
clusters:
- cluster:
insecure-skip-tls-verify: true
server: 'https://vcluster.hd-01.alayanew.com:21443/inCluster/*******p5'
name: username_1
- cluster:
insecure-skip-tls-verify: true
server: 'https://vcluster.hd-01.alayanew.com:21443/inCluster/*******24'
name: username_2
contexts:
- context:
cluster: cluster_1
user: username_1
name: context_name_1
- context:
cluster: cluster_2
user: username_2
name: context_name_2
current-context: context_name_1
kind: Config
preferences: {}
users:
- name: username_1
user:
token: >-
***********************
- name: username_2
user:
token: >-
***********************

Use Environment Variables

You can specify the path of a kubeconfig file by setting the KUBECONFIG environment variable:

export KUBECONFIG=/mnt/vcluster/kube_config

View All Contexts

Use the command kubectl config get-contexts to list all contexts in the kubeconfig file.

kubectl config get-contexts

The result is shown below, where the asterisk (*) indicates the currently active context.

image

Viewing the Current Context

Use the command kubectl config current-context to display the current context:

kubectl config current-context

The result is shown below:

image

Switching Contexts

Use the command kubectl config use-context to switch to another context using.

kubectl config use-context appdev-huangxs

The result is shown below:

image