A Kubernetes context is the kubeconfig entry that tells kubectl which cluster, user, and default namespace to use. Switching context before a cluster command keeps work meant for one team, namespace, or environment from landing in another.
kubectl reads context data from the kubeconfig selected by --kubeconfig, KUBECONFIG, or $HOME/.kube/config. The context name is only a label, so list the available names and confirm the active one before running commands that create, update, or delete resources.
A harmless read after the switch confirms that the selected context reaches the expected namespace before any mutating command runs. Use the command-specific --context flag instead when only one command should target another context.
Steps to switch Kubernetes context with kubectl:
- List the available context names.
$ kubectl config get-contexts -o name team-a team-b
Run kubectl config get-contexts without -o name when the cluster, user, or namespace columns are needed before choosing a context.
- Check the active context before switching.
$ kubectl config current-context team-a
- Switch to the intended context.
$ kubectl config use-context team-b Switched to context "team-b".
kubectl config use-context updates the current-context field in the selected kubeconfig file. Confirm KUBECONFIG or use --kubeconfig before switching inside a shared shell.
- Confirm that kubectl now points at the selected context.
$ kubectl config current-context team-b
- Run a harmless read command in the active context.
$ kubectl get pods No resources found in team-b namespace.
The empty namespace message is still a successful API read. A forbidden, timeout, or certificate error means the active context changed, but access or connectivity still needs checking.
Related: How to check Kubernetes cluster access
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.