0
0
Kubernetesdevops~20 mins

Switching namespace context in Kubernetes - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Namespace Context Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of switching namespace context?
You run the command kubectl config set-context --current --namespace=dev. What will be the output or effect of this command?
AThe command outputs 'Switched to namespace dev' and changes the context.
BAn error message saying 'namespace dev not found' is displayed.
CThe current context's namespace is changed to 'dev' with no output message.
DThe current context is deleted and a new context named 'dev' is created.
Attempts:
2 left
💡 Hint
Think about what the command does to the current context and if it outputs messages.
💻 Command Output
intermediate
2:00remaining
What happens if you switch to a non-existent namespace?
You run kubectl config set-context --current --namespace=unknown but the namespace 'unknown' does not exist. What is the result?
AThe context namespace is set to 'unknown' even if it does not exist, with no error.
Bkubectl returns an error: 'namespace unknown not found'.
CThe command fails with a syntax error.
Dkubectl creates the 'unknown' namespace automatically and switches context.
Attempts:
2 left
💡 Hint
Does kubectl validate namespace existence when setting context namespace?
🔀 Workflow
advanced
3:00remaining
Correct sequence to switch namespace context and verify
Arrange the following commands in the correct order to switch the current kubectl context to namespace 'test' and verify the change.
A1,3,2,4
B1,4,3,2
C1,2,4,3
D4,1,3,2
Attempts:
2 left
💡 Hint
First switch namespace, then check pods in that namespace, then verify namespace in context, finally list all namespaces.
Troubleshoot
advanced
2:00remaining
Why does kubectl still show pods from default namespace after switching?
You ran kubectl config set-context --current --namespace=prod but kubectl get pods still shows pods from the 'default' namespace. What is the most likely cause?
AYou did not reload or restart your shell session after switching namespaces.
BYou are overriding the namespace with the <code>--namespace</code> flag in your get pods command.
CYou have multiple contexts and the current context was not changed.
DThe 'prod' namespace does not exist, so kubectl defaults to 'default'.
Attempts:
2 left
💡 Hint
Check if any command line flags override the context namespace.
Best Practice
expert
3:00remaining
Best practice to switch namespace context safely in scripts
You want to write a script that switches the kubectl context namespace to 'staging' only if the namespace exists. Which approach is best?
ACreate the namespace with <code>kubectl create namespace staging</code> before switching context.
BDelete the current context and create a new one with namespace 'staging'.
CDirectly run <code>kubectl config set-context --current --namespace=staging</code> without checking.
DRun <code>kubectl get namespace staging</code> and if it exists, run <code>kubectl config set-context --current --namespace=staging</code>.
Attempts:
2 left
💡 Hint
Think about avoiding errors in scripts by checking resource existence first.