Setting the Default Namespace in a Context
Most CKAD tasks name a namespace, and every one of them is scored in that namespace. Typing `-n` on every command is both slow and unreliable - the command you forget it on is the one that silently succeeds somewhere else. Setting it on the context once per task removes the whole class of mistake, and this shows the two commands that do it and the ones that look across everything.
Working at Exam Speed Guide 4 of 44 Beginner
- Kubernetes1.36.4
- Runtimecontainerd 2.2.6
- CNICalico v3.32.1
- TimeAbout 12 min
- Reviewed23 August 2026
Written against the versions above. `kubectl config set-context --current --namespace=X` edits your kubeconfig file. On a shared machine that affects every other shell using the same kubeconfig, which is why this guide runs on an idle cluster and sets the context back at the end.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| CKA4001 | 192.168.0.191 | Ubuntu 26.04 LTS | Single Node (control plane, untainted) | 2 Core | 4 GB | 50 GB |
Before you start
- A cluster and kubectl, and a kubeconfig you are allowed to edit.
- The session creates namespace
ckad-ns, sets it as the context default, and sets the context back todefaultbefore finishing.
-
Where kubectl thinks it is
Two commands tell you, and the second is the one to memorise:
kubectl config view --minify -o jsonpath='{..namespace}'An empty answer means
default.--minifycuts the kubeconfig down to the current context, which is why this works regardless of how many clusters are configured.On exam day the first thing worth doing in each task is checking this, because the task statement's namespace and your shell's namespace are two different things.
bash Example session kubectl config get-contextsCURRENT NAME CLUSTER AUTHINFO NAMESPACE* kubernetes-admin@kubernetes kubernetes kubernetes-admin defaultkubectl config view --minify -o jsonpath='{..namespace}'; echo "[empty means default]"default[empty means default]Expected resultOne context, marked current, with no namespace set.
Success conditionYou can answer "where am I" in one command.
-
What forgetting it looks like
Create a Pod in
ckad-ns, then look for it without saying where:No resources found in default namespace.That is the failure mode. Not an error - a perfectly successful command, returning the truth about the wrong place. Under time pressure the natural reading is "the Pod did not get created", and the next thing people do is create it again, in
default, where it scores nothing.The message does say
in default namespace, and reading that phrase carefully is worth a mark or two by itself.bash Example session kubectl create namespace ckad-nsnamespace/ckad-ns createdkubectl -n ckad-ns run web --image=nginx:alpine --restart=Neverpod/web createdkubectl get podsNo resources found in default namespace.kubectl get pods -n ckad-nsNAME READY STATUS RESTARTS AGEweb 0/1 ContainerCreating 0 1sExpected resultNothing in
default, the Pod present inckad-ns.Success conditionYou recognise the empty result that means wrong namespace, not missing object.
-
Set it once
Context "kubernetes-admin@kubernetes" modified.Now
kubectl get podswith no flags returns the Pod. Every subsequent command in that task -create,describe,logs,exec,delete- lands in the right place without you thinking about it.This is one command per task, replacing
-non perhaps forty commands. It is also strictly safer: the flag you forget is the mistake, and there is no flag to forget.bash Example session kubectl config set-context --current --namespace=ckad-nsContext "kubernetes-admin@kubernetes" modified.kubectl config view --minify -o jsonpath='{..namespace}'ckad-nskubectl get pods -n ckad-nsNAME READY STATUS RESTARTS AGEweb 0/1 ContainerCreating 0 1sExpected resultThe context is modified and bare
get podsfinds the Pod.Success conditionYour shell is pointed at the task's namespace for the rest of the task.
-
Looking across all of them
Two flags matter when the task does not name a namespace, or when you have lost something:
-A(or--all-namespaces) searches everywhere. Combine it withcustom-columnsso the namespace is the first thing you read.--field-selectorfilters server-side on a handful of built-in fields -status.phase,spec.nodeName,metadata.name. It is not a label selector and it does not accept arbitrary paths, butstatus.phase=Runningandstatus.phase!=Runningare both worth knowing.And for a task that has gone wrong, events sorted by time in the namespace you care about is usually the fastest read there is:
2026-08-23T02:57:54Z Scheduled web 2026-08-23T02:57:55Z Pulling webbash Example session kubectl get pods -A -o 'custom-columns=NS:.metadata.namespace,POD:.metadata.name' --no-headers | head -8calico-system calico-apiserver-6b55b5fbd7-f89xqcalico-system calico-apiserver-6b55b5fbd7-gwmh5calico-system calico-kube-controllers-85b567599d-m2x8fcalico-system calico-node-zdsv6calico-system calico-typha-5745db6759-l8xg5calico-system csi-node-driver-w94s7ckad-gen api-5d47c54ff7-2cbpmckad-gen api-5d47c54ff7-6dlhskubectl get pods -A --field-selector=status.phase=Running --no-headers | wc -l18kubectl get events -n ckad-ns --sort-by=.lastTimestamp -o 'custom-columns=TIME:.lastTimestamp,REASON:.reason,OBJ:.involvedObject.name' --no-headers | tail -52026-08-23T02:57:54Z Scheduled web2026-08-23T02:57:55Z Pulling webExpected resultA namespace-first listing, a count of running Pods, and a time-ordered event list.
Success conditionYou can find an object you have misplaced without guessing.
-
Put it back
default[back to default]Worth doing between tasks, and worth doing on any shared machine. A kubeconfig with a stale default namespace is a trap you set for yourself an hour earlier - and if anyone else uses the same kubeconfig, one you set for them.
bash Example session kubectl config set-context --current --namespace=defaultContext "kubernetes-admin@kubernetes" modified.kubectl config view --minify -o jsonpath='{..namespace}'; echo "[back to default]"default[back to default]kubectl delete namespace ckad-ns --wait=falsenamespace "ckad-ns" deletedExpected resultThe context reads
defaultagain.Success conditionYou leave the kubeconfig as you found it.
Troubleshooting
kubectl get podsreturns nothing and you are sure the Pod exists.Why: You are in a different namespace.
Fix:Read the message - it names the namespace it searched. Then
kubectl get pods -A | grep <name>.A task was completed correctly and scored zero.
Why: The objects were created in the wrong namespace.
Fix:Set the context namespace as the first action of every task.
--field-selectorrejects the field you want.Why: Only certain fields are indexed; arbitrary JSON paths are not supported.
Fix:Use labels and
-l, or filter client-side with-o jsonpath/custom-columns.