Sub topic 2
Setting resource requests and limits
This chapter assumes you can apply manifests with kubectl, read YAML, and know what Pods and Deployments are. You should also know roughly what CPU and memory use means for a container.
Every scaling mechanism in Kubernetes reads or writes resource requests. If they are missing or wrong, the scheduler, the Horizontal Pod Autoscaler (HPA) and the cluster autoscaler all make poor decisions.
Requests and limits
A request is the amount of a resource a container is guaranteed. A limit is the most it may use.
- The scheduler uses requests to pick a node. A Pod fits only if the node has enough unrequested capacity.
- The kubelet uses limits to constrain the container at runtime.
- CPU is measured in cores.
200mmeans 200 millicores, or 0.2 of a core. - Memory is measured in bytes.
128Mimeans 128 mebibytes.
CPU and memory behave differently when a container hits its limit.
| Resource | Type | Over the limit |
|---|---|---|
| CPU | Compressible | The container is throttled. It slows down but keeps running. |
| Memory | Incompressible | The container is killed with an out-of-memory error (OOMKilled). |
A Deployment with requests and limits
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
resources:
requests:
cpu: 200m
memory: 128Mi
limits:
memory: 256MiThis manifest requests 200m CPU and 128Mi memory per Pod. It sets a memory limit of 256Mi and no CPU limit. Omitting the CPU limit is a common choice. It avoids throttling when a node has spare CPU, while the request still guarantees a share under contention.
Set the memory limit deliberately. Memory cannot be reclaimed without killing the process, so a limit protects the node from a leaking container.
Why scaling depends on requests
- Scheduler: it places Pods by their requests, not by actual usage.
- HPA: a
Utilizationtarget is a percentage of the request. If a container uses 140m CPU and requests 200m, utilization is 70%. With no CPU request, the HPA cannot compute utilization and reports an error. - Cluster autoscaler: it adds a node when Pods are Pending because no node has room for their requests.
Because of this, a request set too high wastes money, since nodes fill up on paper while sitting idle. A request set too low packs Pods too tightly. They compete for CPU, and the HPA sees inflated utilization and scales out sooner than needed.
Quality of Service classes
Kubernetes assigns each Pod a QoS class from its requests and limits. The class decides who is evicted first when a node runs out of memory.
- Guaranteed: every container has requests equal to limits for both CPU and memory. Evicted last.
- Burstable: at least one request or limit is set, but the Pod is not Guaranteed. The
webDeployment above is Burstable. - BestEffort: no requests or limits at all. Evicted first.
Choosing values
Start from measurement, not guesses. Run the workload under realistic load and observe usage with kubectl top pods, which needs Metrics Server (covered in a later chapter). Then:
- Set the CPU request near typical usage, so the HPA's percentage target means something.
- Set the memory request near typical usage, and the memory limit above the observed peak with headroom.
- Revisit the values after each major release.
The Vertical Pod Autoscaler can recommend values, and a later chapter covers it.
Common mistakes
- No requests on HPA targets. The HPA shows
<unknown>for CPU utilization. - Memory limit below real peak. Pods restart repeatedly with
OOMKilled. - Requests copied between services. Each workload needs its own values.
Exercise
A container requests 250m CPU and currently uses 100m. An HPA targets 50% average CPU utilization. Is the container above or below the target, and what happens if you raise the request to 500m without changing usage?
Show answer
Utilization is 100m / 250m = 40%, which is below the 50% target. Raising the request to 500m makes utilization 100m / 500m = 20%, so the HPA sees even less load and is more likely to scale in. The usage did not change. Only the denominator did.
Exercise
A Pod has a memory limit of 256Mi and the process tries to use 300Mi. What happens, and how would a CPU limit behave differently if the process tried to exceed it?
Show answer
The container is killed with OOMKilled, because memory cannot be reclaimed any other way. Exceeding a CPU limit only throttles the container, which slows it down without killing it.