Requests, limits, quotas & LimitRanges
Set resource requests and limits and enforce namespace quotas.
By Invicly Tech, , in Kubernetes Administrator (CKA). About 9 minutes to read.
In short
Requests reserve CPU and memory for scheduling, and limits cap what a container may use. Exceeding a CPU limit causes throttling, while exceeding a memory limit gets the container OOM-killed. ResourceQuotas cap totals per namespace and LimitRanges set per-container defaults and bounds.
Explained simply
Every node has a fixed amount of CPU and memory. When many Pods share a node, Kubernetes needs to know how much each one needs, or one greedy app could starve the rest. You tell it with two numbers per container: a request and a limit.
The request is a reservation. The scheduler only places a Pod on a node that still has that much unreserved room. The limit is a ceiling. The container may use more than its request when the node is quiet, but never more than its limit.
Going over the limit hurts differently for CPU and memory. Too much CPU just makes the app slower, because it gets paused for tiny moments. Too much memory gets the container killed (OOMKilled) and restarted, because memory cannot be taken back gently.
Admins add two guard rails per namespace. A ResourceQuota is a budget for the whole team: for example, no more than 4 CPUs of requests and 20 Pods. A LimitRange fills in sensible default values when a developer forgets to set them.
Press Play on a diagram to walk through it one step at a time.
- Every container sets CPU and memory
- Requests equal limits
- Evicted last
- At least one request or limit
- Not Guaranteed
- Evicted after BestEffort
- No requests or limits at all
- Uses spare capacity
- Evicted first under pressure
- 1Admission
LimitRange fills defaults; quota checks budget
- 2Scheduling
Requests must fit node allocatable
- 3Container start
Limits become cgroup settings
- 4Running
CPU over limit is throttled
- 5Memory over limit
OOMKilled, exit code 137
- 6Node pressure
kubelet evicts by QoS and usage
In depth
Units and the classic mistakes
CPU is measured in cores: 1 equals 1000m (millicores), so 250m is a quarter of a core. Memory is measured in bytes with suffixes: Mi and Gi are powers of 1024, while M and G are powers of 1000. 128Mi is about 134 million bytes.
The most painful typo is a lowercase m on memory. memory: 128m means 0.128 bytes, not 128 megabytes, and the container cannot start. Another surprise: if you set only a limit, Kubernetes copies it into the request. That can make a Pod much harder to schedule than you expected.
- cpu: 0.5 and cpu: 500m are the same
- memory: 1Gi = 1024Mi
- Only limits set means requests equal limits
What the node really enforces
The scheduler compares requests with a node's allocatable resources, which is capacity minus space reserved for the system and the kubelet and a small eviction buffer. kubectl describe node shows both, plus an Allocated resources table with the sum of requests and limits on that node.
On the node, the kubelet and container runtime turn your numbers into Linux cgroup settings. The CPU limit becomes a CFS quota: the container may use its share of each 100 ms period and is then throttled until the next one. The memory limit becomes a hard cap; crossing it makes the kernel OOM killer stop the process, and the container status shows reason OOMKilled with exit code 137. When the whole node runs short of memory, the kubelet evicts Pods, starting with those using most beyond their requests.
- 1Capacity
Total RAM the node reports
- 2Reserved
kube-reserved and system-reserved
- 3Eviction threshold
Kept free, often 100Mi
- 4Allocatable
What Pod requests may add up to
- 5Pod requests
Sum must stay at or under allocatable
ResourceQuota in practice
A ResourceQuota caps the sum of resources in one namespace. Common keys are requests.cpu, requests.memory, limits.cpu, limits.memory, pods, services, persistentvolumeclaims, requests.storage and object counts like count/deployments.apps. Admission checks each new object against the remaining budget and rejects it with an exceeded quota error.
Two behaviours confuse people. First, once a quota covers a resource such as requests.cpu, every new Pod must specify it, or it is rejected. Second, with a Deployment the rejection does not show on the Deployment: the ReplicaSet simply fails to create Pods, and the reason appears in kubectl describe rs as a FailedCreate event.
- kubectl describe quota -n dev shows Used versus Hard
- kubectl create quota q --hard=pods=10,requests.cpu=2 -n dev
- Quotas never affect Pods that already exist
LimitRange: defaults and bounds
A LimitRange of type Container can set default (the limit added when none is given), defaultRequest, min, max and maxLimitRequestRatio. It acts at admission, when a Pod is created, so existing Pods are untouched. If only default is set, the request also defaults to that value.
Pair a quota with a LimitRange in every shared namespace: the quota sets the team budget, the LimitRange makes sure Pods without values still get some, so they are not rejected. kubectl describe pod shows the values that were actually applied, which is how you confirm the LimitRange worked.
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Set requests and limits on containers~45m
- 2Create a ResourceQuota~45m
- 3Set defaults with a LimitRange~45m
- 4Explain QoS classes~45m
Key points
- The scheduler uses requests, not limits or actual usage, to place Pods.
- CPU over limit is throttled; memory over limit is OOMKilled, exit code 137.
- With a CPU or memory quota, new Pods must declare those values or get LimitRange defaults.
- 1 CPU equals 1000m; memory uses Mi and Gi suffixes.
Key terms
Tap a card to flip it. 0/5 known
Examples
apiVersion: v1
kind: ResourceQuota
metadata: { name: team-quota, namespace: dev }
spec:
hard: { requests.cpu: "4", requests.memory: 8Gi, pods: "20" }
---
apiVersion: v1
kind: LimitRange
metadata: { name: defaults, namespace: dev }
spec:
limits:
- type: Container
defaultRequest: { cpu: 100m, memory: 128Mi }
default: { cpu: 500m, memory: 256Mi }Why the Deployment has zero Pods
In namespace dev a quota sets requests.cpu, and a developer applies a Deployment without resources. Step 1: kubectl get deploy shows 0/3 ready. Step 2: kubectl get pods shows nothing at all. Step 3: describe the ReplicaSet, not the Deployment, and read the FailedCreate event. Step 4: add a LimitRange with defaults, or set resources on the Deployment, and the ReplicaSet creates the Pods on its next retry.
kubectl -n dev get deploy api # READY 0/3
kubectl -n dev describe rs -l app=api | grep -A3 Events
# Error creating: pods ... is forbidden: failed quota: team-quota:
# must specify requests.cpu
kubectl -n dev set resources deploy api --requests=cpu=100m,memory=128Mi \
--limits=memory=256Mi
kubectl -n dev describe quota team-quotaSpotting an OOMKilled container
A Pod restarts every few minutes. Step 1: kubectl get pod shows a growing RESTARTS count. Step 2: describe shows Last State: Terminated, Reason: OOMKilled, Exit Code: 137. Step 3: kubectl top pod shows memory close to the limit. Step 4: raise the memory limit, or fix the leak, and watch the restarts stop.
kubectl get pod worker-0
kubectl get pod worker-0 -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}'
# OOMKilled
kubectl top pod worker-0 --containers
kubectl set resources statefulset worker --limits=memory=512Mi --requests=memory=384MiQuick reference
- 1 CPU = 1000m; memory Mi and Gi are powers of 1024; 128m memory is 0.128 bytes
- Only a limit set means the request is copied from it
- kubectl describe node | grep -A8 'Allocated resources' shows reserved totals
- kubectl set resources deploy web --requests=cpu=100m --limits=memory=256Mi
- Quota rejections for Deployments show on the ReplicaSet as FailedCreate
- Exit code 137 plus reason OOMKilled means the memory limit was hit
- Guaranteed QoS: every container has CPU and memory limits equal to requests
Common mistakes
- Setting requests larger than any node, leaving Pods Pending forever.
- Adding a quota without a LimitRange, so Pods without requests are rejected.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Create a Pod named qos with image nginx so that it gets the Guaranteed QoS class, and print its QoS class.
- Exercise 2medium
In namespace team, allow at most 5 Pods and 1 CPU of requests, and make Pods without values default to 100m CPU request and 200m CPU limit.
- Exercise 3hard
Create a Deployment big in team with 8 replicas and no resources set. Explain how many Pods run and prove where the error appears.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Should I always set CPU limits?
- Opinions differ. Many teams set CPU requests and memory limits but leave CPU limits off, to avoid needless throttling. For the exam, do exactly what the task asks.
- What does exit code 137 mean in Kubernetes?
- It is 128 plus signal 9 (SIGKILL). Together with reason OOMKilled, it means the container went over its memory limit.
- What is the difference between ResourceQuota and LimitRange?
- A ResourceQuota limits the total for a whole namespace. A LimitRange sets defaults and minimum or maximum values for each single container or Pod.