📖 Scenario: You are managing a Kubernetes cluster for a small web application. To ensure the application runs smoothly without using too many resources, you need to set CPU requests and limits for the application pod.
🎯 Goal: Learn how to define CPU requests and limits in a Kubernetes pod configuration to control resource usage effectively.
📋 What You'll Learn
Create a pod configuration with a container named webapp
Set the CPU request to 250m (250 millicores)
Set the CPU limit to 500m (500 millicores)
Use the correct YAML structure for resource requests and limits
💡 Why This Matters
🌍 Real World
Setting CPU requests and limits is essential in Kubernetes to ensure applications get enough resources without overloading the cluster.
💼 Career
Understanding resource management in Kubernetes is a key skill for DevOps engineers and cloud administrators.
Progress0 / 4 steps
1
Create the basic pod YAML skeleton
Create a YAML file with a pod named cpu-demo-pod and a container named webapp using the image nginx. Include the required fields: apiVersion, kind, metadata, and spec with the container definition.
Kubernetes
Hint
Start with the basic pod structure and container details.
2
Add CPU requests to the container
Add a resources section under the webapp container. Inside resources, add requests with cpu: "250m" to request 250 millicores of CPU.
Kubernetes
Hint
Indent resources under the container, then add requests with the CPU value.
3
Add CPU limits to the container
Extend the resources section by adding limits with cpu: "500m" to limit CPU usage to 500 millicores.
Kubernetes
Hint
Under resources, add limits with the CPU limit value.
4
Display the final pod YAML
Print the complete YAML configuration for the pod named cpu-demo-pod with the container webapp that has CPU requests and limits set.
Kubernetes
Hint
Show the full YAML text as the output.
Practice
(1/5)
1. What does setting a cpu request in a Kubernetes pod specification do?
easy
A. It guarantees a minimum amount of CPU for the container.
B. It limits the maximum CPU the container can use.
C. It sets the CPU priority for the container.
D. It reserves memory for the container.
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 A
Quick Check:
CPU request = minimum CPU guaranteed [OK]
Hint: Requests guarantee minimum CPU, limits cap maximum CPU [OK]
Common Mistakes:
Confusing CPU request with CPU limit
Thinking requests set maximum CPU
Mixing CPU with memory resource
2. Which of the following is the correct way to specify CPU requests and limits in a pod YAML?
easy
A. resources:
cpu:
request: 500m
limit: 1
B. resources:
requests:
cpu: 500
limits:
cpu: 1000
C. resources:
requests:
cpu: "500m"
limits:
cpu: "1"
D. resources:
requests:
cpu: "1.0"
limits:
cpu: "0.5"
Solution
Step 1: Check correct YAML structure for resources
The correct keys are requests and limits under resources, with CPU values as strings with units like "500m" or "1".
A. CPU requests and limits cannot be set together.
B. CPU request is less than CPU limit, which is invalid.
C. CPU values must be integers, not strings.
D. CPU limit is less than CPU request, which is invalid.
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 D
Quick Check:
Limit >= Request rule violated = CPU limit is less than CPU request, which is invalid. [OK]
Hint: Limit must be >= request, else config is invalid [OK]
Common Mistakes:
Setting limit less than request
Thinking CPU values must be integers
Believing requests and limits can't coexist
5. You want to ensure a container always gets at least 250m CPU but never uses more than 750m CPU. Which YAML snippet correctly sets this with CPU requests and limits?
hard
A. resources:
requests:
cpu: "250m"
limits:
cpu: "750m"
B. resources:
requests:
cpu: "750m"
limits:
cpu: "250m"
C. resources:
requests:
cpu: 0.25
limits:
cpu: 0.75
D. resources:
requests:
cpu: "0.75"
limits:
cpu: "0.25"
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 A
Quick Check:
Request 250m ≤ Limit 750m with correct units = resources:
requests:
cpu: "250m"
limits:
cpu: "750m" [OK]
Hint: Use millicores with 'm' and keep request ≤ limit [OK]
Common Mistakes:
Swapping request and limit values
Using decimal numbers without quotes
Not using millicores for fractional CPU
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.