0
0
Kubernetesdevops~10 mins

Switching namespace context in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Switching namespace context
Start: Current kubectl context
Check current namespace
Run command: kubectl config set-context --current --namespace=<new-namespace>
Update context namespace
Verify with kubectl config view or kubectl config get-contexts
kubectl commands now use new namespace
END
This flow shows how you change the current kubectl namespace context so commands run in the new namespace.
Execution Sample
Kubernetes
kubectl config set-context --current --namespace=dev
kubectl get pods
Switch current kubectl context to 'dev' namespace, then list pods in 'dev' namespace.
Process Table
StepCommandContext Namespace BeforeActionContext Namespace AfterOutput
1kubectl config get-contexts --no-headers -o namedefaultCheck current context namedefaultcurrent-context-name
2kubectl config view --minify --output 'jsonpath={..namespace}'defaultCheck current namespacedefaultdefault
3kubectl config set-context --current --namespace=devdefaultSet current context namespace to 'dev'devContext updated.
4kubectl config view --minify --output 'jsonpath={..namespace}'devVerify namespace changedevdev
5kubectl get podsdevList pods in new namespacedevNo resources found in dev namespace.
6kubectl config set-context --current --namespace=defaultdevSwitch back to default namespacedefaultContext updated.
7kubectl get podsdefaultList pods in default namespacedefaultpod1 pod2 pod3
8-defaultEnd of demonstrationdefault-
💡 Namespace context switched successfully; kubectl commands now run in the selected namespace.
Status Tracker
VariableStartAfter Step 3After Step 6Final
current_namespacedefaultdevdefaultdefault
Key Moments - 3 Insights
Why do kubectl commands still show resources from the default namespace before switching?
Because the current context's namespace is 'default' initially (see execution_table step 2), so kubectl commands run in 'default' namespace until changed.
Does 'kubectl config set-context --current --namespace=dev' create the namespace 'dev' if it doesn't exist?
No, it only changes the context's namespace setting. The namespace must exist already or kubectl commands may fail or show no resources (see step 5).
How to verify the namespace context has changed?
Use 'kubectl config view --minify --output jsonpath={..namespace}' as shown in steps 2 and 4 to see the current namespace.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the namespace after step 3?
Adefault
Bdev
Ckube-system
Dproduction
💡 Hint
Check the 'Context Namespace After' column in step 3.
At which step does the kubectl command start listing pods in the 'dev' namespace?
AStep 2
BStep 4
CStep 5
DStep 7
💡 Hint
Look at the 'Command' and 'Output' columns to find when pods are listed in 'dev'.
If you run 'kubectl get pods' without switching namespace, which namespace is used by default?
Adefault
Bdev
Ckube-public
Dcustom
💡 Hint
See the 'Context Namespace Before' in step 1 and 2.
Concept Snapshot
Switch namespace context in kubectl:
- Use: kubectl config set-context --current --namespace=<namespace>
- Changes current context's namespace
- Verify with: kubectl config view --minify --output 'jsonpath={..namespace}'
- Commands like 'kubectl get pods' run in new namespace
- Namespace must exist beforehand
Full Transcript
This lesson shows how to switch the namespace context in kubectl. You start by checking your current namespace, usually 'default'. Then you run 'kubectl config set-context --current --namespace=dev' to change the namespace to 'dev'. After that, kubectl commands like 'kubectl get pods' will operate in the 'dev' namespace. You can verify the change by viewing the current namespace in the context. Switching back to 'default' is done the same way. Remember, changing the context does not create namespaces; they must exist already.