📖 Scenario: You are managing a Kubernetes cluster where you want to ensure that your application pods have guaranteed CPU and memory resources. This helps the cluster allocate resources efficiently and prevents pods from using too much or too little.
🎯 Goal: You will create a Kubernetes pod configuration YAML file that sets resource requests and limits for CPU and memory. This ensures your pod has the right amount of resources reserved and capped.
📋 What You'll Learn
Create a pod manifest with metadata name myapp-pod
Add a container named myapp-container using the image nginx
Set resource requests for CPU to 100m and memory to 200Mi
Set resource limits for CPU to 500m and memory to 500Mi
Print the final YAML manifest
💡 Why This Matters
🌍 Real World
Setting resource requests and limits is essential in real Kubernetes clusters to ensure fair resource distribution and stable application performance.
💼 Career
DevOps engineers and site reliability engineers often configure resource requests and limits to optimize cluster resource usage and prevent outages.
Progress0 / 4 steps
1
Create the basic pod manifest
Create a YAML manifest for a pod named myapp-pod with one container named myapp-container using the image nginx. Write the YAML starting with apiVersion: v1 and kind: Pod.
Kubernetes
Hint
Start with the basic pod structure and add metadata and container details.
2
Add resource requests
Add resource requests under the container spec. Set cpu to 100m and memory to 200Mi inside resources.requests.
Kubernetes
Hint
Indent resource requests correctly under the container.
3
Add resource limits
Add resource limits under the container spec inside resources. Set cpu to 500m and memory to 500Mi inside resources.limits.
Kubernetes
Hint
Place limits at the same level as requests under resources.
4
Print the final YAML manifest
Print the complete YAML manifest for the pod with resource requests and limits exactly as created.
Kubernetes
Hint
Use a print statement to output the full YAML manifest as a string.
Practice
(1/5)
1. What does setting a resource request for a container in Kubernetes guarantee?
easy
A. The container will get at least the requested CPU and memory resources.
B. The container can never use more than the requested resources.
C. The container will be restarted if it uses more than the requested resources.
D. The container will share resources equally with all other containers.
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 A
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 A
Quick Check:
CPU limit = max CPU usage [OK]
Hint: Limits define max usage, requests define minimum [OK]
Common Mistakes:
Confusing requests with limits
Adding requests and limits incorrectly
Assuming no max limit if requests set
4. You set a memory limit of 256Mi but forgot to set a memory request. What is the likely effect on your Pod scheduling?
medium
A. The Pod will use unlimited memory since request is missing.
B. The Pod may be scheduled with no guaranteed memory, risking eviction under pressure.
C. The Pod will be scheduled with 256Mi guaranteed memory automatically.
D. The Pod will fail to start due to missing memory request.
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 B
Quick Check:
Missing request means no guaranteed resources [OK]
Hint: Always set requests to guarantee resources and avoid eviction [OK]
Common Mistakes:
Assuming limits imply requests
Thinking Pod fails without requests
Believing missing requests mean unlimited resources
5. You want to ensure a container always has at least 100m CPU and 128Mi memory but never uses more than 300m CPU and 256Mi memory. Which is the correct resource configuration snippet?