Kubernetes Namespace Resource Isolation
Required knowledge for the CKS certification.
Namespaces are Kubernetes's primary boundary for grouping and isolating workloads, but a namespace alone provides no enforcement of how much CPU or memory its pods can consume. Without explicit resource controls, a single misconfigured or compromised microservice can exhaust CPU and memory across the entire node, block other workloads from scheduling, and trigger a cluster-wide cascade. Three namespace-level controls address this: ResourceQuota caps aggregate resource consumption for the namespace, LimitRange injects default requests and limits into containers that omit them and enforces per-object maximums, and Pod Security Admission labels enforce a minimum security baseline on every pod admitted to the namespace.
These controls are complementary and are most effective when deployed together. ResourceQuota sets the namespace-wide ceiling; LimitRange ensures every container contributes a share to that ceiling even if the pod spec omits explicit values; and PSA labels ensure the pods themselves conform to a declared security posture.
1. ResourceQuota: Capping Namespace-Level Resource Consumption
Issue: A namespace with no ResourceQuota imposes no upper bound on the CPU, memory, or storage that its pods can request. A compromised or runaway workload can request unbounded resources, starving pods in other namespaces and triggering node pressure evictions.
Fix: Define a ResourceQuota for each team or application namespace to set hard limits on aggregate CPU, memory, and storage consumption.
When a namespace has a CPU or memory ResourceQuota, Kubernetes enforces that every new pod in that namespace declares requests or limits for those resources. Per the Kubernetes documentation: "If you enforce a resource quota in a namespace for either cpu or memory, you and other clients, must specify either requests or limits for that resource, for every new Pod you submit. If you don't, the control plane may reject admission for that Pod."
Violations are rejected at admission with HTTP status code 403 Forbidden. The error message describes the constraint that would have been violated.
Example: Team Namespace ResourceQuota
apiVersion: v1
kind: ResourceQuota
metadata:
name: team-a-quota
namespace: team-a
spec:
hard:
requests.cpu: "10"
requests.memory: 20Gi
limits.cpu: "20"
limits.memory: 40Gi
Important: Workload Objects Are Not Blocked
A Deployment, StatefulSet, or other workload controller is admitted even when the pods it manages cannot be created due to quota exhaustion. The quota enforcement applies to the resulting pod objects, not the controller. Per the Kubernetes documentation: "If you create a Deployment that tries to use more resources than are available, the creation of the Deployment (or other workload management object) succeeds, but the Deployment may not be able to get all of the Pods it manages to exist."
Always check pod events (kubectl describe pod) to distinguish a quota violation from a scheduling failure.
Protecting the Quota from Modification
Quota enforcement is only as strong as the RBAC that protects the ResourceQuota object itself. Kubernetes documentation recommends: "the cluster administrator should also restrict access to delete or update that ResourceQuota; for example, by defining a ValidatingAdmissionPolicy." Without this, any user with RBAC write access to the namespace can raise or delete the quota.
Verifying ResourceQuota Status
kubectl describe resourcequota team-a-quota -n team-a
The output shows each resource's hard limit and current usage. Namespaces with no quota at all can be identified with:
kubectl get namespace -o json | \
jq -r '.items[] | select(.metadata.name != "kube-system") | .metadata.name' | \
xargs -I{} sh -c 'kubectl get resourcequota -n {} 2>/dev/null | grep -q . || echo "{}: no quota"'
2. LimitRange: Injecting Defaults and Enforcing Per-Object Limits
Issue: A namespace with a CPU or memory ResourceQuota rejects any pod that omits explicit resource requests or limits. Without a LimitRange to supply defaults, every pod spec must declare requests and limits explicitly — and any that forgets them is blocked at admission, breaking deployments that pre-date the quota or were written without resource discipline.
Fix: Pair every ResourceQuota with a LimitRange that injects default requests and limits into containers that omit them, and enforces minimum and maximum bounds so no single container can claim an outsized share of the namespace quota.
Per the Kubernetes documentation: "Kubernetes constrains resource allocations to Pods in a particular namespace whenever there is at least one LimitRange object in that namespace." The LimitRange admission controller applies default values before the pod spec reaches ResourceQuota enforcement, so containers without explicit values are admitted rather than rejected.
Example: Namespace LimitRange
apiVersion: v1
kind: LimitRange
metadata:
name: team-a-defaults
namespace: team-a
spec:
limits:
- type: Container
defaultRequest:
cpu: 250m
memory: 256Mi
default:
cpu: 500m
memory: 512Mi
max:
cpu: "1"
memory: 2Gi
min:
cpu: 100m
memory: 128Mi
The default field sets the limit that is injected when a container does not declare one. The defaultRequest field sets the request. The max and min fields enforce absolute bounds — any container that declares a value outside this range is rejected with 403 Forbidden.
Key Operational Notes
Changes to LimitRange are not retroactive. Per the Kubernetes documentation: "LimitRange validations occur only at Pod admission stage, not on running Pods. If you add or modify a LimitRange, the Pods that already exist in that namespace continue unchanged." Existing pods must be re-created to pick up updated bounds.
Multiple LimitRanges in one namespace produce non-deterministic defaults. The documentation states: "If two or more LimitRange objects exist in the namespace, it is not deterministic which default value will be applied." Use exactly one LimitRange per namespace.
LimitRange does not validate its own consistency. A defaultRequest that exceeds the default limit will produce pods that cannot be scheduled. Test new LimitRange configurations with kubectl apply --dry-run=server before applying to a live namespace.
Verifying LimitRange Enforcement
kubectl describe limitrange team-a-defaults -n team-a
To confirm that a specific pod received injected defaults, inspect its spec after admission:
kubectl get pod <pod-name> -n team-a -o jsonpath='{.spec.containers[*].resources}'
3. Pod Security Admission Labels: Enforcing a Namespace-Level Security Posture
Issue: Without a Pod Security Admission (PSA) label, any pod spec is admitted to the namespace regardless of its security context — including pods requesting root access, hostNetwork, or unrestricted capabilities.
Fix: Apply PSA labels to each namespace to enforce a minimum security level on every admitted pod.
PSA labels follow the format pod-security.kubernetes.io/<MODE>: <LEVEL>. The three modes are:
| Mode | Effect |
|---|---|
enforce | Pod is rejected if it violates the specified level |
audit | Violation is recorded in the audit log; pod is admitted |
warn | User receives a warning; pod is admitted |
The three policy levels are privileged (no restrictions), baseline (prevents known escalations), and restricted (current hardening best practices). For full details on what each level restricts, see Pod Security Standards.
Important: Per the Kubernetes documentation, "enforce mode is not applied to workload resources, only to the resulting pod objects." A Deployment itself is not rejected; only the pods it creates are evaluated.
Gradual Rollout Approach
Switching an existing namespace directly to enforce: restricted often breaks workloads that were never written to the restricted profile. The recommended approach is to run a dry-run first, then apply audit and warn to get visibility before enforcing.
Step 1 — Dry-run to assess impact on existing pods:
kubectl label --dry-run=server --overwrite ns --all \
pod-security.kubernetes.io/enforce=baseline
This runs the admission evaluation without changing any policy. Violations are returned as warnings so you can triage before committing.
Step 2 — Apply audit and warn cluster-wide:
kubectl label --overwrite ns --all \
pod-security.kubernetes.io/audit=baseline \
pod-security.kubernetes.io/warn=baseline
Step 3 — Enforce on individual namespaces once compliant:
kubectl label --overwrite ns team-a \
pod-security.kubernetes.io/enforce=restricted \
pod-security.kubernetes.io/enforce-version=v1.37
Pinning the version (enforce-version) ties the policy to the rules that shipped with a specific Kubernetes minor release, so a cluster upgrade does not silently tighten the enforcement rules against existing workloads.
Auditing PSA Coverage
Namespaces with no enforce label can be identified with:
kubectl get namespaces --selector='!pod-security.kubernetes.io/enforce'
4. Combining All Three Controls
The following example shows a production namespace manifest with ResourceQuota, LimitRange, and PSA labels applied together:
apiVersion: v1
kind: Namespace
metadata:
name: team-a
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/enforce-version: v1.37
pod-security.kubernetes.io/warn: restricted
pod-security.kubernetes.io/warn-version: v1.37
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/audit-version: v1.37
---
apiVersion: v1
kind: ResourceQuota
metadata:
name: team-a-quota
namespace: team-a
spec:
hard:
requests.cpu: "10"
requests.memory: 20Gi
limits.cpu: "20"
limits.memory: 40Gi
---
apiVersion: v1
kind: LimitRange
metadata:
name: team-a-defaults
namespace: team-a
spec:
limits:
- type: Container
defaultRequest:
cpu: 250m
memory: 256Mi
default:
cpu: 500m
memory: 512Mi
max:
cpu: "1"
memory: 2Gi
min:
cpu: 100m
memory: 128Mi
This configuration ensures that:
- No pod is admitted unless it conforms to the
restrictedPSS level. - No container runs without explicit (or injected) CPU and memory requests and limits.
- The namespace's total CPU and memory consumption is bounded at the values set in the ResourceQuota.
References
This article is based on information from the following official sources:
- Resource Quotas - Kubernetes Documentation
- Limit Ranges - Kubernetes Documentation
- Pod Security Admission - Kubernetes Documentation
- Enforce Pod Security Standards with Namespace Labels - Kubernetes Documentation