Sub topic 3
Which resources are namespaced and which are not
This chapter assumes you know Pods, Deployments, and Services, can run kubectl, and can read simple YAML. Every Kubernetes object has a scope. Knowing the scope tells you where to create an object, how to look it up, and why some commands ignore the namespace you pass.
Two scopes
A resource type is either namespaced or cluster-scoped.
- A namespaced resource lives inside exactly one namespace. Its name must be unique only within that namespace. Two namespaces can each hold a Pod called
web. - A cluster-scoped resource belongs to the whole cluster. Its name must be unique across the cluster, and it has no namespace field.
Pods, Deployments, Services, ConfigMaps, Secrets, PersistentVolumeClaims, Roles, and RoleBindings are namespaced. Nodes, PersistentVolumes, StorageClasses, ClusterRoles, ClusterRoleBindings, and the Namespace objects themselves are cluster-scoped.
A Namespace is not inside a namespace. It is the container, so it sits at cluster level.
Ask the API server
Do not memorize the lists. The API server knows which types are namespaced, and kubectl api-resources reports it.
kubectl api-resources --namespaced=true
kubectl api-resources --namespaced=falseThe first command lists types such as pods, services, and roles. The second lists types such as nodes, persistentvolumes, and namespaces. Custom resources installed by a CustomResourceDefinition (CRD) appear here too. The CRD's author chooses whether each one is namespaced or cluster-scoped.
How the scope affects commands
For namespaced resources, kubectl uses the namespace from your context, or from --namespace. If you give neither, it uses default.
kubectl get pods --namespace team-a
kubectl get pods --all-namespacesFor cluster-scoped resources, the namespace setting has no effect. These two commands return the same result:
kubectl get nodes
kubectl get nodes --namespace team-aA manifest for a namespaced object can carry metadata.namespace. If it does not, kubectl apply uses your current namespace. If the manifest's namespace differs from the --namespace flag, kubectl reports an error instead of choosing one.
Common errors
- Looking in the wrong namespace.
kubectl get podsreturns nothing, but the Pods exist in another namespace. Add--namespaceor--all-namespaces. - Applying to the wrong namespace. A manifest with no
metadata.namespacelands in whatever namespace your context points to. Check the context before you apply. - Referencing across namespaces. A Pod can mount only a Secret, ConfigMap, or PersistentVolumeClaim from its own namespace. Copy the object into each namespace that needs it.
- Binding the wrong kind of role. A Role and RoleBinding apply to one namespace. A ClusterRole is cluster-scoped, but a RoleBinding can grant it inside a single namespace.
- Expecting a PersistentVolume to be per-team. A PersistentVolumeClaim is namespaced, but the PersistentVolume it binds to is not.
Summary
Check scope with kubectl api-resources before you assume a namespace flag will do anything. If a type is namespaced, pass the right namespace. If it is cluster-scoped, ignore the flag.
Exercise
You run kubectl get storageclasses --namespace team-a. Will the output include only StorageClasses that belong to team-a? Explain, and name the command that confirms your answer.
Show answer
No. StorageClass is cluster-scoped, so it has no namespace and the --namespace flag has no effect. The command lists every StorageClass in the cluster. Run kubectl api-resources --namespaced=false to confirm that storageclasses appears in the cluster-scoped list.
Exercise
A Pod in team-a needs a Secret named db-creds that exists only in team-b. What are your options?
Show answer
A Pod can reference only Secrets in its own namespace, so it cannot use the team-b Secret. Create a copy of db-creds in team-a, or fetch the value from an external secret store.