Sub topic 1
What a namespace is and why you use one
Prerequisites
This chapter assumes you know the basic Kubernetes objects (Pods, Deployments, and Services). It also assumes you can run kubectl commands and read simple YAML manifests. You need access to a cluster where you can create namespaces, such as a local kind or minikube cluster.
The working model
A namespace is a named scope inside a single cluster. Most objects, such as Pods, Services, Deployments, and Secrets, live in exactly one namespace. Within a namespace, each object name must be unique per resource type. Across namespaces, the same name can be reused.
This means two teams can each run a Deployment called web in one cluster. They are separate objects because team-a/web and team-b/web have different namespaces.
Namespaces exist for three practical reasons:
- Name separation. Teams or environments do not collide on names.
- Policy attachment. Quotas, RBAC rules, network rules, and Pod security settings can be applied to one namespace at a time. Later chapters cover each of these.
- Organization. You can list, watch, or delete everything in one scope with a single
--namespaceflag.
A namespace is not a separate cluster. All namespaces share the same nodes, the same control plane, and the same kernel on each node. By default, a namespace does not block network traffic or limit resource use. Those protections only appear when you add the matching objects.
Creating and using a namespace
Create a namespace, list all namespaces, and then list Pods in one namespace or in all of them:
kubectl create namespace team-a
kubectl get namespaces
kubectl get pods --namespace team-a
kubectl get pods --all-namespacesIf you omit --namespace, kubectl uses the namespace from your current context. That is default unless you changed it.
You can also declare a namespace in YAML. This is the better choice when the namespace should be tracked in version control:
apiVersion: v1
kind: Namespace
metadata:
name: team-a
labels:
team: a
pod-security.kubernetes.io/enforce: baselineThe labels here are ordinary metadata, except the pod-security.kubernetes.io/enforce label. Kubernetes reads that label to apply a Pod security standard. Labels on namespaces are covered in a later chapter.
Common errors
- Forgetting the namespace flag. Running
kubectl get podsshows only the current namespace. If your Pods seem to be missing, they are probably in another namespace. Add--namespaceor--all-namespacesto check. - Applying a manifest before its namespace exists. If a manifest sets
metadata.namespace: team-aand that namespace has not been created, the apply fails with a "namespaces not found" error. Create the namespace first, or put it earlier in the same file or directory. - Assuming isolation. Putting workloads in separate namespaces does not stop them from talking to each other. Without a NetworkPolicy, any Pod can reach any other Pod.
- Mismatched namespaces. If a manifest sets
metadata.namespaceand you pass a different--namespaceon the command line, kubectl reports a conflict instead of choosing one.
Exercise
Two teams both create a Deployment named web. One uses namespace team-a and the other uses team-b. Does this cause a name conflict, and does it stop the Pods from reaching each other over the network?
Show answer
There is no name conflict, because object names only need to be unique within a namespace for each resource type. The namespaces do not stop network traffic, though. Without a NetworkPolicy, Pods in team-a can still reach Pods in team-b.