Namespaces: dividing a cluster

Separate spaces for teams and apps.

Beginner20 min · lesson 19 of 21
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (3 files, 1 KB): namespaces.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/namespaces.tar.gz && tar -xzf namespaces.tar.gz, which creates ~/lab/namespaces/. SHA-256: b4e662d42cce710e95b74ceccf7dd9bcefd1b9aec3d59e92ab688176774a6827

Two teams share a cluster, and both want a Deployment called web. In a single shared space the second kubectl create fails, and one team's runaway test can take every node's spare capacity from the other.

Lesson 15 (DNS) used a namespace in passing: a named section of a cluster. Most objects, such as Pods, Deployments, Services and ConfigMaps, live in exactly one namespace, and names only have to be unique inside it. You can also put limits on a namespace. What a namespace does not separate matters just as much.

The namespaces you already have

ubuntu@secopslog-k8s:~/lab/namespaces · Kubernetes 1.37
$ kubectl get namespaces
NAME STATUS AGE default Active 32s kube-node-lease Active 32s kube-public Active 32s kube-system Active 32s local-path-storage Active 27s

Everything you created so far went into default. kube-system holds the cluster's own parts, local-path-storage holds kind's storage provisioner from lesson 18 (PersistentVolumes), and kube-public and kube-node-lease are small internal ones you rarely touch.

Same name, two namespaces

ubuntu@secopslog-k8s:~/lab/namespaces · Kubernetes 1.37
$ kubectl create namespace team-a && kubectl create namespace team-b
namespace/team-a created namespace/team-b created
$ kubectl create deployment web -n team-a --image=nginx:1.30.6-alpine && kubectl create deployment web -n team-b --image=nginx:1.30.6-alpine && kubectl rollout status deployment/web -n team-a && kubectl rollout status deployment/web -n team-b
deployment.apps/web created deployment.apps/web created Waiting for deployment "web" rollout to finish: 0 of 1 updated replicas are available... deployment "web" successfully rolled out deployment "web" successfully rolled out
$ kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE kube-system coredns-559f6c778d-2vl6m 0/1 Running 0 25s kube-system coredns-559f6c778d-9hf9b 1/1 Running 0 24s kube-system etcd-lab-control-plane 1/1 Running 0 33s kube-system kindnet-b5xjw 1/1 Running 0 22s kube-system kindnet-rbbzj 1/1 Running 0 25s kube-system kindnet-z2nmf 1/1 Running 0 22s kube-system kube-apiserver-lab-control-plane 1/1 Running 0 33s kube-system kube-controller-manager-lab-control-plane 1/1 Running 0 33s kube-system kube-proxy-886vh 1/1 Running 0 22s kube-system kube-proxy-l4lph 1/1 Running 0 22s kube-system kube-proxy-npk9b 1/1 Running 0 25s kube-system kube-scheduler-lab-control-plane 1/1 Running 0 33s local-path-storage local-path-provisioner-75f7fc7dc5-tkfh8 1/1 Running 0 25s team-a web-56d6d5bd78-8cgdz 1/1 Running 0 2s team-b web-56d6d5bd78-vfsnj 1/1 Running 0 2s
$ kubectl get deployment web
Error from server (NotFound): deployments.apps "web" not found

Two Deployments called web exist side by side, one in team-a and one in team-b. The -n flag picks the namespace for a command, and -A (all namespaces) adds a NAMESPACE column: the two web- Pods sit at the bottom, below the system Pods. The last command went wrong on purpose. Without -n, kubectl looks in default, finds no web there and says NotFound, although two exist.

Typing -n team-a every time gets old. Your kubeconfig context can carry a default namespace instead:

ubuntu@secopslog-k8s:~/lab/namespaces · Kubernetes 1.37
$ kubectl config set-context --current --namespace=team-a && kubectl get deployments
Context "kind-lab" modified. NAME READY UP-TO-DATE AVAILABLE AGE web 1/1 1 1 2s

After Context "kind-lab" modified., a plain kubectl get deployments looks in team-a. This setting lives only in your own kubeconfig file, not in the cluster. It is easy to forget, so check it when commands suddenly "lose" your objects.

A quota for a namespace

A ResourceQuota caps what a whole namespace may use. This one allows team-b two Pods, and a total of half a CPU and 256 MiB of memory in requests:

quota.yaml
# team-b may run at most 2 Pods, and together they may request at most
# half a CPU and 256 MiB of memory.
apiVersion: v1
kind: ResourceQuota
metadata:
name: compute
namespace: team-b
spec:
hard:
pods: "2"
requests.cpu: 500m
requests.memory: 256Mi
ubuntu@secopslog-k8s:~/lab/namespaces · Kubernetes 1.37
$ kubectl apply -f quota.yaml && kubectl describe resourcequota compute -n team-b
resourcequota/compute created Name: compute Namespace: team-b Resource Used Hard -------- ---- ---- pods 1 2 requests.cpu 0 500m requests.memory 0 256Mi
$ kubectl run extra -n team-b --image=nginx:1.30.6-alpine
Error from server (Forbidden): pods "extra" is forbidden: failed quota: compute: must specify requests.cpu for: extra; requests.memory for: extra

describe shows Used next to Hard. The existing web Pod already counts as one Pod. Then a new Pod was refused with Forbidden ... must specify requests.cpu. Once a quota limits CPU or memory requests, every new Pod in that namespace must state its requests, or the quota could not count it. The web Pod was already running when the quota arrived, so it stays.

Asking every team to add requests to every Pod is tedious. A LimitRange fills in defaults for containers that set none:

limits.yaml
# Requests and limits for any container in team-b that does not set its own.
apiVersion: v1
kind: LimitRange
metadata:
name: defaults
namespace: team-b
spec:
limits:
- type: Container
defaultRequest:
cpu: 100m
memory: 64Mi
default:
cpu: 200m
memory: 128Mi
ubuntu@secopslog-k8s:~/lab/namespaces · Kubernetes 1.37
$ kubectl apply -f limits.yaml && kubectl run extra -n team-b --image=nginx:1.30.6-alpine && kubectl get pod extra -n team-b -o jsonpath="{.spec.containers[0].resources}{\"\n\"}"
limitrange/defaults created pod/extra created {"limits":{"cpu":"200m","memory":"128Mi"},"requests":{"cpu":"100m","memory":"64Mi"}}
$ kubectl run extra2 -n team-b --image=nginx:1.30.6-alpine
Error from server (Forbidden): pods "extra2" is forbidden: exceeded quota: compute, requested: pods=1, used: pods=2, limited: pods=2
$ kubectl describe resourcequota compute -n team-b
Name: compute Namespace: team-b Resource Used Hard -------- ---- ---- pods 2 2 requests.cpu 100m 500m requests.memory 64Mi 256Mi

The same kubectl run now works. The Pod spec shows the requests and limits the LimitRange added, though the command never mentioned them. The next Pod was refused for a different reason: exceeded quota: compute, requested: pods=1, used: pods=2, limited: pods=2. The quota now reads 2 of 2 Pods, 100m CPU and 64Mi memory, which is exactly the one defaulted Pod. Note that the error comes back straight from kubectl run. When a Deployment hits a quota, the refusal shows up as an event on its ReplicaSet instead, and the Deployment simply has fewer Pods than it asked for.

What a namespace does not separate

A namespace is a way to name and limit things, not a wall. A Pod in team-a can call the Service in team-b by its name plus the namespace, as lesson 15 (DNS) showed:

ubuntu@secopslog-k8s:~/lab/namespaces · Kubernetes 1.37
$ kubectl expose deployment web -n team-b --port=80 && kubectl run client -n team-a --rm -i --restart=Never --image=busybox:1.38.0 -- sh -c "wget -qO- -T 5 http://web.team-b | grep \"<title>\""
service/web exposed All commands and output from this session will be recorded in container logs, including credentials and sensitive information passed through the command prompt. If you don't see a command prompt, try pressing enter. <title>Welcome to nginx!</title> pod "client" deleted from team-a namespace

The busybox Pod ran in team-a and got the nginx welcome page from web.team-b. By default every Pod can reach every other Pod and Service in the cluster. To block that, you need NetworkPolicies, which the security course covers. Namespaces also share the same nodes, so a Pod from one team can run next to a Pod from another. And some objects belong to no namespace at all:

ubuntu@secopslog-k8s:~/lab/namespaces · Kubernetes 1.37
$ kubectl api-resources --namespaced=false | head
NAME SHORTNAMES APIVERSION NAMESPACED KIND componentstatuses cs v1 false ComponentStatus namespaces ns v1 false Namespace nodes no v1 false Node persistentvolumes pv v1 false PersistentVolume mutatingadmissionpolicies admissionregistration.k8s.io/v1 false MutatingAdmissionPolicy mutatingadmissionpolicybindings admissionregistration.k8s.io/v1 false MutatingAdmissionPolicyBinding mutatingwebhookconfigurations admissionregistration.k8s.io/v1 false MutatingWebhookConfiguration validatingadmissionpolicies admissionregistration.k8s.io/v1 false ValidatingAdmissionPolicy validatingadmissionpolicybindings admissionregistration.k8s.io/v1 false ValidatingAdmissionPolicyBinding

This is the first page of the list (| head). namespaces, nodes and persistentvolumes are cluster-wide. That is why the PV in lesson 18 had no namespace, while its claim did. Anyone allowed to change cluster-scoped objects affects every namespace.

Hands-on

Give a namespace a hard cap, hit it, then delete the namespace with everything in it.

1. Create the namespace sandbox, a quota of one Pod, and one nginx Pod: kubectl create namespace sandbox && kubectl create quota small -n sandbox --hard=pods=1 && kubectl run one -n sandbox --image=nginx:1.30.6-alpine. kubectl create quota writes a ResourceQuota without a file.

2. Try a second Pod: kubectl run two -n sandbox --image=nginx:1.30.6-alpine.

3. Delete the whole namespace and look inside it: kubectl delete namespace sandbox && kubectl get pods -n sandbox.

4. Put your context back on default, delete the two team namespaces and list what is left: kubectl config set-context --current --namespace=default && kubectl delete namespace team-a team-b && kubectl get namespaces.

What you should see: step 1 prints namespace/sandbox created, resourcequota/small created and pod/one created. Step 2 is refused with exceeded quota: small, requested: pods=1, used: pods=1, limited: pods=1. In step 3 the Pod and the quota went with the namespace, so kubectl get pods -n sandbox says No resources found in sandbox namespace. Step 4 ends with only the five namespaces you started with.

Remember

Namespaces give names a scope and let you cap a team with a ResourceQuota, with a LimitRange to fill in requests nobody wrote. They do not stop network traffic or keep teams off each other's nodes, and deleting a namespace deletes everything in it.

Quick check
01You run kubectl get deployment api and get NotFound, but a teammate swears it exists. What do you check first?
Incorrect — A broken connection gives a connection error, not NotFound from the server.
Incorrect — Deleting a ReplicaSet never removes its Deployment; the Deployment makes a new one.
Correct — Without -n, kubectl only looks in your current namespace, as the NotFound for web showed.
Incorrect — Both names work. deploy is only a short form.
02A namespace has a ResourceQuota with requests.cpu. A new Pod without any requests is refused. What is the usual fix?
Correct — After the LimitRange, the same kubectl run worked and got 100m CPU and 64Mi memory.
Incorrect — Those Pods are not the problem. The new Pod was refused for having no requests.
Incorrect — The error said must specify requests.cpu, not that the Pod count was used up.
Incorrect — Pods cannot be moved between namespaces. You would have to create a new one.
03Team A and team B have separate namespaces. Can a Pod in team A's namespace call a Service in team B's?
Incorrect — The busybox Pod in team-a got the welcome page from web.team-b.
Incorrect — Quotas limit resources. They have nothing to do with traffic.
Incorrect — Cluster IPs work from any namespace, and the Pod used the Service name.
Correct — Namespaces do not filter traffic. NetworkPolicies do.

Related