Configuration, secrets, storage and access · 5 MIN READ

Namespaces, ResourceQuotas and LimitRanges: dividing one cluster between teams

Use namespaces to scope names, access and policy, tell which objects are cluster-wide, cap a team's consumption with ResourceQuota and LimitRange, and delete a namespace knowing what goes with it.

One cluster usually serves several teams, services and environments, and all of them want a Deployment called web. Namespaces give each group its own space for names, permissions and policies. They are not a security boundary on their own and some objects ignore them entirely, so it pays to know exactly what they divide.

kubernetes · A MAP OF THE IDEA

  1. Namespace per team or env
  2. RoleBinding for the team
  3. Quota plus LimitRange
  4. Guard namespace deletion

Useful when

  • Teams reuse names without colliding.
  • RBAC, quotas and policies attach at one boundary.

Watch out

  • Namespaces do not isolate network traffic by default.
  • Deleting a namespace deletes every object in it.

Implementation & operation cost: A namespace template per team, with RBAC, quota, LimitRange and policies, kept in Git and applied consistently.

01

What a namespace scopes

A namespaced object's name only has to be unique within its namespace and kind, so team-a/web and team-b/web are different Deployments. Roles and RoleBindings grant access within one namespace, NetworkPolicies select pods in it, ResourceQuotas and LimitRanges apply to it, and Services get DNS names that include it, such as web.team-b.svc.cluster.local. Pods can reach Services in other namespaces by those names, because namespaces do not isolate the network unless NetworkPolicies say so. kubectl works in the namespace of your current context, default unless you changed it; -n picks another and -A lists across all of them. A new cluster has default, kube-system, kube-public and kube-node-lease, and kind adds local-path-storage. Keep your workloads out of default and kube-system.

02

What is not namespaced

Some kinds describe the cluster itself and have no namespace: Nodes, PersistentVolumes, StorageClasses, IngressClasses, Namespaces, ClusterRoles, ClusterRoleBindings, PriorityClasses and CustomResourceDefinitions. kubectl ignores -n for them, and access to them needs a ClusterRole granted through a ClusterRoleBinding. That is why a PersistentVolumeClaim belongs to a namespace while the PV it binds does not, and why a team allowed to create StorageClasses can affect every other team. kubectl api-resources prints every kind with a NAMESPACED column, and kubectl api-resources --namespaced=false on a real cluster lists only the cluster-scoped ones. When you design per-team access, list these kinds explicitly and decide who may change each of them.

03

ResourceQuota: a budget per namespace

A ResourceQuota caps what one namespace may consume in total: the sums of CPU and memory requests and limits, requested storage, and object counts such as pods, services or persistentvolumeclaims. The quota admission plugin checks every create and rejects a pod that would exceed it with Forbidden: exceeded quota. Once a quota tracks requests.cpu or limits.memory, every new pod must declare those values or it is rejected too. Because a Deployment's pods are created by its ReplicaSet, the rejection appears as FailedCreate events on the ReplicaSet while the Deployment itself was accepted, a classic source of confusion. On a real cluster kubectl describe quota shows used against hard for each resource. In the GopherForge lab terminal ResourceQuota is enforced for pods, CPU and memory, as the admission plugin does.

yaml
apiVersion: v1
kind: ResourceQuota
metadata:
  name: team-a-quota
  namespace: team-a
spec:
  hard:
    requests.cpu: "4"
    requests.memory: 8Gi
    limits.cpu: "8"
    limits.memory: 16Gi
    pods: "50"
    persistentvolumeclaims: "10"
    requests.storage: 100Gi
Read the code

team-a may request 4 CPUs and 8Gi of memory in total, with twice that in limits, and run 50 pods with 10 claims totalling 100Gi. Only pods that have not finished count, so a completed Job frees its share. Size a quota from expected load plus headroom for rolling updates, which briefly run extra pods.

04

LimitRange: defaults and bounds per container

A LimitRange constrains individual containers, pods or claims in one namespace. For containers it sets default limits and defaultRequest values that admission fills in when a manifest omits them, min and max bounds that reject outliers, and optionally a maxLimitRequestRatio. Together with a quota it means a developer who forgets resources still gets a pod that fits the budget instead of a Forbidden error. Defaults apply only when a pod is created; changing a LimitRange never touches running pods. The GopherForge lab terminal fills in LimitRange defaults for new containers; practise its min and max bounds on a real cluster.

yaml
apiVersion: v1
kind: LimitRange
metadata:
  name: container-defaults
  namespace: team-a
spec:
  limits:
  - type: Container
    defaultRequest:
      cpu: 100m
      memory: 128Mi
    default:
      cpu: 500m
      memory: 256Mi
    max:
      cpu: "2"
      memory: 2Gi
Read the code

A container in team-a without resources gets requests of 100m CPU and 128Mi and limits of 500m and 256Mi. A container whose limit exceeds 2 CPUs or 2Gi is rejected at admission with a message that names the maximum.

05

Per-team namespaces, and deleting one

A common layout gives each team, or each team and environment, its own namespace with a RoleBinding for the team, a quota, a LimitRange and default-deny NetworkPolicies, all generated from one template so every team looks the same. Labels on namespaces, such as team=payments, let NetworkPolicies and admission policies select them. Deleting a namespace deletes everything in it: Deployments, Services, Secrets and PVCs, and through the reclaim policy possibly the data. The namespace shows Terminating while the namespace controller removes its contents, and it can hang there when an object carries a finalizer whose controller is gone or an aggregated API is unavailable; the namespace's status conditions name the cause. Grant delete on namespaces to very few people.

06

Try it in the lab terminal

Create two team namespaces and deploy a Deployment with the same name into both, then look across namespaces with -A and a label selector. Switch the default namespace of your kubeconfig context so that commands without -n go to team-a. Finally delete team-b without waiting, catch it in the Terminating state, advance the lab clock and confirm that its Deployment and pods went with it.

bash
kind create cluster
kubectl get namespaces
kubectl create namespace team-a
kubectl create namespace team-b
kubectl create deployment web --image=nginx -n team-a
kubectl create deployment web --image=nginx:1.27 --replicas=2 -n team-b
kubectl rollout status deploy/web -n team-b
kubectl get pods -A -l app=web
kubectl get pods
kubectl config set-context --current --namespace=team-a
kubectl get deploy
kubectl delete namespace team-b --wait=false
kubectl get ns team-b
cluster tick 30s
kubectl get ns team-b
kubectl get pods -A -l app=web
Read the code

get namespaces lists default, kube-node-lease, kube-public, kube-system and local-path-storage. Both namespaces accept a Deployment named web, and get pods -A -l app=web shows one pod in team-a and two in team-b in a NAMESPACE column, while a plain get pods answers No resources found in default namespace. After set-context, get deploy without -n lists team-a's web. --wait=false returns at once, so get ns team-b catches STATUS Terminating; 30 seconds later it reports namespaces "team-b" not found, and only team-a's pod is left.

TAKE THIS WITH YOU

Namespaces scope names, RBAC, policies and quotas, but not nodes, PVs, StorageClasses or cluster roles, and not the network by default. Give each team a namespace with a quota and a LimitRange, and treat deleting a namespace as deleting everything in it.

Check your understanding

You run kubectl delete namespace staging. What happens to the Deployments, Secrets and PersistentVolumeClaims inside it?

Go to the source

Progress stays in this browser. No account needed.