Difficulty: Intermediate
Explain resource requests vs resource limits in Kubernetes. What happens when a container exceeds its CPU or memory limit?
A request is the amount of CPU or memory the scheduler reserves for a container; a Pod is only placed on a node with enough unreserved capacity to cover its requests. A limit is the ceiling the container is allowed to use at runtime. Exceeding the CPU limit causes throttling (the process is slowed, not killed), while exceeding the memory limit gets the container OOM killed with exit code 137.
500m gets roughly twice the CPU time of one with 250m.Units: CPU in cores or millicores (250m is a quarter core). Memory in bytes, usually Mi/Gi (binary) rather than M/G (decimal).
kubectl describe shows Last State: Terminated, Reason: OOMKilled.If you set a limit but no request, Kubernetes sets the request equal to the limit.
Requests and limits together decide the Pod's Quality of Service class, which affects eviction order:
| QoS class | Rule | Evicted |
|---|---|---|
Guaranteed | Every container has CPU and memory requests equal to limits | Last |
Burstable | At least one request or limit set, but not Guaranteed | Middle |
BestEffort | No requests or limits at all | First |
LimitRange for namespace defaults and ResourceQuota to cap a team's total requests.| Request | Limit | |
|---|---|---|
| Used by | Scheduler and kubelet eviction | Kernel cgroups at runtime |
| CPU behaviour | Relative share under contention | Throttled when exceeded |
| Memory behaviour | Reserved on the node | OOMKilled when exceeded |
| If omitted | Defaults to the limit if one is set | No ceiling, can use spare capacity |
apiVersion: v1
kind: Pod
metadata:
name: orders-api
spec:
containers:
- name: app
image: myorg/orders-api:3.2.0
resources:
requests:
cpu: "500m" # half a core reserved for scheduling
memory: "512Mi"
limits:
memory: "512Mi" # equal to request: predictable memory
# no CPU limit: can burst into idle CPU without throttling
env:
- name: NODE_OPTIONS
value: "--max-old-space-size=384" # keep heap below the limit
This Pod is Burstable. Memory is capped tightly, CPU can burst, and the Node.js heap is sized below the memory limit to avoid OOM kills.
kubectl top pod orders-api # needs metrics-server
kubectl describe pod orders-api | grep -A3 'Last State'
# Last State: Terminated
# Reason: OOMKilled
# Exit Code: 137
kubectl get pod orders-api -o jsonpath='{.status.qosClass}'
# Burstable
Compare kubectl top against requests to right-size, and use describe to confirm whether restarts are OOM kills.
Resource Requests, Resource Limits, QoS Classes, CPU Throttling, OOMKilled