Resource Requests and Limits
🎯 Introduction
Previously, we used probes to check the order service's health. Now consider another question: what happens when many Pods on one machine all consume large amounts of CPU and memory?
The order service needs resources to run reliably. Without knowing roughly what each container needs, Kubernetes cannot place Pods effectively. An application that continually consumes memory may also affect other applications.
This article covers two settings: requests and limits. You will learn how Kubernetes uses them to place Pods and what can happen when CPU or memory is constrained.
🧱 Why use requests and limits?
To run the order service on a Kubernetes node, we need to answer two questions:
requests: How many resources should we budget for this container? Kubernetes uses this information to decide which node can run the Pod.limits: How many resources can the running container use at most? Kubernetes uses these values to constrain resource usage.
Put simply, requests guides placement; limits sets usage ceilings. Keep this distinction in mind as we examine the values below.
🧩 Understanding CPU and memory values
This article uses four values:
| Setting | Example | Meaning |
|---|---|---|
CPU requests | 100m | Scheduling accounts for a requirement of 0.1 CPU cores |
CPU limits | 500m | Runtime CPU usage is limited to the equivalent of 0.5 cores |
Memory requests | 128Mi | Scheduling accounts for a requirement of 128Mi of memory |
Memory limits | 256Mi | Container memory usage is limited to 256Mi |
Two units are easy to confuse:
mmeans one thousandth of a CPU:1000mrepresents one CPU core's computing capacity, so100mis0.1cores, not 100 MB.Miis a memory unit:128Mimeans128 × 1024 × 1024bytes, and1Giequals1024Mi. For now, recognize these as memory capacities.
For example, suppose a node has 500m CPU and 512Mi memory remaining for new Pod requests. An order container requesting 100m CPU and 128Mi memory fits within that remaining budget, making the node a scheduling candidate.
If the container instead requests 1000m CPU and 1Gi memory, this node cannot satisfy it. Kubernetes looks for another suitable node; if none qualifies, the Pod may remain Pending, awaiting scheduling. This compares node allocatable resources with the requests of scheduled Pods, not the machine's current CPU usage.
Also, a request is not a usage ceiling. With a CPU request of 100m and a limit of 500m, the service can use more than 100m when needed, up to its limit. A memory request of 128Mi does not cause termination at 129Mi; the memory limit here is 256Mi.
These values are for practice, not universal production recommendations for Node.js services. Choose real settings based on application behavior.
🛠 Configuring resources for the order service
Continue with the existing orders Deployment. Open deployment.yaml, find the orders container, and add resources alongside image, ports, env, and the probes.
This is partial configuration, not a complete Deployment manifest. Retain all other existing fields.
spec:
template:
spec:
containers:
- name: orders
# Keep existing image, ports, env, probes, and other fields
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 256Mi
These four values are the CPU and memory settings from the earlier table. Save the file and run this in its directory:
kubectl apply -f deployment.yaml -n course
kubectl rollout status deployment/orders -n course
Simulated output:
deployment.apps/orders configured
deployment "orders" successfully rolled out
Next, check that the Deployment includes the settings:
kubectl describe deployment orders -n course
Simulated output, showing only container resource settings:
Containers:
orders:
Image: course-orders:v1
Limits:
cpu: 500m
memory: 256Mi
Requests:
cpu: 100m
memory: 128Mi
The values under Requests and Limits confirm that the Deployment template records your configuration. They are configured requests and limits, not actual CPU or memory usage.
🔎 What happens when a container exceeds its limits?
CPU and memory are handled differently.
CPU: computing time is usually throttled. Suppose the order service suddenly receives many requests and needs more CPU, but limits.cpu is 500m. CPU throttling may slow endpoint responses. Reaching the CPU limit alone does not terminate the container.
Memory: exceeding the limit may lead to termination. If the service leaks memory and its usage grows beyond limits.memory: 256Mi, it may be terminated due to OOM (out of memory). You may see OOMKilled, followed by a restart according to the Pod's restart policy. Termination is not necessarily immediate the moment usage exceeds the value.
If you see OOMKilled, inspect the memory limit and why the application needs so much memory. If the application works but responds slowly, consider whether its CPU limit is too low. Adding Pod replicas is not a solution to every failure.
We will not deliberately cause OOM or high CPU load here. For beginners, understanding requests versus limits and being able to configure and verify them is sufficient.
❓ Questions and answers
Q: Does requests.cpu: 100m mean the service constantly consumes 0.1 CPU cores?
No. It is a resource request used for scheduling. Actual CPU usage varies with application traffic.
Q: Why configure both requests and limits?
They solve different problems: requests help Kubernetes place Pods, while limits constrain running containers. Their values can differ.
Q: Does reaching a CPU limit terminate a container like exceeding a memory limit?
Usually not. CPU usage is generally throttled, while exceeding memory limits may cause OOM termination.
🧪 Exercise
In deployment.yaml, change the order container's requests.cpu from 100m to 200m, keeping limits.cpu: 500m.
- Run
kubectl apply -f deployment.yaml -n courseagain and wait for the Deployment update to finish. - Use
kubectl describe deployment orders -n courseto confirm thatRequests.cpuis200mandLimits.cpuremains500m. - Consider: with
requests.cpuat200mandlimits.cpustill500m, is the container limited to200mCPU? Why? - Restore
requests.cputo100mand reapply the file to retain the course's original configuration.
Success criteria: Observe the changed settings and explain that 200m changes the declared scheduling requirement, without reducing the CPU usage limit from 500m to 200m.
🎉 You can now configure CPU and memory requests and limits for a Node.js container and understand how Kubernetes places Pods and why CPU and memory limits behave differently.
Official reference: Kubernetes: Resource Management for Pods and Containers
