Namespaces: dividing a cluster
Separate spaces for teams and apps.
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: b4e662d42cce710e95b74ceccf7dd9bcefd1b9aec3d59e92ab688176774a6827Two 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
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
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:
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:
# team-b may run at most 2 Pods, and together they may request at most# half a CPU and 256 MiB of memory.apiVersion: v1kind: ResourceQuotametadata:name: computenamespace: team-bspec:hard:pods: "2"requests.cpu: 500mrequests.memory: 256Mi
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:
# Requests and limits for any container in team-b that does not set its own.apiVersion: v1kind: LimitRangemetadata:name: defaultsnamespace: team-bspec:limits:- type: ContainerdefaultRequest:cpu: 100mmemory: 64Midefault:cpu: 200mmemory: 128Mi
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:
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:
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.
kubectl get deployment api and get NotFound, but a teammate swears it exists. What do you check first?-n, kubectl only looks in your current namespace, as the NotFound for web showed.deploy is only a short form.requests.cpu. A new Pod without any requests is refused. What is the usual fix?kubectl run worked and got 100m CPU and 64Mi memory.must specify requests.cpu, not that the Pod count was used up.team-a got the welcome page from web.team-b.