Sub topic 14
Organizing resources with namespaces
This chapter assumes you have a running kind or minikube cluster, a working kubectl context, and you can apply a Deployment manifest. Namespaces build on those skills.
What a namespace is
A namespace is a named partition inside one cluster. Most resources, such as Pods, Deployments and Services, belong to exactly one namespace. Names only have to be unique within a namespace, so two teams, or two copies of your app, can both own a Deployment called web.
A fresh cluster starts with these namespaces:
| Namespace | Purpose |
|---|---|
| default | Where resources go if you name no namespace |
| kube-system | Cluster components such as DNS and the proxy |
| kube-public | Data readable by all clients, rarely used |
| kube-node-lease | Node heartbeat objects |
Some resources are cluster-scoped and have no namespace. Nodes and Namespaces themselves are examples. Run kubectl api-resources --namespaced=false to list them.
Creating and using namespaces
kubectl create namespace staging
kubectl get namespaces
kubectl apply -f web.yaml --namespace staging
kubectl get pods --namespace staging
kubectl get pods --all-namespacesThe -n flag is short for --namespace, and -A is short for --all-namespaces. Without a flag, kubectl uses the namespace set in the current context, which is default unless you change it.
You can also declare the namespace in the manifest:
apiVersion: v1
kind: Namespace
metadata:
name: staging
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: staging
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27If a manifest sets metadata.namespace and you pass a different -n, kubectl refuses with a namespace mismatch error.
Changing the default namespace
Typing -n staging on every command gets tedious. Store the namespace in your current context instead:
kubectl config set-context --current --namespace=staging
kubectl config view --minify | grep namespaceThis edits your kubeconfig. Later commands without -n now act on staging. A common mistake is forgetting this setting and then wondering why kubectl get pods shows nothing, or deleting something in the wrong place. Check the context's namespace before destructive commands.
Namespaces and Services
Each Service gets a DNS name that includes its namespace: web.staging.svc.cluster.local. Inside the same namespace, the short name web works. From another namespace, use at least web.staging.
Limits of namespaces
Namespaces are not security boundaries by themselves. Without RBAC (role-based access control) and network policies, a Pod in one namespace can still reach Pods in another. They are a naming and organization tool, and a unit for quotas.
Deleting a namespace deletes everything inside it:
kubectl delete namespace stagingThat makes namespaces a convenient way to clean up an experiment in one command.
Exercise
You run kubectl get pods and see no Pods, but you know you applied a Deployment with -n staging. What is the likely cause, and what are two ways to see the Pods?
Show answer
The command is looking in the namespace of the current context, usually default, and your Pods are in staging. Run kubectl get pods -n staging, or use kubectl get pods -A. You can also run kubectl config set-context --current --namespace=staging so later commands default to staging.