Bird
Raised Fist0
Kubernetesdevops~20 mins

Switching namespace context in Kubernetes - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does switching the namespace context in Kubernetes help you do?
easy
A. Work easily within a specific namespace without typing it every time
B. Delete all namespaces at once
C. Create a new Kubernetes cluster
D. Change the Kubernetes version

Solution

  1. Step 1: Understand namespaces in Kubernetes

    Namespaces organize resources so you can separate projects or teams.
  2. Step 2: Understand switching namespace context

    Switching context means your commands default to that namespace, so you don't type it each time.
  3. Final Answer:

    Work easily within a specific namespace without typing it every time -> Option A
  4. Quick Check:

    Switching namespace context = easier work in one namespace [OK]
Hint: Switching namespace saves typing namespace each command [OK]
Common Mistakes:
  • Thinking switching namespace deletes namespaces
  • Confusing namespace with cluster creation
  • Believing it changes Kubernetes version
2. Which of the following is the correct command to switch the current namespace to dev?
easy
A. kubectl set namespace dev
B. kubectl config set-context --current --namespace=dev
C. kubectl switch namespace dev
D. kubectl change context namespace dev

Solution

  1. Step 1: Recall the correct syntax for switching namespace context

    The command uses kubectl config set-context --current --namespace=<name>.
  2. Step 2: Match the command with the namespace dev

    Replace <name> with dev exactly as in kubectl config set-context --current --namespace=dev.
  3. Final Answer:

    kubectl config set-context --current --namespace=dev -> Option B
  4. Quick Check:

    Correct syntax = kubectl config set-context --current --namespace=dev [OK]
Hint: Use 'kubectl config set-context --current --namespace=' [OK]
Common Mistakes:
  • Using non-existent commands like 'kubectl switch namespace'
  • Omitting '--current' flag
  • Using wrong command verbs like 'change' or 'set' alone
3. What will be the output of the command kubectl config view --minify | grep namespace after running kubectl config set-context --current --namespace=testing?
medium
A. namespace: default
B. Error: namespace not found
C. No output
D. namespace: testing

Solution

  1. Step 1: Understand what kubectl config set-context --current --namespace=testing does

    This command sets the current context's namespace to testing.
  2. Step 2: Understand the kubectl config view --minify | grep namespace command

    This shows the current context's namespace setting, which should now be testing.
  3. Final Answer:

    namespace: testing -> Option D
  4. Quick Check:

    Namespace after set-context = testing [OK]
Hint: Check current namespace with 'kubectl config view --minify' [OK]
Common Mistakes:
  • Expecting default namespace after switching
  • Thinking command shows all namespaces
  • Assuming error if namespace does not exist yet
4. You ran kubectl config set-context --current --namespace=prod but your commands still affect the default namespace. What is the likely problem?
medium
A. You did not reload your shell or terminal
B. You used the wrong command to switch namespace
C. Your current context is not set correctly
D. The namespace prod does not exist

Solution

  1. Step 1: Check if the current context is set correctly

    If the current context is not the one you expect, switching namespace won't affect your commands.
  2. Step 2: Understand that namespace switching affects only the current context

    Switching namespace changes the namespace in the current context, so if the context is wrong, commands still use default namespace.
  3. Final Answer:

    Your current context is not set correctly -> Option C
  4. Quick Check:

    Namespace switch affects current context only [OK]
Hint: Verify current context with 'kubectl config current-context' [OK]
Common Mistakes:
  • Assuming shell reload is needed
  • Thinking namespace must exist before switching
  • Using wrong command syntax
5. You want to run multiple commands in the staging namespace without typing --namespace=staging each time. Which sequence of commands correctly sets this up and verifies it?
hard
A. kubectl config set-context --current --namespace=staging kubectl get pods kubectl config view --minify | grep namespace
B. kubectl namespace staging kubectl get pods kubectl config current-context
C. kubectl switch namespace staging kubectl get pods kubectl config get-contexts
D. kubectl set namespace staging kubectl get pods kubectl config get-namespaces

Solution

  1. Step 1: Set the current context's namespace to staging

    Use kubectl config set-context --current --namespace=staging to switch namespace context.
  2. Step 2: Verify by listing pods and checking namespace in current context

    Running kubectl get pods uses the new namespace by default. Checking with kubectl config view --minify | grep namespace confirms the namespace is set.
  3. Final Answer:

    kubectl config set-context --current --namespace=staging kubectl get pods kubectl config view --minify | grep namespace -> Option A
  4. Quick Check:

    Set context namespace then verify with config view [OK]
Hint: Set namespace once with config set-context, then run commands [OK]
Common Mistakes:
  • Using non-existent commands like 'kubectl switch namespace'
  • Not verifying namespace change
  • Confusing context commands with namespace commands
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard