InsightsPlatform Engineering
ResourceQuota and LimitRange: giving each namespace a budget that does not break deployments
How to cap what a team can consume in a shared Kubernetes cluster with ResourceQuota, why a quota on cpu or memory needs LimitRange defaults beside it, and the failures that appear when a Deployment is accepted but its pods are not.
Published November 21, 20266 min read
A shared cluster without namespace budgets works until one team does not follow the rules. A runaway autoscaler, a job that fans out to thousands of pods or a forgotten load test takes the capacity that every other team was counting on. Kubernetes has two objects for this, and they are meant to be used together: ResourceQuota caps a namespace as a whole, and LimitRange shapes the individual pods inside it.
What each object does
| ResourceQuota | LimitRange | |
|---|---|---|
| Scope | The sum over a namespace | One container, pod or claim |
| Can set | Totals for compute, storage and object counts | Minimums, maximums, request-to-limit ratios and defaults |
| Acts | At admission, rejecting a request that would exceed the total | At admission, filling in defaults first and then validating |
| Existing objects | Not affected | Not affected |
Both are enforced when an object is created or updated, and both answer a violation with 403 Forbidden and a message that names the constraint (ResourceQuota, LimitRange). Neither touches what is already running. If you add a quota to a namespace that is already over it, nothing is evicted; new pods are simply refused until usage falls.
A quota for compute and object counts
Start with one quota per team namespace that covers requests, limits and the objects most likely to run away:
apiVersion: v1
kind: ResourceQuota
metadata:
name: compute
namespace: checkout
spec:
hard:
requests.cpu: "20"
requests.memory: 40Gi
limits.memory: 60Gi
count/pods: "100"
count/services.loadbalancers: "2"
count/secrets: "200"requests.cpuandrequests.memorysum the requests of every pod in a non-terminal state. A plaincpuormemorykey means the same as therequests.form.count/<resource>caps objects. Core resources are writtencount/pods; resources from other API groups include the group, such ascount/deployments.appsorcount/widgets.example.comfor a custom resource.count/podscounts pods that are not in theFailedorSucceededphase, so finished Job pods do not use it up.- Object counts are worth having for
services.loadbalancers, because each one can mean a cloud resource that someone pays for.
Aggregated APIs that are not defined as CustomResourceDefinitions are not covered by object count quota, so check that before relying on it for an extension API.
Pick a number you can defend. A common approach is to take the team's current peak requests from Prometheus, add the growth you are willing to approve without a conversation, and review it each quarter.
The rule that breaks deployments
This is where most first rollouts go wrong. Once a namespace has a quota on cpu or memory, every new pod must specify a request or a limit for that resource. Otherwise the control plane may reject it. Existing manifests that never set resources start failing the next time they roll.
The failure is easy to miss, because the object you applied is accepted. A Deployment is created even when its pods cannot be, and the rejection appears on the ReplicaSet:
kubectl get deployment payments -n checkout
# READY 0/3, UP-TO-DATE 0
kubectl describe replicaset -n checkout -l app=payments
# Events show the pod creation refused, with the quota constraint namedThe same effect applies to any controller that creates pods on your behalf, so a stalled rollout with no pods is the signal to read the ReplicaSet or Job events rather than the Deployment's.
LimitRange supplies the defaults
A LimitRange fixes this by injecting default requests and limits into containers that do not set them, so teams do not have to edit every manifest at once:
apiVersion: v1
kind: LimitRange
metadata:
name: container-defaults
namespace: checkout
spec:
limits:
- type: Container
defaultRequest:
cpu: 100m
memory: 128Mi
default:
memory: 256Mi
max:
cpu: "4"
memory: 8Gi
min:
cpu: 10m
memory: 16MiThe admission order matters: defaults are applied first, then the pod is checked against min, max and any ratio. defaultRequest fills in requests, default fills in limits. A maxLimitRequestRatio entry caps how far a limit may sit above its request, which is a way to limit overcommit per container.
Three details cause most of the surprises:
- Defaults are not checked for consistency. A default limit below a request the pod set itself produces an invalid pod. In the documentation's example, a pod that requests 700m of CPU and sets no limit receives a default limit of 500m and is rejected with
must be less than or equal to cpu limit. Set both values, or choose defaults that sit comfortably below typical requests. - Keep one LimitRange per namespace. With two or more, it is not deterministic which default is applied.
- A LimitRange changes nothing that already exists. Running pods keep their old resources until they are recreated.
Budgets for scarce resources
Quota also works for resources you want to ration, such as GPUs. For extended resources, only the requests. prefix is allowed, because overcommit is not permitted for them:
apiVersion: v1
kind: ResourceQuota
metadata:
name: gpu
namespace: ml-research
spec:
hard:
requests.nvidia.com/gpu: "4"Scopes narrow which pods a quota measures. A quota with a scopeSelector only counts pods that match every scope listed. The PriorityClass scope lets you give a team ten high-priority pods and a much larger allowance of ordinary ones:
apiVersion: v1
kind: ResourceQuota
metadata:
name: pods-high
namespace: checkout
spec:
hard:
pods: "10"
scopeSelector:
matchExpressions:
- operator: In
scopeName: PriorityClass
values: ["high"]The other supported scopes are BestEffort, NotBestEffort, Terminating, NotTerminating, CrossNamespacePodAffinity and VolumeAttributesClass. Terminating and NotTerminating separate pods that have an active deadline, such as batch work, from long-running services.
Watching usage before it blocks someone
kubectl describe quota shows used against hard for every key:
Resource Used Hard
-------- ---- ----
count/pods 64 100
requests.cpu 14 20
requests.memory 31Gi 40GiA team that learns about its quota from a failed deploy at the wrong moment will ask for it to be removed. Export usage next to the limit, for example with kube-state-metrics' kube_resourcequota series, and alert at around 80% so the conversation happens a week before the failure.
A rollout order that avoids outages
- Add the LimitRange first. It changes only new pods, and it makes the next step safe.
- Create the quota with generous numbers, set from current usage plus headroom, and watch
describe quotafor a week. - Roll the workloads so existing pods pick up defaults, and fix any that are refused. Check ReplicaSet and Job events, not only Deployments.
- Lower the numbers to the budget you actually intend, and publish how a team asks for more.
- Protect the objects. A quota is only as good as the permissions around it. Restrict who can update or delete ResourceQuota and LimitRange objects, for example with a ValidatingAdmissionPolicy, so that a namespace admin cannot lift their own cap.
Quota also does not solve contention across the cluster. If the sum of all namespace quotas is more than the cluster can hold, the pods that arrive first get the capacity. Either keep the sum within capacity, or accept overcommit knowingly and rely on autoscaling to add nodes.
What changes
A namespace with a quota and defaults has a known upper bound on what it can take, every pod has a request the scheduler can use, and the first sign of a runaway workload is a refused pod in one team's namespace rather than a shortage in everyone's. The cost is a small amount of administration: numbers to set, review and explain, and a clear route for asking for more.