Difficulty: Beginner
How do you switch between different Kubernetes clusters (and namespaces) with kubectl, and how is the kubeconfig file organised?
kubectl reads a kubeconfig file (by default ~/.kube/config) that lists clusters, users and contexts. A context ties one cluster, one user and an optional default namespace together, and kubectl config use-context <name> makes it the active one. You can also target a context for a single command with --context, or change the active namespace with kubectl config set-context --current --namespace=<ns>.
clusters: API server URL and CA certificate for each cluster.users: credentials, such as a client certificate, a token, or an exec plugin that fetches short-lived tokens (EKS uses aws eks get-token, GKE uses gke-gcloud-auth-plugin).contexts: named combinations of cluster + user + namespace.current-context: the context kubectl uses when you do not pass --context.Cloud CLIs write entries for you: aws eks update-kubeconfig --name prod --region ap-south-1, gcloud container clusters get-credentials prod --region asia-south1, or az aks get-credentials.
| Task | Command |
|---|---|
List contexts (current marked with *) | kubectl config get-contexts |
| Show the active context | kubectl config current-context |
| Switch cluster | kubectl config use-context staging |
| Run one command against another cluster | kubectl --context prod get pods |
| Set default namespace for current context | kubectl config set-context --current --namespace=payments |
| Rename a long cloud-generated name | kubectl config rename-context arn:aws:eks:... prod |
The KUBECONFIG environment variable can hold several files separated by : on Linux and macOS (; on Windows). kubectl merges them in memory. To permanently merge, run KUBECONFIG=a:b kubectl config view --flatten > merged. Some teams keep one file per cluster and point KUBECONFIG at the one they need per terminal, which avoids accidents.
The most common real incident here is running a destructive command against production while thinking you are on staging. Practical safeguards:
kubectx and kubens for fast, visible switching.--context in scripts and CI rather than relying on current-context.kubectl config get-contexts
# CURRENT NAME CLUSTER AUTHINFO NAMESPACE
# * staging stg-eks stg-dev default
# prod prod-eks prod-admin payments
kubectl config use-context prod
kubectl config set-context --current --namespace=orders
kubectl config view --minify # show only the active context
# One-off command without switching
kubectl --context staging get pods -n orders
use-context changes the default for all later commands, while --context affects only the single command.
apiVersion: v1
kind: Config
current-context: staging
clusters:
- name: stg-eks
cluster:
server: https://ABC123.gr7.ap-south-1.eks.amazonaws.com
certificate-authority-data: <base64-ca>
users:
- name: stg-dev
user:
exec:
apiVersion: client.authentication.k8s.io/v1beta1
command: aws
args: [eks, get-token, --cluster-name, staging]
contexts:
- name: staging
context:
cluster: stg-eks
user: stg-dev
namespace: default
The exec plugin fetches a short-lived token on each call, so no long-lived secret is stored in the file.
Kubeconfig, Context, KUBECONFIG, kubectl config, Namespaces