Resource requests and limits in Kubernetes - Time & Space Complexity
We want to understand how setting resource requests and limits affects scheduling and running containers in Kubernetes.
How does the system's work grow as we add more containers with these settings?
Analyze the time complexity of scheduling pods with resource requests and limits.
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: app
image: nginx
resources:
requests:
cpu: "100m"
memory: "200Mi"
limits:
cpu: "200m"
memory: "400Mi"
This pod requests and limits CPU and memory resources for scheduling and runtime enforcement.
- Primary operation: Scheduler checks each node's available resources against pod requests.
- How many times: For each pod, the scheduler loops through all nodes to find a fit.
As the number of pods increases, the scheduler must check more pods against nodes.
| Input Size (pods) | Approx. Operations (node checks) |
|---|---|
| 10 | 10 x number_of_nodes |
| 100 | 100 x number_of_nodes |
| 1000 | 1000 x number_of_nodes |
Pattern observation: The work grows linearly with the number of pods times the number of nodes.
Time Complexity: O(p x n)
This means the scheduler's work grows proportionally to the number of pods times the number of nodes.
[X] Wrong: "Scheduling time depends only on the number of pods."
[OK] Correct: The scheduler must check each pod against all nodes, so nodes count matters too.
Understanding how resource requests and limits affect scheduling helps you explain system behavior clearly and shows you grasp real cluster operations.
"What if the scheduler used a cache to track node resources instead of checking all nodes each time? How would the time complexity change?"
Practice
resource request for a container in Kubernetes guarantee?Solution
Step 1: Understand resource requests in Kubernetes
Resource requests specify the minimum CPU and memory a container needs to run properly.Step 2: Clarify what is guaranteed by requests
Kubernetes ensures the container gets at least the requested resources but does not limit usage to that amount.Final Answer:
The container will get at least the requested CPU and memory resources. -> Option AQuick Check:
Resource request = minimum guaranteed resources [OK]
- Confusing requests with limits
- Thinking requests cap usage
- Assuming requests cause restarts
Solution
Step 1: Review correct syntax for resource requests
CPU requests use millicores with "m" suffix as string, memory uses Mi or Gi units as string.Step 2: Identify correct option
resources: requests: cpu: "500m" memory: "256Mi" uses "500m" for CPU and "256Mi" for memory correctly as strings.Final Answer:
resources: requests: cpu: "500m" memory: "256Mi" -> Option DQuick Check:
CPU in millicores and memory in Mi with quotes [OK]
- Omitting quotes around resource values
- Using wrong units like MB instead of Mi
- Specifying CPU as plain number without m
resources:
requests:
cpu: "200m"
limits:
cpu: "500m"Solution
Step 1: Understand CPU requests and limits
Requests set minimum guaranteed CPU, limits set maximum allowed CPU usage.Step 2: Identify maximum CPU from limits
The limit is "500m", meaning 500 millicores is the max CPU the container can use.Final Answer:
500 millicores -> Option AQuick Check:
CPU limit = max CPU usage [OK]
- Confusing requests with limits
- Adding requests and limits incorrectly
- Assuming no max limit if requests set
Solution
Step 1: Understand default behavior when requests are missing
If memory request is not set, Kubernetes treats it as zero, meaning no guaranteed memory.Step 2: Effect on scheduling and eviction
Pod may be scheduled but can be evicted if node memory is low because it has no guaranteed reservation.Final Answer:
The Pod may be scheduled with no guaranteed memory, risking eviction under pressure. -> Option BQuick Check:
Missing request means no guaranteed resources [OK]
- Assuming limits imply requests
- Thinking Pod fails without requests
- Believing missing requests mean unlimited resources
Solution
Step 1: Match minimum guaranteed resources to requests
Requests must be set to 100m CPU and 128Mi memory to guarantee minimum.Step 2: Match maximum allowed resources to limits
Limits must be set to 300m CPU and 256Mi memory to cap usage.Step 3: Verify correct syntax and order
resources: requests: cpu: "100m" memory: "128Mi" limits: cpu: "300m" memory: "256Mi" correctly sets requests first, then limits with proper units and quotes.Final Answer:
resources: requests: cpu: "100m" memory: "128Mi" limits: cpu: "300m" memory: "256Mi" -> Option CQuick Check:
Requests = min, Limits = max with correct units [OK]
- Swapping requests and limits
- Omitting units or quotes
- Using plain numbers without units
Ready to prove it?
Pass the test and this pattern moves unknown
