📖 Scenario: You are managing a Kubernetes cluster where developers deploy their applications. To ensure fair resource usage and avoid accidental overconsumption, you want to set default resource limits and requests for all pods in a namespace.
🎯 Goal: Create a Kubernetes LimitRange resource that sets default CPU and memory requests and limits for containers in a namespace.
📋 What You'll Learn
Create a LimitRange YAML manifest named limitrange.yaml
Set default CPU request to 100m and default memory request to 200Mi
Set default CPU limit to 500m and default memory limit to 500Mi
Apply the LimitRange to the development namespace
💡 Why This Matters
🌍 Real World
In real Kubernetes clusters, setting default resource limits helps avoid resource starvation and ensures fair sharing among applications.
💼 Career
DevOps engineers and cluster administrators use LimitRanges to enforce resource policies and maintain cluster stability.
Progress0 / 4 steps
1
Create the basic LimitRange YAML skeleton
Create a YAML file named limitrange.yaml with the following top-level keys: apiVersion set to v1, kind set to LimitRange, and metadata with name set to resource-limits and namespace set to development.
Kubernetes
Hint
Use the apiVersion, kind, and metadata fields to define the LimitRange resource.
2
Add the spec and limits section
Add a spec section with a limits list containing one item. This item should have type set to Container.
Kubernetes
Hint
The spec section defines the rules. The limits list holds the limit definitions.
3
Set default CPU and memory requests and limits
Inside the limits item, add defaultRequest with cpu: 100m and memory: 200Mi, and default with cpu: 500m and memory: 500Mi.
Kubernetes
Hint
Use defaultRequest for default resource requests and default for default limits.
4
Apply the LimitRange to the development namespace and verify
Run the command kubectl apply -f limitrange.yaml to create the LimitRange in the development namespace. Then run kubectl get limitrange resource-limits -n development -o yaml to display the applied LimitRange.
Kubernetes
Hint
Use kubectl apply -f limitrange.yaml to apply and kubectl get limitrange resource-limits -n development -o yaml to verify.
Practice
(1/5)
1. What is the main purpose of a LimitRange in a Kubernetes namespace?
easy
A. To schedule pods on specific nodes
B. To set default and maximum resource limits for containers in that namespace
C. To create network policies for pods
D. To manage persistent storage volumes
Solution
Step 1: Understand LimitRange role
A LimitRange defines default, minimum, and maximum resource limits for containers in a namespace.
Step 2: Compare with other options
Network policies, scheduling, and storage are managed by other Kubernetes resources, not LimitRange.
Final Answer:
To set default and maximum resource limits for containers in that namespace -> Option B
Quick Check:
LimitRange controls resource limits = C [OK]
Hint: LimitRange controls resource limits, not networking or storage [OK]
Common Mistakes:
Confusing LimitRange with NetworkPolicy
Thinking LimitRange schedules pods
Assuming LimitRange manages storage
2. Which of the following is the correct YAML snippet to set a default CPU request of 100m using a LimitRange?
5. You want all containers in a namespace to have a default CPU request of 200m and a default memory request of 512Mi, but also want to ensure no container can request more than 1 CPU or 1Gi memory. Which LimitRange YAML snippet correctly enforces this?