Kubeconfig Contexts

Difficulty: Beginner

Question

How do you switch between different Kubernetes clusters (and namespaces) with kubectl, and how is the kubeconfig file organised?

Answer

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>.

What is inside a kubeconfig

  • 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.

Day-to-day commands

TaskCommand
List contexts (current marked with *)kubectl config get-contexts
Show the active contextkubectl config current-context
Switch clusterkubectl config use-context staging
Run one command against another clusterkubectl --context prod get pods
Set default namespace for current contextkubectl config set-context --current --namespace=payments
Rename a long cloud-generated namekubectl config rename-context arn:aws:eks:... prod

Using multiple files

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.

Avoiding the classic mistake

The most common real incident here is running a destructive command against production while thinking you are on staging. Practical safeguards:

  • Show the current context and namespace in your shell prompt (kube-ps1, Starship).
  • Use kubectx and kubens for fast, visible switching.
  • Prefer --context in scripts and CI rather than relying on current-context.
  • Give production read-only credentials by default and use a separate context with elevated access when needed.
  • Never commit a kubeconfig to Git; it often contains tokens or private keys.
How a kubeconfig context is built. current-context selects a context, which points to a cluster, a user and a namespace.
  1. current-context: prod: The default kubectl uses for every command.
  2. context prod: Combines cluster, user and default namespace.
  3. cluster prod-eks: API server URL and CA certificate.
  4. user prod-admin: Certificate, token or exec plugin for auth.
  5. namespace payments: Used when a command does not pass -n.

Code examples

Switching clusters and namespaces

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.

Minimal kubeconfig structure

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.

Key points

  • Context = cluster + user + optional namespace; current-context is the default.
  • Switch with kubectl config use-context; target once with --context.
  • Set the default namespace with kubectl config set-context --current --namespace.
  • KUBECONFIG can merge several files (colon separated, semicolon on Windows).
  • Show the active context in your prompt to avoid running commands on production by mistake.

Common follow-up questions

  • How does kubectl authenticate to an EKS or GKE cluster?
  • How would you give a teammate access to a cluster without sharing your kubeconfig?
  • What happens if two kubeconfig files define a context with the same name?
  • How do you make sure CI pipelines deploy to the right cluster?

Interview tips

  • Explain the three building blocks (clusters, users, contexts) before listing commands.
  • Mention a safety habit like showing the context in your prompt; interviewers value operational care.
  • Know how your cloud provider generates kubeconfig entries.

Concepts covered

Kubeconfig, Context, KUBECONFIG, kubectl config, Namespaces

More Kubernetes & Orchestration questions