GeekCoding101 make your way to geek

kubectl Set Context: Switch or Change It

Switch or change the kubectl context with use-context, create or edit one with config set-context, set the namespace, and avoid hitting the wrong cluster.

10 min read
Terminal showing kubectl --context=prod reading the prod context while current-context still prints dev
On this pageShort answer: kubectl config set-context vs use-context

Most kubectl mistakes I want to avoid come down to one question: which cluster and namespace is this command about to hit? Contexts are how kubectl answers that, and kubectl config set-context is the command that builds and edits them.

This post walks through creating, editing and overriding contexts in a safe local lab, then covers habits that keep you out of trouble on real clusters. If you are new to kubectl itself, start with Kubernetes on a Mac.

Short answer: kubectl config set-context vs use-context

kubectl config set-context creates a context or edits an existing one. kubectl config use-context selects which context kubectl uses by default. Editing a context with set-context does not switch to it, unless you edit the one that is already current.

According to the official kubectl config set-context reference, fields you supply are merged into an existing context, and --current modifies the current context.

Commands at a glance

GoalCommand
Create a contextkubectl config set-context NAME --cluster=CLUSTER --user=USER --namespace=NS
Switch to a context (kubectl use-context)kubectl config use-context NAME
Change the namespace of a named contextkubectl config set-context NAME --namespace=NS
Change the namespace of the current contextkubectl config set-context --current --namespace=NS
List contextskubectl config get-contexts
Show the current contextkubectl config current-context
Run one command against another contextkubectl --context=NAME ...
Remove a namespace defaultkubectl config unset contexts.NAME.namespace

What a kubectl context is

A context is a named bundle in your kubeconfig file: a cluster, a user (credentials), and optionally a default namespace. It is only a local mapping on your machine. Creating or editing a context does not create anything in a cluster and does not contact it.

The kubeconfig also stores a current-context value, which says which bundle kubectl uses when you do not say otherwise. For how kubeconfig files are located and merged, see the official organize cluster access with kubeconfig page.

How to switch or change context (kubectl config use-context)

Changing your context is a three-step job: list what you have, switch, then confirm. “Change cluster” usually means the same thing: you switch to a context that points at the other cluster.

I ran this part on 2026-12-03 with kubectl v1.37.3 (Kustomize v5.8.1), in a new throwaway KUBECONFIG holding the dev and prod contexts that section 2 builds, with dev current.

  1. List the contexts. The current one is marked with *:
Terminal window
kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* dev demo demo-user dev
prod demo demo-user prod
  1. Switch:
Terminal window
kubectl config use-context prod
Switched to context "prod".
  1. Confirm:
Terminal window
kubectl config current-context
prod

use-context changes the default for every later command that reads that kubeconfig. To run a single command against another context without switching, use --context.

set-context, use-context, rename-context and delete-context

CommandWhat it doesChanges the current context?
kubectl config set-context NAME ...Creates or edits a context entry (cluster, user, namespace)Does not switch
kubectl config use-context NAMEMakes NAME the current contextYes
kubectl config current-contextPrints the current contextNo
kubectl config rename-context OLD NEWRenames a contextDoes not switch. If OLD was current, current-context becomes NEW
kubectl config delete-context NAMERemoves a context entryDoes not switch, even if you delete the current one (see below)

Renaming the current context keeps you on it under the new name, as the rename-context reference describes:

Terminal window
kubectl config rename-context prod production
kubectl config current-context
Context "prod" renamed to "production".
production

Deleting the current context is the one to watch:

Terminal window
kubectl config delete-context production
kubectl config current-context
kubectl config get-contexts
kubectl get pods
warning: this removed your active context, use "kubectl config use-context" to select a different one
deleted context production from config
production
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
dev demo demo-user dev
Error in configuration: context was not found for specified context: production

current-context still prints the deleted name, no row has a *, and commands that need a context fail until you run kubectl config use-context with one that exists. (For this run KUBECONFIG was the relative path config inside a temporary directory.)

Terminal output of renaming the current context and then deleting it, with the warning and the follow-up error

If you switch often, the third-party tool kubectx shortens this: kubectx NAME switches and kubectx - goes back to the previous context. I did not run it for this post.

1. Keep the lab away from your real kubeconfig

I ran sections 1 to 5 on macOS on 2026-11-09 with kubectl v1.35.0 (Kustomize v5.7.1). The kubeconfig lived in a temporary directory, the server address was the fake https://example.invalid, the user was an empty placeholder, and no real cluster was contacted.

To reproduce that, point KUBECONFIG at a throwaway file before you run anything:

Terminal window
export KUBECONFIG="$(mktemp -d)/config"

KUBECONFIG can also list several files separated by colons, and kubectl merges them. You can use the --kubeconfig flag to point a single command at one file instead. Both are described on the kubeconfig page linked above. Close the terminal (or unset KUBECONFIG) when you are done and your real config is untouched.

2. Create two contexts with kubectl config set-context

A context refers to a cluster and a user, so I first defined both, then created dev and prod contexts that share them and differ only in namespace. Then I switched to dev with use-context.

Terminal window
kubectl version --client
kubectl config set-cluster demo --server=https://example.invalid
kubectl config set-credentials demo-user
kubectl config set-context dev --cluster=demo --user=demo-user --namespace=dev
kubectl config set-context prod --cluster=demo --user=demo-user --namespace=prod
kubectl config use-context dev
kubectl config get-contexts
Client Version: v1.35.0
Kustomize Version: v5.7.1
Cluster "demo" set.
User "demo-user" set.
Context "dev" created.
Context "prod" created.
Switched to context "dev".
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* dev demo demo-user dev
prod demo demo-user prod
Terminal output of creating the dev and prod contexts and listing them with get-contexts

The * in the CURRENT column marks the context kubectl will use. Note the two “created” messages: set-context reports “created” for a new name.

3. kubectl set namespace: set the context namespace

To set the namespace for the context you are on, edit the context:

Terminal window
kubectl config set-context --current --namespace=NS
kubectl config view --minify -o jsonpath='{..namespace}{"\n"}'

The second command prints the namespace back. There is no kubectl set namespace subcommand: kubectl set changes fields on resources (env, image, resources, selector, serviceaccount, subject). Trying it with kubectl v1.37.3 on 2026-12-03:

Terminal window
kubectl set namespace x
error: unknown command "namespace x"
See 'kubectl set -h' for help and examples

To change the default namespace for a named context, run set-context again with just the field you want to change. Because supplied fields are merged, the cluster and user stay as they were.

Terminal window
kubectl config set-context dev --namespace=team-a
kubectl config get-contexts
kubectl config current-context
Context "dev" modified.
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* dev demo demo-user team-a
prod demo demo-user prod
dev

When you want to change the namespace of whatever context is active, you do not need to know its name. Add --current, then read the value back with --minify, which limits the output to the current context:

Terminal window
kubectl config set-context --current --namespace=team-b
kubectl config view --minify -o jsonpath='{.contexts[0].context.namespace}{"\n"}'
Context "dev" modified.
team-b
Terminal output of changing the dev namespace to team-a and then team-b with --current

This is the usual answer to “kubectl set namespace for context”: there is no separate command, it is set-context with --namespace.

4. Use a context for one command without switching

The --context flag runs a single command against another context and leaves current-context alone. Here I asked for the prod context while dev stayed selected:

Terminal window
kubectl --context=prod config view --minify -o jsonpath='{.contexts[0].name}{" "}{.contexts[0].context.namespace}{"\n"}'
kubectl config current-context
kubectl config get-contexts
prod prod
dev
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* dev demo demo-user team-b
prod demo demo-user prod
Terminal output showing a one-off --context=prod command while the current context stays dev

The first line shows kubectl resolved the prod context and its namespace. Afterwards, current-context is still dev. This is the safest way to touch another cluster, because nothing lingers after the command finishes.

5. What happens with a typo, and how to remove a namespace default

Switching to a context that does not exist fails and leaves the current context unchanged. I added echo "exit=$?" to show the exit code:

Terminal window
kubectl config use-context missing; echo "exit=$?"
kubectl config current-context
error: no context exists with the name: "missing"
exit=1
dev

To remove a namespace default from a context, unset the property. Without a namespace, kubectl falls back to default.

Terminal window
kubectl config unset contexts.dev.namespace
kubectl config view --minify
Property "contexts.dev.namespace" unset.
apiVersion: v1
clusters:
- cluster:
server: https://example.invalid
name: demo
contexts:
- context:
cluster: demo
user: demo-user
name: dev
current-context: dev
kind: Config
users:
- name: demo-user
user: {}
Terminal output of a failed use-context on a missing name and then unsetting the dev namespace

The dev context entry now has only cluster and user, with no namespace line.

Safety habits for real clusters

  • Check before destructive commands. Run kubectl config current-context before delete, apply or drain. It is one short command and it prints one word.
  • Prefer explicit --context in scripts. A script that relies on whatever was selected last will eventually run against the wrong place. kubectl --context=NAME ... makes the target part of the command.
  • Name contexts clearly. Names like prod-eu-admin and staging-readonly beat cluster1 when you are tired.
  • Show the context in your shell prompt. Many people add the current context and namespace to their prompt so the answer is always visible. Choose any approach you trust.
  • Keep a namespace default per context so you do not forget -n and land in default.

If you want to understand what the cluster side looks like, see Kubernetes control plane components and cluster setup. The official multi-cluster configuration walkthrough is also worth reading.

Troubleshooting

”error: current-context is not set”

kubectl prints error: current-context is not set when the kubeconfig has no current-context, often because it is new or empty. Run kubectl config get-contexts to see what exists, then kubectl config use-context NAME.

I changed a context but requests still go elsewhere

Check what overrides it:

  • A --context, --namespace or --kubeconfig flag in the command, or in a shell alias or wrapper.
  • A KUBECONFIG variable pointing at a different file, or listing several files that are merged. See the kubeconfig page for precedence rules.
  • Editing a different context than the active one. set-context NAME does not switch to NAME.

kubectl config view --minify shows what kubectl resolved for the current context.

The context exists but the API call fails

A valid context only means the local entry is well formed. Connectivity (server address, network, VPN) and authentication (credentials, expiry) are separate problems. In my lab, https://example.invalid is deliberately unreachable, so any real API call would fail by design.

FAQ

What is the difference between kubectl set-context and use-context?

set-context creates or edits a context entry. use-context selects which context is current. Creating a context does not select it.

How do I change the kubectl context?

Run kubectl config get-contexts to see the names, then kubectl config use-context NAME. Check with kubectl config current-context.

How do I set the current context?

kubectl config use-context NAME sets it. kubectl config set-context only creates or edits a context; it does not switch to it.

How do I rename a context?

Run kubectl config rename-context OLD NEW. If OLD is the current context, kubectl updates current-context to NEW for you.

How do I change the default namespace?

Run kubectl config set-context --current --namespace=NS for the active context, or kubectl config set-context NAME --namespace=NS for a named one.

How do I list contexts?

Run kubectl config get-contexts. The * in the CURRENT column marks the active one.

How do I delete a context?

Run kubectl config delete-context NAME. If NAME is the current context, kubectl warns you but leaves current-context pointing at the deleted name, so switch to another context afterwards.

Does set-context create a cluster or user?

No. It only references a cluster and user by name. Define those with kubectl config set-cluster and kubectl config set-credentials, as in step 2.

How do I see the current namespace?

Run kubectl config view --minify -o jsonpath='{.contexts[0].context.namespace}{"\n"}'. An empty line means no namespace is set, so kubectl uses default.

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.

Keep reading

All posts