CertGrid CertGrid
Hands-on Lab·Certified Kubernetes Application Developer

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

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.

Runs on cka4001 because it MODIFIES THE KUBECONFIG. Doing that on a cluster somebody else is working on would silently redirect their commands into your namespace.
Server NameIP AddressOSRolesCPURAMHDD
CKA4001192.168.0.191Ubuntu 26.04 LTSSingle Node (control plane, untainted)2 Core4 GB50 GB

Before you start

  1. 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. --minify cuts 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.

  2. 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          1s

    Expected resultNothing in default, the Pod present in ckad-ns.

    Success conditionYou recognise the empty result that means wrong namespace, not missing object.

  3. Set it once

    Context "kubernetes-admin@kubernetes" modified.

    Now kubectl get pods with 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 -n on 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          1s

    Expected resultThe context is modified and bare get pods finds the Pod.

    Success conditionYour shell is pointed at the task's namespace for the rest of the task.

  4. 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 with custom-columns so the namespace is the first thing you read.

    --field-selector filters 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, but status.phase=Running and status.phase!=Running are 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     web
    bash 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     web

    Expected 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.

  5. 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" deleted

    Expected resultThe context reads default again.

    Success conditionYou leave the kubeconfig as you found it.

Troubleshooting

Official sources