Working with Contexts and Namespaces · CKA
The same command can act on different resources depending on its context and namespace.
Learn with progress and quizzesFirst, understand the concepts.
Context
A context combines a cluster, a user, and a default namespace. Make a habit of checking the current context before running commands.
Namespace
Namespaces scope resources within a cluster. Some resources, such as Nodes and PersistentVolumes, are cluster-scoped instead.
Why it matters
Development and production can both contain a Pod named web. Identify a target by cluster, namespace, resource kind, and name together.
Work through the reasoning
Make the target explicit
Read current-context, then inspect its cluster and user with get-contexts. Explicit -n flags make namespace choices visible without changing your default context settings.
Separate empty results from errors
No resources found means the selected scope has no matching objects. Forbidden indicates authorization. Connection or certificate errors are not fixed by choosing another namespace.
Learn by doing.
You need kubectl and access to your own practice Kubernetes cluster. Each lab is independent. Check your current context first, then run the commands in your own terminal.
Create the study-cka namespace and use the -n option to list Pods in it.
kubectl config current-context
kubectl config get-contexts
kubectl create namespace study-cka --dry-run=client -o yaml | kubectl apply -f -
kubectl get pods -n study-ckaHow to check your result
The namespace should exist, and the command should show a Pod list or “No resources found”. Compare the target with a command that omits -n.
Apply what you learned
In one context, team-a contains web but team-b returns an empty list. Name two things to check.
Reveal the reasoning after trying
Check whether these namespaces represent the intended environments and where web was actually created. Identically named objects in different namespaces are distinct. An empty list alone is not a reason to switch clusters.
Ready to check your understanding?
Which resource is not namespaced?
- Pod
- Deployment
- Node
- Service
Reveal the reasoning after trying
Node
Nodes are cluster-scoped. Pods, Deployments, and Services are namespaced resources.