Resource Requests
Kubernetes uses two key concepts to manage container resources:
Resource Requests
A request specifies the minimum amount of resources that the container needs to run. The Kubernetes scheduler uses this information to decide which node to place the pod on.
- The scheduler ensures that the sum of all container requests on a node doesn’t exceed the node’s capacity
- Requests guarantee that a container will have at least that amount of resource available
Resource Limits
A limit specifies the maximum amount of resources that a container can use. If a container attempts to use more than its limit:
- For CPU: The container is throttled
- For memory: The container may be terminated (
OOMKilled
) if it exceeds its limit for too long
Example: Setting Requests and Limits
apiVersion: v1
kind: Pod
metadata:
name: resource-demo
spec:
containers:
- name: resource-demo-container
image: nginx
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
In this example:
- The container requests 64 MiB of memory and 0.25 CPU cores
- The container is limited to 128 MiB of memory and 0.5 CPU cores
Understanding CPU Resources
CPU resources are measured in CPU units. One CPU in Kubernetes is equivalent to:
- 1 vCPU/Core on cloud providers
- 1 hyperthread on bare-metal Intel processors with hyperthreading
CPU resources can be specified as:
- Whole numbers:
1
(1 CPU core) - Decimals:
0.5
(half a CPU core) - Millicores:
500m
(500 millicores = 0.5 CPU core)
CPU Behavior
- CPU Requests: Guaranteed minimum allocation
- CPU Limits: Throttling is applied when exceeded
- CPU is a compressible resource - containers can be throttled when exceeding their limit without being terminated
Understanding Memory Resources
Memory resources are measured in bytes. You can express memory as:
- Plain integers:
512
(512 bytes) - Binary notation:
128Mi
(128 mebibytes = 134,217,728 bytes) - Decimal notation:
128M
(128 megabytes = 128,000,000 bytes)
Common units include:
Ki
(kibibyte),Mi
(mebibyte),Gi
(gibibyte),Ti
(tebibyte)K
(kilobyte),M
(megabyte),G
(gigabyte),T
(terabyte)
Memory Behavior
- Memory Requests: Guaranteed minimum allocation
- Memory Limits: Container is terminated if exceeded
- Memory is a non-compressible resource - containers will be
OOMKilled
if they try to use more than their limit