Sub topic 1
What Resource Requests and Limits Are
Prerequisites
This chapter assumes you can do the following:
- You know the basic Kubernetes objects: pods, deployments and namespaces.
- You can read and apply YAML manifests with
kubectl. - You know that CPU cores and memory are finite machine resources.
The two numbers on every container
Each container in a pod can declare two values for each resource, CPU and memory.
- A request is the amount the container is guaranteed. The scheduler uses it to decide which node can host the pod.
- A limit is the most the container may use at runtime. The kubelet and the container runtime enforce it on the node.
Requests and limits are set per container, not per pod. The pod's total is the sum across its containers. Kubernetes also adds init containers and pod overhead to that total under its own rules, which later chapters don't cover.
A first example
This pod asks for a quarter of a CPU core and 128 MiB of memory. It is capped at half a core and 256 MiB.
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: app
image: nginx:1.27
resources:
requests:
cpu: 250m
memory: 128Mi
limits:
cpu: 500m
memory: 256MiThe 250m means 250 millicores, or 0.25 of a core. The next chapter covers units in detail.
Two different jobs
Requests and limits act at different times and on different components.
| Row | Request | Limit |
|---|---|---|
| Used by | Scheduler, at placement time | Kubelet and runtime, while running |
| Question it answers | Which node has room for this pod? | How much may this container consume? |
| Effect of exceeding it | Not applicable. A request is a reservation, not a ceiling. | CPU is throttled. Memory over the limit can trigger an OOM kill. |
A request is a bookkeeping promise. The scheduler subtracts it from the node's allocatable capacity, whether or not the container is actually using that much. A limit is a runtime ceiling.
The diagram below shows where each value is used.
What happens when you set nothing
If a container has no requests or limits, Kubernetes treats it as asking for nothing and allowing anything.
- The scheduler sees a request of zero, so it may pack many such pods onto one node.
- Nothing caps the container, so it can consume whatever the node has free.
- When the node runs short, these pods are the first candidates for eviction.
A namespace can supply defaults through a LimitRange, which a later chapter covers.
If you set only a limit, Kubernetes copies it into the request for that resource. If you set only a request, the limit stays unset.
Why this matters
Values that are too low or too high both cause problems.
- Requests set too low let the scheduler overcommit a node. Pods then compete for resources and may be throttled or evicted.
- Requests set too high waste capacity. Pods may stay
Pendingeven though the cluster is mostly idle. - Limits set too low cause throttling or
OOMKilledcontainers. - Missing limits let one noisy container affect its neighbours.
The combination of requests and limits also determines the pod's QoS class, which decides who gets evicted first under pressure.
Checking what a pod declared
You can read the settings back from the live object:
kubectl get pod web -o jsonpath='{.spec.containers[0].resources}'kubectl describe pod web shows the same values under each container's Requests and Limits, and it shows the node's allocated totals under Allocated resources.
Exercise
A container declares requests.memory: 128Mi and limits.memory: 256Mi. Which component uses the 128Mi value, and what does it do with it?
Show answer
The scheduler uses it. It subtracts 128Mi from a candidate node's allocatable memory to decide whether the pod fits. The 256Mi limit is enforced later by the kubelet and runtime on the node.
Exercise
A pod stays Pending while the cluster's actual CPU usage is low. Which setting is the most likely cause?
Show answer
The CPU request is too high. The scheduler reserves requests, not actual usage, so nodes can look full to it while they are mostly idle.