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:
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:
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:
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:
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:
Then you can run:
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:
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-contextin scripts instead of passing--contextexplicitly. - Forgetting which shell is pointed at production.
- Keeping one giant kubeconfig file with no naming discipline.
- Assuming contexts solve parallel work automatically when each
kubectlcommand still targets only one cluster. - Hiding a risky default namespace inside a context and forgetting it is there.
Summary
- Each
kubectlcommand uses one context, so explicit context targeting is the key multi-cluster technique. - Use
--contextfor scripts and high-safety workflows. - Use
use-contextonly when you intentionally want to change the shell's default target. - Merge multiple kubeconfig files with
KUBECONFIGwhen that keeps credentials cleaner. - Aliases, multiple terminals, and clear context names make multi-cluster work safer and faster.

