Skip to content

How to Switch Between Kubernetes Contexts

DodaTech 2 min read

In this tutorial, you'll learn about How to Switch Between Kubernetes Contexts. We cover key concepts, practical examples, and best practices.

The Problem

You run kubectl get pods and connect to the wrong cluster or namespace. With multiple clusters (dev, staging, production) in your kubeconfig, it's easy to accidentally run destructive commands on the wrong environment. Kubernetes contexts let you switch between clusters, users, and namespaces in a single command. Without proper context management, you might delete production resources while thinking you're working on a dev cluster.

Quick Fix

1. List all available contexts

kubectl config get-contexts

Expected output:

CURRENT   NAME                  CLUSTER               AUTHINFO              NAMESPACE
*         minikube              minikube              minikube              default
          dev-cluster           dev-cluster           dev-admin             dev
          prod-cluster          prod-cluster           prod-admin            production

The * marks your current context. The NAMESPACE column shows the default namespace for each context.

2. Switch to a different context

kubectl config use-context dev-cluster

All subsequent kubectl commands run against the dev cluster. Verify:

kubectl config current-context
# Output: dev-cluster

3. View the current context

kubectl config current-context

4. Set a different namespace for the current context

kubectl config set-context --current --namespace=staging

After this, kubectl get pods without the -n flag will show pods in the staging namespace of the current cluster.

5. Create a new context

kubectl config set-context my-context \
  --cluster=prod-cluster \
  --user=prod-admin \
  --namespace=production

This is useful when you need a context with a different namespace than the default one defined in your kubeconfig.

6. Delete a context

kubectl config delete-context my-context

7. Switch context quickly with an alias (optional)

alias kctx='kubectl config use-context'
kctx dev-cluster

8. Add context to shell prompt (KUBE_PS1)

# Install kube-ps1
brew install kube-ps1  # macOS
source "$(brew --prefix)/opt/kube-ps1/share/kube-ps1.sh"
PS1='$(kube_ps1)'$PS1

Common Causes

Cause Symptom Fix
Wrong cluster selected Pods not found in expected namespace kubectl config use-context <name>
Multiple kubeconfig files Contexts appear/disappear Merge with KUBECONFIG env var
Namespace not set default namespace when you expect production kubectl config set-context --current --namespace=<name>
Context renamed or deleted kubectl can't find the server Re-add the cluster config

Prevention

  • Use kubectx (a popular tool) for faster context switching: kubectx <name>
  • Rename contexts clearly: kubectl config rename-context old-name new-name
  • Always run kubectl config current-context before destructive commands
  • Add a shell prompt indicator showing the current Kubernetes context to prevent accidents
  • Use alias krun='kubectl --context=prod-cluster --namespace=production' for production-only commands

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro