📖 Scenario: You are managing a small Kubernetes cluster that runs several applications. To keep the cluster healthy and responsive, you need to control how much CPU and memory each application can use. This helps avoid one app using too many resources and slowing down others.
🎯 Goal: Learn how to set resource limits and requests for a Kubernetes pod to manage CPU and memory usage effectively.
📋 What You'll Learn
Create a pod specification with resource requests and limits
Set CPU and memory requests and limits correctly
Understand the impact of resource management on pod scheduling and stability
Display the pod configuration to verify resource settings
💡 Why This Matters
🌍 Real World
In real Kubernetes clusters, setting resource requests and limits prevents apps from using too many resources, which keeps the system stable and responsive.
💼 Career
Understanding resource management is essential for DevOps engineers and site reliability engineers to maintain healthy and efficient Kubernetes environments.
Progress0 / 4 steps
1
Create a basic pod YAML
Create a YAML file named pod.yaml that defines a pod called myapp running the container image nginx. Include the apiVersion, kind, metadata, and spec sections with the container name webserver.
Kubernetes
Hint
Start with the basic pod structure and specify the container image as nginx.
2
Add resource requests
In the pod.yaml, add resource requests under the container webserver. Set the CPU request to 100m and memory request to 200Mi.
Kubernetes
Hint
Use the resources.requests field to specify minimum CPU and memory the pod needs.
3
Add resource limits
Extend the pod.yaml to add resource limits under the container webserver. Set the CPU limit to 500m and memory limit to 500Mi.
Kubernetes
Hint
Use the resources.limits field to specify the maximum CPU and memory the pod can use.
4
Display the pod YAML
Print the contents of pod.yaml to verify the resource requests and limits are set correctly.
Kubernetes
Hint
Use multiple print statements to output each line of the pod YAML exactly as specified.
Practice
(1/5)
1. Why is resource management important in Kubernetes?
easy
A. It helps keep applications stable and fair in using CPU and memory.
B. It allows unlimited resource usage for all applications.
C. It disables resource limits to improve speed.
D. It automatically deletes unused pods.
Solution
Step 1: Understand resource management purpose
Resource management controls how much CPU and memory each app can use to avoid crashes and slowdowns.
Step 2: Identify correct effect of resource management
It ensures apps run smoothly and share resources fairly, preventing one app from using too much.
Final Answer:
It helps keep applications stable and fair in using CPU and memory. -> Option A
2. Which of the following is the correct way to specify resource requests in a pod YAML?
easy
A. resources:
requests:
cpu: "500m"
memory: "128Mi"
B. resources:
limits:
cpu: "500m"
memory: "128Mi"
C. resources:
requests:
cpu: 500
memory: 128
D. resources:
requests:
cpu: "500"
memory: "128"
Solution
Step 1: Check YAML syntax for resource requests
Resource requests must be under 'resources.requests' with units like "500m" for CPU and "128Mi" for memory.
Step 2: Identify correct units and structure
resources:
requests:
cpu: "500m"
memory: "128Mi" uses correct keys and units. resources:
limits:
cpu: "500m"
memory: "128Mi" uses limits, not requests. Options A and D miss units or use wrong formats.
Final Answer:
resources:
requests:
cpu: "500m"
memory: "128Mi" -> Option A
Quick Check:
Requests need units and correct YAML keys [OK]
Hint: Requests need units like 'm' for CPU and 'Mi' for memory [OK]
Common Mistakes:
Using limits instead of requests
Omitting units for CPU or memory
Using numeric values without quotes or units
3. Given this pod spec snippet, what happens if the container tries to use 1 CPU but the limit is 500m?
A. The container can use up to 1 CPU without restrictions.
B. The container ignores limits and uses requested CPU only.
C. The container is limited to 500m CPU and cannot exceed it.
D. The container will be killed immediately for exceeding requests.
Solution
Step 1: Understand CPU requests vs limits
Requests are guaranteed CPU; limits are the max CPU allowed. Here, request is 250m, limit is 500m.
Step 2: Analyze container CPU usage behavior
If container tries to use 1 CPU, it exceeds the 500m limit, so Kubernetes restricts it to 500m CPU max.
Final Answer:
The container is limited to 500m CPU and cannot exceed it. -> Option C
Quick Check:
CPU limit caps usage at 500m [OK]
Hint: Limits cap max usage; requests guarantee minimum [OK]
Common Mistakes:
Confusing requests with limits
Thinking container can exceed CPU limit
Believing container is killed for exceeding requests
4. You set a pod's memory limit to 256Mi but the container crashes with an OOMKilled error. What is the likely cause?
medium
A. The memory limit was too high, causing a crash.
B. The container tried to use more than 256Mi memory and was killed.
C. The pod did not have any resource requests set.
D. The CPU limit was exceeded, causing the crash.
Solution
Step 1: Understand OOMKilled meaning
OOMKilled means the container used more memory than its limit and was terminated by Kubernetes.
Step 2: Connect memory limit to crash cause
Since the limit was 256Mi, using more causes the container to be killed to protect the node.
Final Answer:
The container tried to use more than 256Mi memory and was killed. -> Option B
Quick Check:
OOMKilled = memory limit exceeded [OK]
Hint: OOMKilled means memory limit exceeded and container killed [OK]
Common Mistakes:
Confusing CPU limit with memory limit
Assuming high memory limit causes crash
Ignoring resource requests impact
5. You have multiple pods competing for CPU on a node. Pod A requests 200m CPU and limits 400m, Pod B requests 300m and limits 600m. If the node has 1 CPU available, how should Kubernetes allocate CPU to keep apps stable?
hard
A. Kubernetes ignores requests and only enforces limits strictly.
B. Kubernetes allows pods to use unlimited CPU regardless of requests or limits.
C. Kubernetes kills Pod A because it has lower requests than Pod B.
D. Kubernetes guarantees each pod its requested CPU and throttles usage above limits if needed.
Solution
Step 1: Understand CPU requests and limits in multi-pod scenario
Requests are guaranteed CPU shares; limits are max allowed. Kubernetes schedules pods so requests fit node capacity.
Step 2: Analyze CPU allocation with 1 CPU available
Pod A gets at least 200m CPU, Pod B gets 300m CPU guaranteed. If usage exceeds limits, Kubernetes throttles CPU to keep node stable.
Final Answer:
Kubernetes guarantees each pod its requested CPU and throttles usage above limits if needed. -> Option D
Quick Check:
Requests guarantee CPU; limits cap usage [OK]
Hint: Requests guarantee CPU; limits cap max usage [OK]
Common Mistakes:
Thinking pods get unlimited CPU
Assuming pods with lower requests get killed
Believing limits are ignored
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.