Sub topic 8
Capping a namespace with ResourceQuota
This chapter assumes you can run kubectl, read YAML, and already know Pods, Deployments, and Services. You should also know that a Pod can declare CPU and memory requests and limits.
Why a namespace needs a cap
A namespace does not limit how much of the cluster its workloads can use. One team can create thousands of Pods and starve everyone else. A ResourceQuota is a namespaced object that sets a hard ceiling on the total resources and object counts in that namespace.
The API server enforces the quota at admission time. When you create an object that would push the namespace over a limit, the request is rejected right away.
Defining a quota
This quota caps Pod count and the combined CPU and memory in team-a.
apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-quota
namespace: team-a
spec:
hard:
pods: "20"
requests.cpu: "4"
requests.memory: 8Gi
limits.cpu: "8"
limits.memory: 16GiEach key under hard is a separate cap:
podslimits how many Pods can exist in the namespace.requests.cpuandrequests.memorylimit the sum of all container requests.limits.cpuandlimits.memorylimit the sum of all container limits.
The quota counts the sum across every Pod in the namespace. It does not restrict any single Pod. Per-container bounds belong to LimitRange, which the next chapter covers.
Apply the quota and check usage:
kubectl apply -f compute-quota.yaml
kubectl describe resourcequota compute-quota --namespace team-aThe describe output lists each resource with its Used and Hard values, so you can see how close the namespace is to its ceiling.
How admission works
A rejected request returns an error such as forbidden: exceeded quota: compute-quota. The message names the resource, the amount requested, what is already used, and the limit.
The requests and limits requirement
Once a quota covers requests.cpu, requests.memory, limits.cpu, or limits.memory, every new Pod in that namespace must specify that value for each container. A Pod that omits it is rejected. This is the most common quota error.
You have two ways to deal with it:
- Add requests and limits to every container spec.
- Create a LimitRange in the namespace, so containers that omit values receive defaults.
What else a quota can count
Compute is not the only thing you can cap. A quota can also limit object counts, such as services, secrets, configmaps, and persistentvolumeclaims. It can limit storage with requests.storage. Capping these counts stops a runaway script from filling the cluster's data store with objects.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Pod rejected with "must specify limits.cpu" | Quota covers a compute resource the Pod omits | Set values on the container, or add a LimitRange with defaults |
| Pod rejected with "exceeded quota" | Namespace total would pass the hard limit | Free resources, scale down, or raise the quota |
| Deployment shows fewer Pods than desired | The ReplicaSet's Pod creation is being rejected | Run `kubectl describe replicaset` and read the events |
| Raising the quota has no effect on the old Pod | Quotas apply only at creation time | Delete the Pod or let the controller recreate it |
Lowering a quota does not evict running Pods. It only blocks new ones, because the check happens at admission.
Exercise
A namespace has a quota with requests.memory set to 8Gi. A developer applies a Pod manifest whose container has no memory request, and the API server rejects it. Give two ways to fix this without removing the quota.
Show answer
Add a memory request, and a memory limit if the quota also covers limits.memory, to the container spec. Alternatively, create a LimitRange in the namespace with defaultRequest and default values, so the container gets those values at admission.
Exercise
You lower requests.cpu in a ResourceQuota from 8 to 2, but the namespace already uses 5 CPUs of requests. What happens to the running Pods, and to a new Pod that requests 1 CPU?
Show answer
The running Pods keep running, because quotas are checked only when objects are created. The new Pod is rejected, because the used total of 5 already exceeds the new hard limit of 2.