CPU requests and limits in Kubernetes - Time & Space Complexity
We want to understand how the CPU resource management in Kubernetes scales as more containers are scheduled.
Specifically, how does the system handle CPU requests and limits when the number of pods grows?
Analyze the time complexity of the following Kubernetes resource configuration snippet.
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: busybox
resources:
requests:
cpu: "100m"
limits:
cpu: "200m"
This snippet sets CPU requests and limits for a container inside a pod, defining how much CPU it asks for and the maximum it can use.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Kubernetes scheduler checks CPU requests and limits for each pod to allocate CPU resources.
- How many times: This check happens once per pod, so it repeats as many times as there are pods.
As the number of pods increases, the scheduler must process each pod's CPU requests and limits to assign CPU resources.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 CPU checks |
| 100 | 100 CPU checks |
| 1000 | 1000 CPU checks |
Pattern observation: The number of CPU resource checks grows directly with the number of pods.
Time Complexity: O(n)
This means the CPU resource allocation work grows linearly as more pods are added.
[X] Wrong: "CPU requests and limits are checked once for the whole cluster regardless of pod count."
[OK] Correct: Each pod's CPU needs are checked individually, so the work grows with the number of pods.
Understanding how resource checks scale helps you explain how Kubernetes manages workloads efficiently as clusters grow.
"What if we added CPU limits but no requests? How would the time complexity change?"
Practice
cpu request in a Kubernetes pod specification do?Solution
Step 1: Understand CPU request meaning
CPU request is the minimum CPU resource Kubernetes guarantees to a container.Step 2: Differentiate from CPU limit
CPU limit sets the maximum CPU usage, not the minimum guaranteed.Final Answer:
It guarantees a minimum amount of CPU for the container. -> Option AQuick Check:
CPU request = minimum CPU guaranteed [OK]
- Confusing CPU request with CPU limit
- Thinking requests set maximum CPU
- Mixing CPU with memory resource
Solution
Step 1: Check correct YAML structure for resources
The correct keys arerequestsandlimitsunderresources, with CPU values as strings with units like "500m" or "1".Step 2: Validate resources: requests: cpu: "500m" limits: cpu: "1" format
resources: requests: cpu: "500m" limits: cpu: "1" uses correct keys and units: "500m" means 0.5 CPU, "1" means 1 CPU core.Final Answer:
resources: requests: cpu: "500m" limits: cpu: "1" -> Option CQuick Check:
Correct YAML keys and CPU units = resources: requests: cpu: "500m" limits: cpu: "1" [OK]
- Using 'request' instead of 'requests'
- Specifying CPU as numbers without quotes or units
- Mixing up 'cpu' and 'memory' keys
resources:
requests:
cpu: "200m"
limits:
cpu: "500m"
What happens if the container tries to use 600m CPU?Solution
Step 1: Understand CPU limit effect
The CPU limit is the maximum CPU the container can use. Here it is 500m (0.5 CPU).Step 2: Predict container behavior when exceeding limit
If the container tries to use 600m CPU, Kubernetes throttles it to the limit, 500m CPU.Final Answer:
The container will be throttled to 500m CPU. -> Option BQuick Check:
CPU limit caps usage = throttled to 500m [OK]
- Thinking container can exceed CPU limit
- Confusing throttling with killing container
- Assuming CPU request is the max limit
resources:
requests:
cpu: "100m"
limits:
cpu: "50m"
What is wrong with this configuration?Solution
Step 1: Compare CPU request and limit values
CPU request is 100m (0.1 CPU), CPU limit is 50m (0.05 CPU).Step 2: Validate Kubernetes rules for requests and limits
CPU limit must be equal or greater than CPU request. Here limit < request, which is invalid.Final Answer:
CPU limit is less than CPU request, which is invalid. -> Option DQuick Check:
Limit >= Request rule violated = CPU limit is less than CPU request, which is invalid. [OK]
- Setting limit less than request
- Thinking CPU values must be integers
- Believing requests and limits can't coexist
Solution
Step 1: Identify correct CPU units and values
CPU values can be specified as millicores with 'm' suffix, e.g., "250m" for 0.25 CPU.Step 2: Check request and limit order
Request must be less than or equal to limit. resources: requests: cpu: "250m" limits: cpu: "750m" sets request to 250m and limit to 750m correctly.Final Answer:
resources: requests: cpu: "250m" limits: cpu: "750m" -> Option AQuick Check:
Request 250m ≤ Limit 750m with correct units = resources: requests: cpu: "250m" limits: cpu: "750m" [OK]
- Swapping request and limit values
- Using decimal numbers without quotes
- Not using millicores for fractional CPU
Ready to prove it?
Pass the test and this pattern moves unknown
