Sub topic 4
Namespaces: scoping your resources
This chapter assumes you can read a basic manifest (apiVersion, kind, metadata, spec) and that kubectl is configured for a cluster you can write to.
What a namespace does
A namespace is a named scope inside a cluster. Most resources live in exactly one namespace. A resource name only has to be unique within its namespace, so two teams can each have a Deployment called web.
Namespaces are also where you attach access rules (RBAC) and resource quotas. They do not isolate network traffic by themselves. Pods in different namespaces can still reach each other unless a NetworkPolicy blocks it.
A new cluster starts with these namespaces:
| Namespace | Purpose |
|---|---|
| `default` | Where resources go if you specify nothing |
| `kube-system` | Cluster components such as DNS |
| `kube-public` | Data readable by all clients |
| `kube-node-lease` | Node heartbeat objects |
Creating a namespace
A Namespace needs no spec. The name is enough:
apiVersion: v1
kind: Namespace
metadata:
name: demoApply it like any other manifest:
kubectl apply -f namespace.yaml
kubectl get namespace demoPlacing resources in a namespace
There are two ways to choose the namespace for a resource.
- Set
metadata.namespacein the manifest. This is the better choice, because the file then says where it belongs. - Pass
-n demoon the command line.
If you do neither, the resource goes to the namespace of your current kubectl context, which is default unless you changed it.
If the manifest names one namespace and -n names another, kubectl refuses with an error that the namespace from the provided object does not match. The namespace must also exist before you apply anything into it. Otherwise you get namespaces "demo" not found. In a multi-document file, put the Namespace first.
Cluster-scoped resources
Some kinds belong to the whole cluster, not to a namespace. Examples are Nodes, PersistentVolumes, StorageClasses, ClusterRoles, and Namespaces themselves. Setting metadata.namespace on these has no useful effect. To see which kinds are which:
kubectl api-resources --namespaced=true
kubectl api-resources --namespaced=falseWorking across namespaces
Most kubectl get commands look only at one namespace.
kubectl get pods -n demo
kubectl get pods -A
kubectl config set-context --current --namespace=demoThe last command changes the default namespace for your current context. It is convenient, but it is also an easy way to apply to the wrong place. Passing -n explicitly in scripts is safer.
Services get a DNS name that includes the namespace. A Service web in demo is reachable as:
webfrom Pods indemoweb.demofrom other namespacesweb.demo.svc.cluster.localas the full name
Common mistakes
- Applying a manifest that has no namespace and finding it later in
default. - Looking for a Pod with
kubectl get podsand seeing nothing, because it lives in another namespace. - Referencing a Service by its short name from a different namespace, which fails to resolve.
Exercise
You apply a Deployment manifest that has no metadata.namespace, without -n, and your context uses the default namespace. Where does the Deployment go, and how do you find Pods from another namespace called demo?
Show answer
The Deployment is created in default. To list Pods in demo, run kubectl get pods -n demo, or use -A to list Pods in all namespaces.