Kubernetes resource requests and limits are often treated as simple configuration values, but they directly influence how workloads are scheduled and how containers behave under resource pressure.
A pod can request CPU and memory from the cluster and optionally define limits for how much it is allowed to consume.
For example:
resources:
requests:
cpu: "250m"
memory: "256Mi"
limits:
cpu: "1"
memory: "512Mi"These values affect scheduling, runtime behavior, and resource isolation.
When evaluating a Kubernetes upgrade or tuning workloads, developers should test whether the configured resources match actual application requirements rather than relying on default values.
Understanding CPU and Memory Requests
A resource request tells Kubernetes how much resource a container expects to need.
For example:
resources:
requests:
cpu: "500m"
memory: "512Mi"This means the scheduler considers the pod's requested resources when selecting a node.
CPU is generally expressed in millicores:
1000m = 1 CPU
500m = 0.5 CPU
250m = 0.25 CPUMemory is normally expressed using units such as:
128Mi
256Mi
512Mi
1GiRequests are important because Kubernetes uses them when determining whether a node has sufficient allocatable capacity.
Understanding Resource Limits
A limit defines the maximum resource consumption allowed for a container.
For example:
resources:
requests:
cpu: "250m"
memory: "256Mi"
limits:
cpu: "1"
memory: "512Mi"Here, the workload requests:
250 millicores of CPU
256 MiB of memory
while its configured limits are:
1 CPU
512 MiB of memory
Requests and limits serve different purposes and should not automatically be set to the same value.
CPU and Memory Behave Differently
One of the most important concepts is that CPU and memory are handled differently.
CPU is a compressible resource.
If a container reaches its CPU limit, Kubernetes can throttle its CPU usage.
Memory is different.
If a container exceeds its memory limit and the system cannot reclaim enough memory, the container can be terminated due to an out-of-memory condition.
Conceptually:
CPU
|
+-- Usage exceeds limit
|
v
Throttling
Memory
|
+-- Usage exceeds available limit
|
v
Possible OOM terminationThis difference is critical when configuring production workloads.
Creating a Test Pod
A simple resource-testing pod can be defined as:
apiVersion: v1
kind: Pod
metadata:
name: resource-test
spec:
containers:
- name: app
image: nginx
resources:
requests:
cpu: "250m"
memory: "256Mi"
limits:
cpu: "1"
memory: "512Mi"Apply it with:
kubectl apply -f resource-test.yamlThen verify:
kubectl get pod resource-testInspecting Allocated Resources
Use:
kubectl describe pod resource-testLook for the container's resource configuration.
You can also retrieve the complete configuration:
kubectl get pod resource-test -o yamlThis is useful when troubleshooting because the live Kubernetes object may differ from the configuration file you believe was deployed.
Checking Node Capacity
Before testing resource allocation, inspect the node:
kubectl describe node <node-name>Important values include:
Capacity
Allocatable
Allocated resourcesA node might have:
CPU Capacity: 8
CPU Allocatable: 7.5
Memory Capacity: 32Gi
Memory Allocatable: 30GiThe distinction between capacity and allocatable resources matters because not all node resources are available for application pods.
Checking Actual Usage
For clusters with the appropriate resource metrics available, use:
kubectl top podsFor a particular namespace:
kubectl top pods -n productionNode usage can be checked with:
kubectl top nodesThese commands help compare configured resources with actual consumption.
For example:
NAME CPU(cores) MEMORY(bytes)
api-pod 180m 210Mi
worker 750m 420MiThis provides a starting point for deciding whether requests and limits are reasonable.
Testing CPU Allocation
Create a workload that intentionally consumes CPU.
For example:
apiVersion: v1
kind: Pod
metadata:
name: cpu-test
spec:
containers:
- name: cpu
image: busybox
command:
- sh
- -c
- |
while true; do
:
done
resources:
requests:
cpu: "100m"
limits:
cpu: "500m"The process continuously performs work, allowing you to observe CPU behavior.
Monitor it:
kubectl top pod cpu-testThe exact observed CPU usage depends on the node, runtime, competing workloads, and resource configuration.
CPU Throttling
Suppose a container has:
limits:
cpu: "500m"and attempts to consume more CPU than that.
The container does not normally receive unlimited CPU simply because the node has spare capacity. Its CPU usage can be constrained according to the configured limit.
This can become important for latency-sensitive services.
A workload that repeatedly hits its CPU limit may experience:
Increased response latency
Lower throughput
Longer processing times
Queue buildup
Therefore, CPU limits should be evaluated against actual workload behavior.
Testing Memory Allocation
Memory should be tested separately.
For example:
apiVersion: v1
kind: Pod
metadata:
name: memory-test
spec:
containers:
- name: memory
image: polinux/stress
args:
- --vm
- "1"
- --vm-bytes
- "400M"
- --vm-hang
- "1"
resources:
requests:
memory: "256Mi"
limits:
memory: "512Mi"The purpose of this type of test is to observe how the workload behaves when memory consumption approaches its configured limit.
Join the conversation! Your thoughts help the community grow.