Lesson 5 of 11 · 9 min · Running workloads well
Namespaces, resource requests and limits, and quotas
What you will learn
- What namespaces are and are not
- Cross-namespace access and DNS names
- Requests and limits for CPU and memory, and what happens when exceeded
- QoS classes
- ResourceQuota and LimitRange to keep one team from consuming everything
In simple words
Many teams share one cluster, like many families sharing one apartment building. Namespaces are the separate flats. Requests and limits say how much electricity each flat is promised and the most it may draw, so that one noisy neighbour can't leave everyone else in the dark.
Words to know
- Namespace: a named section of a cluster that keeps one team's or project's objects together.
- Request: the CPU and memory a container is promised. Kubernetes uses it to choose a machine.
- Limit: the most a container may use. Going over the memory limit gets it stopped.
- Quota: a cap on how much one namespace may use in total.
Namespaces
A namespace is a named partition of a cluster. Most objects (Pods, Deployments, Services, ConfigMaps, Secrets) live in exactly one namespace; names must be unique within a namespace, not across the cluster. Typical uses:
- one namespace per environment (
dev,staging,prod) in a small cluster (large organisations prefer separate clusters for production), - one per team or application (
team-a,shop), - system components in their own (
kube-system).
A fresh cluster has default, kube-system, kube-public and kube-node-lease:
kubectl create namespace team-a
kubectl get ns
default kube-node-lease kube-public kube-system team-a team-b
Use -n team-a on commands, -A for all namespaces, or set a default for your context: kubectl config set-context --current --namespace=team-a. Forgetting the namespace is the number one reason for "my Deployment has disappeared".
Namespaces are for organisation, access control and resource control. They are not a hard security boundary on their own: by default Pods in different namespaces can talk to each other (network policies change that), and they share nodes. Some objects are cluster-wide (Nodes, PersistentVolumes, StorageClasses, Namespaces themselves, ClusterRoles).
With RBAC you grant a team rights only inside its namespace (Role + RoleBinding), and with quotas you cap what it can use.
Talking across namespaces
A Service name resolves only to a Service in the same namespace unless you qualify it. From a Pod in team-b we tried to reach the Service api that lives in team-a:
kubectl -n team-b exec c2 -- nslookup api
# ** server can't find api.team-b.svc.cluster.local: NXDOMAIN
kubectl -n team-b exec c2 -- nslookup api.team-a.svc.cluster.local
# Name: api.team-a.svc.cluster.local Address: 10.43.74.206
The short name failed because the Pod's DNS search list starts with its own namespace (team-b.svc.cluster.local). The full form <service>.<namespace>.svc.cluster.local (or api.team-a) works.
Deleting a namespace deletes everything in it: a very powerful command. Double-check the name.
Requests and limits
For each container you can declare:
resources:
requests:
cpu: 250m # a quarter of a core
memory: 128Mi
limits:
cpu: "1"
memory: 256Mi
CPU is measured in cores (1 = one core, 250m = 250 millicores); memory in bytes (128Mi, 1Gi).
- Requests are what the container is guaranteed, and what the scheduler uses: a Pod is placed only on a node with enough unreserved capacity for the sum of its requests. They also feed the autoscaler's utilisation maths.
- Limits are the maximum it may use, enforced by the kernel (cgroups):
- CPU over limit → throttled (slowed down), never killed.
- Memory over limit → the container is killed (OOMKilled, exit code 137), because memory cannot be taken back. We reproduced this in the troubleshooting lessons.
The consequences of not setting them:
- No requests: the scheduler thinks the Pod needs nothing and may overload a node; autoscaling by CPU utilisation cannot work.
- No memory limit: one leaking Pod can consume the node's memory, so the kernel kills other Pods.
- Limits far above requests: nodes are overcommitted; under pressure Pods get evicted.
- Tiny CPU limits cause throttling that appears as mysterious latency. Many teams set CPU requests but leave CPU limits unset (or generous), and set memory requests equal to limits.
How to choose: measure real usage (metrics server, Prometheus, kubectl top pod), set requests near typical use and memory limits with headroom above peak; revisit regularly (Vertical Pod Autoscaler can recommend).
QoS classes
Kubernetes assigns each Pod a Quality of Service class from its requests and limits. When a node runs out of memory it evicts/kills in this order: BestEffort first, then Burstable, and Guaranteed last. We created three Pods and read their classes:
| Pod resources | QoS class |
|---|---|
requests = limits for CPU and memory in every container (cpu 100m/100m, memory 64Mi/64Mi) |
Guaranteed |
| some requests or limits set, but not equal | Burstable |
| nothing set | BestEffort |
qos-guaranteed -> Guaranteed
qos-burstable -> Burstable
qos-besteffort -> BestEffort
Run important workloads as Guaranteed or Burstable with proper requests; keep BestEffort for disposable jobs.
ResourceQuota: cap a namespace
A ResourceQuota limits the total resources and object counts in a namespace. We created one for team-b:
apiVersion: v1
kind: ResourceQuota
metadata: {name: small, namespace: team-b}
spec:
hard:
pods: "2"
requests.cpu: 500m
requests.memory: 256Mi
Two Pods were created; the third was refused immediately with an explanatory error, and so was a Pod asking for a whole CPU:
Error from server (Forbidden): pods "q3" is forbidden: exceeded quota: small, requested: pods=1, used: pods=2, limited: pods=2
Error from server (Forbidden): pods "q4" is forbidden: exceeded quota: small, requested: pods=1,requests.cpu=1, used: pods=2,requests.cpu=200m, limited: pods=2,requests.cpu=500m
kubectl describe quota small -n team-b shows used versus hard values. Note that once a quota covers CPU or memory, every Pod in the namespace must declare requests/limits for those resources, or creation is rejected; a LimitRange supplies defaults:
apiVersion: v1
kind: LimitRange
metadata: {name: defaults, namespace: team-b}
spec:
limits:
- type: Container
default: {cpu: 500m, memory: 256Mi} # limits when none given
defaultRequest: {cpu: 100m, memory: 64Mi} # requests when none given
max: {memory: 1Gi} # no container may ask for more
Quotas can also cap counts of Services, PersistentVolumeClaims, LoadBalancers and storage. They are the main tool for fair sharing in multi-team clusters and for stopping a mistake (or a runaway CI job) from consuming the budget.
Common mistakes
- Deploying everything in
default. - Assuming namespaces isolate network traffic or security.
- No requests/limits, or
requestsfar below real use so nodes are overcommitted. - Memory limit below the application's real peak → OOMKilled loops.
- Tiny CPU limits → throttling and latency.
- Deleting a namespace carelessly.
- Forgetting that a quota demands requests on all Pods.
Try it yourself
- Create two namespaces, deploy a Service in one and reach it from the other by its full DNS name.
- Create Pods with different requests/limits and read their
qosClassfromkubectl get pod -o yaml. - Apply a ResourceQuota, exceed it, and read the error. Add a LimitRange with defaults and see what changes.
- Use
kubectl top pods(needs metrics server) to compare real use with your requests.
Check yourself
What is the difference between a request and a limit?
A request is the amount reserved and used for scheduling; a limit is the maximum the container may consume, enforced at run time (CPU throttled, memory OOM-killed).
What happens when a container exceeds its memory limit?
The kernel kills it (OOMKilled, exit 137) and Kubernetes restarts it according to the restart policy.
Which Pods are evicted first when a node is short of memory?
BestEffort Pods, then Burstable ones using more than their requests; Guaranteed Pods last.
Are namespaces a security boundary?
Not by themselves. They scope names, RBAC and quotas, but by default network traffic is allowed between namespaces and workloads share nodes; add network policies, RBAC and Pod security controls.
Interview questions
How would you stop one team from using all the cluster's resources?
A namespace per team with ResourceQuotas for CPU, memory and object counts, LimitRanges for defaults, RBAC limiting what they can create, and monitoring/alerts on quota usage.
Why are CPU limits sometimes considered harmful?
CPU limits throttle containers even when the node has idle CPU, causing latency spikes. Many teams set CPU requests (for scheduling) but no CPU limit, while keeping memory limits.
A Pod is Pending after you added a ResourceQuota. Why?
Either the quota is exhausted (the API rejects creation with a quota error) or the Pod has no requests/limits where the quota requires them. Check events and kubectl describe quota.
Summary
- Namespaces partition names, access and quotas, but are not a network or security boundary on their own.
- Requests drive scheduling; limits are enforced: CPU throttles, memory kills (137). Requests and limits decide the QoS class.
- ResourceQuota caps a namespace; LimitRange gives defaults; reach other namespaces with
service.namespace.
Going deeper
- Namespaces: usage and DNS.
- Resource management for Pods and containers: requests, limits and units.
- Pod Quality of Service classes: classification and eviction order.
- Resource quotas and limit ranges: namespace-level controls.
See how much of this you can explain
A free mock interview asks you new questions on Kubernetes for DevOps Engineer and scores every answer, with what to fix.