kubectl
Kubernetes
contexts
multi-cluster management
DevOps

Kubectl how to work with different clusters contexts at the same time

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

kubectl can manage many clusters, users, and namespaces through kubeconfig contexts, but each individual command still talks to one context at a time. Working with multiple clusters "at the same time" really means making context selection explicit so you can switch quickly, run separate commands in parallel, and avoid sending production commands to the wrong place.

Understand What a Context Contains

A Kubernetes context is a named combination of:

  • a cluster
  • a user or credential
  • an optional default namespace

List the contexts you already have:

bash
kubectl config get-contexts
kubectl config current-context

That should be your starting point before doing any multi-cluster workflow changes.

Use --context Per Command

The safest way to work across clusters is to keep the current context unchanged and specify the target context per command:

bash
kubectl --context=dev-cluster get pods -A
kubectl --context=prod-cluster get pods -A

This is especially useful in scripts because it makes the target cluster visible in the command itself. It also avoids subtle bugs caused by a shell session still pointing at yesterday's context.

Switch the Current Context When Needed

If you are spending a while in one environment, switching the active context is fine:

bash
kubectl config use-context dev-cluster
kubectl get pods

Just remember that every later kubectl command in that shell now targets dev-cluster unless you switch again or use --context.

Merge and Organize Kubeconfig Files

You do not need every cluster in one huge file. Kubernetes can merge multiple kubeconfig files via the KUBECONFIG environment variable:

bash
export KUBECONFIG="$HOME/.kube/dev-config:$HOME/.kube/prod-config"
kubectl config get-contexts

This keeps credentials organized by environment while still giving kubectl a combined view.

A practical pattern is one kubeconfig per team or environment, then merge them in the shell where you need both.

Run Multiple Clusters Side by Side

Because kubectl only targets one context per invocation, the easiest "same time" workflow is separate terminals, tmux panes, or shell aliases:

bash
alias kdev='kubectl --context=dev-cluster'
alias kprod='kubectl --context=prod-cluster'

Then you can run:

bash
kdev get deploy
kprod get deploy

This is safer than constantly switching the global current context, especially when production and development commands are both active in your day-to-day work.

Some teams also customize the shell prompt to show the active context and namespace. That is not required, but it is a practical guardrail when context mistakes are expensive.

Namespace and Context Together

Contexts can also carry a default namespace, which reduces repeated flags:

bash
1kubectl config set-context dev-backend \
2  --cluster=dev-cluster \
3  --user=dev-user \
4  --namespace=backend

That can be helpful, but it also increases the need to know exactly which context you are using. A default namespace hidden inside a context can surprise you just as much as a wrong cluster.

Common Pitfalls

  • Relying on current-context in scripts instead of passing --context explicitly.
  • Forgetting which shell is pointed at production.
  • Keeping one giant kubeconfig file with no naming discipline.
  • Assuming contexts solve parallel work automatically when each kubectl command still targets only one cluster.
  • Hiding a risky default namespace inside a context and forgetting it is there.

Summary

  • Each kubectl command uses one context, so explicit context targeting is the key multi-cluster technique.
  • Use --context for scripts and high-safety workflows.
  • Use use-context only when you intentionally want to change the shell's default target.
  • Merge multiple kubeconfig files with KUBECONFIG when that keeps credentials cleaner.
  • Aliases, multiple terminals, and clear context names make multi-cluster work safer and faster.

Course illustration
Course illustration