Sub topic 4
Creating and inspecting namespaces with kubectl
This chapter assumes you can run basic kubectl commands, read simple YAML, and know what Pods, Deployments, and Services are. You will create namespaces, put objects in them, inspect them, and avoid the most common mistakes.
Creating a namespace
The quickest way is the imperative command. A namespace name must be a valid DNS label: lowercase letters, digits, and hyphens, at most 63 characters.
kubectl create namespace team-a
kubectl get namespacesget namespaces lists every namespace with its status and age. A healthy namespace shows Active.
You can also declare a namespace in YAML. This is better for version control, and it lets you attach labels at creation time.
apiVersion: v1
kind: Namespace
metadata:
name: team-a
labels:
team: a
pod-security.kubernetes.io/enforce: baselineApply it with kubectl apply -f namespace.yaml. The pod-security.kubernetes.io/enforce label is a Pod Security Admission setting, covered in a later chapter. Here it only shows that labels live on the namespace object.
Working inside a namespace
Most kubectl commands act on the default namespace unless you say otherwise. Use --namespace (or -n) to choose one.
kubectl get pods --namespace team-a
kubectl get pods --all-namespacesThe first command lists Pods only in team-a. The second lists Pods in every namespace and adds a NAMESPACE column. You can use -A as a short form of --all-namespaces.
The same flag works when you create objects:
kubectl create deployment web --image=nginx --namespace team-aYou can also set metadata.namespace inside a manifest. If the manifest names a namespace and you pass a different -n flag, kubectl reports a mismatch error instead of guessing.
Inspecting a namespace
kubectl describe shows labels, status, and any quota or limit range that applies.
kubectl describe namespace team-a
kubectl get namespace team-a --output yamlEvery namespace also carries the label kubernetes.io/metadata.name, which Kubernetes sets automatically to the namespace name. You will use it later to select namespaces in NetworkPolicy.
To see which resource types belong to a namespace and which do not, ask the API:
kubectl api-resources --namespaced=true
kubectl api-resources --namespaced=falseNamespaced types include Pods, Services, and Deployments. Cluster-scoped types include Nodes, PersistentVolumes, and Namespaces themselves.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `No resources found in default namespace.` | You forgot `-n`, so kubectl looked in `default`. | Add `-n team-a`. |
| `namespaces "team-a" not found` | The namespace was never created, or the name has a typo. | Run `kubectl get namespaces`. |
| `the namespace from the provided object does not match the namespace` | The manifest and the `-n` flag disagree. | Remove one, or make them match. |
| `Error from server (Forbidden)` | Your user lacks RBAC permission in that namespace. | Ask for a Role and RoleBinding. |
The most frequent mistake is acting on the wrong namespace. Deleting or applying something in default when you meant team-a is easy when you rely on the implicit default. The next chapter shows how to change that default safely.
Deleting a namespace
kubectl delete namespace team-aThis deletes the namespace and everything in it, including Deployments, Secrets, and PersistentVolumeClaims. There is no confirmation prompt. Check the name before you press Enter.
Exercise
You run kubectl get pods and see "No resources found in default namespace", yet you know a Deployment named web runs in team-a. What command lists its Pods, and what command would find Pods in every namespace at once?
Show answer
Run kubectl get pods --namespace team-a (or -n team-a) to list the Pods in team-a. Run kubectl get pods --all-namespaces (or -A) to list Pods in every namespace. The first attempt found nothing because kubectl searched only the default namespace.
Exercise
Why is kubectl get all -n team-a unreliable for checking that a namespace is empty before you delete it?
Show answer
The all category includes only a fixed set of common resource types. Secrets, ConfigMaps, Ingresses, PersistentVolumeClaims, and custom resources are not included, so the namespace can still hold objects. List the types you care about explicitly.