Sub topic 5
Changing your default namespace
This chapter assumes you can run kubectl commands, read YAML, and know what a Pod and a Service are. It also assumes you have created a namespace such as team-a, as shown in the previous chapter.
Why the default matters
Every kubectl command that touches a namespaced resource needs a namespace. If you don't pass --namespace (or -n), kubectl uses the namespace from your current context. When the context names none, kubectl uses default.
This causes a common mistake. You work in team-a all day, forget the flag once, and kubectl reports "No resources found in default namespace." Worse, you might create or delete objects in the wrong place. Setting a default namespace for your context removes the need to type the flag each time.
What a context is
A kubectl context is a named bundle of three values stored in your kubeconfig file (by default ~/.kube/config):
- a cluster to talk to
- a user (credentials) to authenticate as
- an optional namespace
The namespace is the only part this chapter changes. The cluster and user stay as they are.
kubectl config get-contextsThe output has a NAMESPACE column. An empty value means the context has no default, so kubectl falls back to default.
Setting the namespace
Use set-context with --current to change the active context:
kubectl config set-context --current --namespace=team-a
kubectl config view --minify --output 'jsonpath={..namespace}'The first command writes team-a into the current context in your kubeconfig. The second prints the namespace now in effect. --minify limits the output to the current context, and the JSONPath expression picks out the namespace field. If it prints nothing, no namespace is set.
Now these two commands are equivalent:
kubectl get pods
kubectl get pods --namespace team-aThe change persists in the kubeconfig file. It survives new terminal sessions and affects every shell that uses that file.
Overriding the default
An explicit flag always wins over the context. To look at another namespace once, without changing your setting:
kubectl get pods --namespace kube-system
kubectl get pods --all-namespacesThe second command ignores the default and lists Pods everywhere.
Common errors
- Wrong cluster, right namespace. A namespace name exists per cluster. If
team-ais set but you switched to a different cluster context, you may be in ateam-ayou didn't intend. Checkkubectl config current-contextbefore destructive commands. - A new context has no namespace. Switching with
kubectl config use-contextselects a context with its own stored namespace. Each context remembers its own value, so a namespace you set on one context does not follow you to another. - A typo is accepted.
set-contextdoes not check that the namespace exists. If you mistypeteam-aa, later commands return empty results or "namespace not found" errors for creates. Confirm withkubectl get namespace team-a. - Manifests with a fixed namespace. If a YAML file sets
metadata.namespace, that value is used and conflicts with a different--namespaceflag cause an error. A manifest without it is created in your default namespace.
Resetting
To go back to default, set the namespace explicitly:
kubectl config set-context --current --namespace=defaultExercise
You ran kubectl config set-context --current --namespace=team-a. A teammate's script runs kubectl get pods --namespace team-b from your machine. Which namespace does the script query, and does your context's default change afterward?
Show answer
It queries team-b, because an explicit --namespace flag overrides the context. The context default stays team-a, since the flag affects only that one command and does not write to the kubeconfig.
Exercise
After setting the namespace on your current context, you switch to another context with kubectl config use-context staging and run kubectl get pods. Why might it not list Pods from team-a?
Show answer
The namespace is stored per context. The staging context has its own namespace value (possibly empty, meaning default), so the setting you made on the previous context does not apply.