Kubernetes lets you define CPU and memory (RAM) constraints for containers using resource requests and limits. This ensures fair resource allocation, prevents node overcommitment, and improves cluster stability and performance. Today youβll learn how to configure, monitor, and troubleshoot them.
β‘ Quick Reference
Keep the momentum going β everything you need on LearnXOps in one place:
- π Courses β Video courses & AI-built courses
- π Ebooks β Community ebooks & your own
- π§ͺ All Labs & Challenges β DevOps, Cloud, SRE & more
- βΈοΈ Kubernetes Labs & Playground β kind clusters, playground & challenges
- π€ AI Agent & Bot Labs β OpenClaw Β· Hermes Β· CrewAI Β· LangGraph Β· MCP

Download a high-resolution copy of this diagram here for future reference.
Case Study: Production-grade PostgreSQL setup in Kubernetes using PGO (PostgreSQL Operator)
π Key Learnings
πΉ What are resource requests and limits, and how do they work (deep understanding)?
πΉ Difference between:
requests: minimum guaranteed resources for schedulinglimits: max allowed usage, enforced at runtime
πΉ Scheduling decisions based on requests, not limits
πΉ What happens when a container exceeds its limit (OOMKilled, throttling)
πΉ How to apply resource constraints at:
- Container level
- Namespace level (via ResourceQuota)
πΉ CPU units (m = millicores), memory units (Mi, Gi)
πΉ Monitoring resource usage with kubectl top, metrics-server, or Prometheus
πΉ QoS Classes in Kubernetes
πΉ Best practices for setting appropriate values
π§ Learn here
Resource Requests and Limits in Kubernetes:
In Kubernetes, resource requests and limits define how much CPU and memory (RAM) a container can use:
- Requests: The minimum amount of resources guaranteed to the container. Kubernetes uses this during scheduling.
- Limits: The maximum amount of resources the container is allowed to use. The container is throttled or killed if it tries to exceed this.
Difference between Requests and Limits
| Aspect | requests |
limits |
|---|---|---|
| Purpose | Minimum guaranteed resources | Max allowed usage |
| Used by Scheduler | β Yes | β No |
| Enforced at runtime | β No | β Yes |
| Behavior when exceeded | Runs normally | CPU: throttled, Memory: OOMKilled |
How Kubernetes Makes Scheduling Decisions
The Kube-scheduler uses requests to place pods onto nodes. It ensures the node has at least that amount of available CPU/Memory. limits are not considered during scheduling.
YAML Example:
apiVersion: v1
kind: Pod
metadata:
name: app-pod
spec:
containers:
- name: app-container
image: nginx
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
What Happens When a Container Exceeds Its Limits?
- CPU Limit Exceeded: The container is throttled (limited in processing speed).
- Memory Limit Exceeded: The container is terminated with an OOMKilled error.
Check for OOMKilled:
kubectl get pod app-pod
kubectl describe pod app-pod | grep -A5 State
Resource Constraints at Different Levels
1. Container Level β Defined in the resources section (see YAML above)
2. Namespace Level β Use ResourceQuota to limit usage for the entire namespace
YAML: ResourceQuota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: mem-cpu-quota
namespace: dev-team
spec:
hard:
requests.cpu: "2"
requests.memory: "2Gi"
limits.cpu: "4"
limits.memory: "4Gi"
Apply it:
kubectl apply -f resource-quota.yaml
CPU and Memory Units
- CPU:
- 1 core =
1000m 500m= 0.5 CPU cores
- 1 core =
- Memory:
Mi= Mebibytes (1Mi = 1024^2 bytes)Gi= Gibibytes (1Gi = 1024^3 bytes)
Monitoring Resource Usage
1. Metrics Server
Install:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
Check usage:
kubectl top pods
kubectl top nodes
Prometheus + Grafana
Install via Helm:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install k8s-monitor prometheus-community/kube-prometheus-stack -n monitoring --create-namespaceUse Grafana dashboards to track CPU/Memory.
QoS Classes in Kubernetes
Kubernetes assigns QoS Classes to pods based on their resource requests and limits for CPU and memory. These classes influence pod eviction during node resource pressure (especially memory).
There are 3 QoS classes:
| QoS Class | Condition |
|---|---|
Guaranteed |
Requests = Limits for both CPU and memory for all containers |
Burstable |
At least one container has a CPU or memory request or limit, but the pod doesn't meet the Guaranteed rules |
BestEffort |
No container has any CPU or memory request or limit |
Why QoS Classes Matter?
- During memory pressure, the kubelet first evicts pods using more than they requested, then looks at Priority and usage. In practice that means:
BestEffortβ evicted firstBurstableβ evicted nextGuaranteedβ evicted last
- Helps prioritize critical workloads.
YAML Examples of Each QoS Class
1. Guaranteed
apiVersion: v1
kind: Pod
metadata:
name: guaranteed-pod
spec:
containers:
- name: app
image: nginx
resources:
requests:
memory: "128Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "250m"
β Limits = Requests for both CPU and memory β Guaranteed QoS
2. Burstable
apiVersion: v1
kind: Pod
metadata:
name: burstable-pod
spec:
containers:
- name: app
image: nginx
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "500m"
β Requests < Limits for at least one resource β Burstable QoS
3. BestEffort
apiVersion: v1
kind: Pod
metadata:
name: besteffort-pod
spec:
containers:
- name: app
image: nginx
β No requests/limits set β BestEffort QoS
π§ Check QoS Class of a Pod
kubectl get pod <pod-name> -o jsonpath='{.status.qosClass}'
Example:
kubectl get pod guaranteed-pod -o jsonpath='{.status.qosClass}'
# Output: Guaranteed
π‘ Useful Commands
- View all pod QoS classes in a namespace:
kubectl get pods -o custom-columns="NAME:.metadata.name,QOS:.status.qosClass"
QOS Best Practices
- For critical apps, use Guaranteed QoS.
- For normal workloads, Burstable is fine with sane requests and upper limits.
- Avoid BestEffort unless absolutely non-critical.
- Use ResourceQuota at the namespace level to control overall usage.
Request & Limit Best Practices
- Always define
requests, and set a memorylimit. CPU limits are optional: they throttle a container even when the node has idle CPU, so many teams skip them for latency-sensitive apps. - Set realistic values based on past usage.
- Use
kubectl topand Prometheus metrics to tune values. - Avoid overcommitting memory.
- Apply
LimitRangeandResourceQuotain namespaces for governance. - Monitor OOMKilled and throttling events to adjust limits.
π‘Bonus: Set Default Limits with LimitRange
apiVersion: v1
kind: LimitRange
metadata:
name: default-limits
namespace: dev-team
spec:
limits:
- default:
cpu: "500m"
memory: "512Mi"
defaultRequest:
cpu: "250m"
memory: "256Mi"
type: Container
Apply it:
kubectl apply -f limit-range.yaml
π Learning References
π Kubernetes Resource Management β Official Docs
π CPU and Memory Units
π ResourceQuota
π LimitRange
π Understanding Pod Scheduling
π₯ Challenges
π‘ Challenge 1: Deploy a container with 128Mi memory request and 256Mi limit
π‘ Challenge 2: Trigger an OOMKilled state using stress image and inspect logs
π‘ Challenge 3: Use kubectl top and kubectl describe to analyze resource usage
π‘ Challenge 4: Apply a LimitRange and deploy a pod without explicit limits to see defaults applied
π‘ Challenge 5: Set limits too low and observe throttling or restart behavior
π‘ Challenge 6: Deploy 5 pods with total usage exceeding node capacity β observe scheduling behavior
π‘ Challenge 7: Create a ResourceQuota limiting total CPU and memory in a namespace
π‘ Challenge 8: Simulate an app that bursts CPU but has a tight limit β identify throttling
π‘ Challenge 9: Monitor metrics using metrics-server or Prometheus to validate actual usage
π‘ Challenge 10: Test HPA behavior with different request values and watch scaling impact
π‘Challenge 11: Final Project: Resource-Constrained Application Environment
π Create a namespace test-env
π Add:
LimitRangefor default per-container limitsResourceQuotato cap total usage
π Deploy:
- 2-3 apps with well-defined resource settings
- 1 pod that intentionally exceeds the quota (watch it fail)
π Analyze and document the behavior of OOMKilled, throttled, and pending pods
π Push YAMLs + a summary of findings to GitHub
Solutions
β Challenge 1: Deploy a container with 128Mi memory request and 256Mi limit
apiVersion: v1
kind: Pod
metadata:
name: mem-limited-pod
spec:
containers:
- name: memory-demo
image: busybox
command: ["sleep", "3600"]
resources:
requests:
memory: "128Mi"
limits:
memory: "256Mi"
β Challenge 2: Trigger an OOMKilled state using stress image and inspect logs
apiVersion: v1
kind: Pod
metadata:
name: oom-stress-pod
spec:
containers:
- name: stress
image: polinux/stress
args: ["--vm", "1", "--vm-bytes", "300M", "--vm-hang", "1"]
resources:
limits:
memory: "200Mi"
Inspect with:
kubectl describe pod oom-stress-pod
kubectl logs oom-stress-pod
β Challenge 3: Use kubectl top and kubectl describe to analyze resource usage
kubectl top pod mem-limited-pod
kubectl describe pod mem-limited-pod
β Challenge 4: Apply a LimitRange and deploy a pod without explicit limits
apiVersion: v1
kind: LimitRange
metadata:
name: default-limit-range
spec:
limits:
- default:
cpu: "500m"
memory: "256Mi"
defaultRequest:
cpu: "250m"
memory: "128Mi"
type: Container
apiVersion: v1
kind: Pod
metadata:
name: no-resource-pod
spec:
containers:
- name: app
image: nginx
β Challenge 5: Set limits too low and observe throttling or restart behavior
apiVersion: v1
kind: Pod
metadata:
name: low-cpu-pod
spec:
containers:
- name: cpu-hog
image: vish/stress
args: ["--cpu", "1"]
resources:
limits:
cpu: "100m"
Inspect with:
kubectl describe pod low-cpu-pod
kubectl top pod low-cpu-pod
β Challenge 6: Deploy 5 pods with total usage exceeding node capacity
for i in {1..5}; do
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: stress-$i
spec:
containers:
- name: stress
image: polinux/stress
args: ["--vm", "1", "--vm-bytes", "300M", "--vm-hang", "1"]
resources:
requests:
memory: "300Mi"
limits:
memory: "500Mi"
EOF
done
Check scheduling:
kubectl get pods -o wide
β Challenge 7: Create a ResourceQuota limiting total CPU and memory
apiVersion: v1
kind: ResourceQuota
metadata:
name: quota-demo
spec:
hard:
requests.cpu: "1"
requests.memory: "1Gi"
limits.cpu: "2"
limits.memory: "2Gi"
β Challenge 8: Simulate app that bursts CPU but has a tight limit
apiVersion: v1
kind: Pod
metadata:
name: bursty-cpu-pod
spec:
containers:
- name: stress
image: vish/stress
args: ["--cpu", "2"]
resources:
limits:
cpu: "200m"
Check for throttling:
kubectl top pod bursty-cpu-pod # usage stays pinned at about 200m
# kubectl describe does not show throttling; with Prometheus:
# rate(container_cpu_cfs_throttled_periods_total{pod="bursty-cpu-pod"}[5m])
# / rate(container_cpu_cfs_periods_total{pod="bursty-cpu-pod"}[5m])β Challenge 9: Monitor metrics using metrics-server or Prometheus
Ensure metrics-server is deployed:
kubectl top pod
Or use Prometheus:
kubectl get svc -n monitoring | grep prometheus # find the Prometheus service name
kubectl port-forward svc/<prometheus-service> 9090:9090 -n monitoringQuery:
container_memory_working_set_bytes{container!=""} # what the OOM killer compares to the limit
rate(container_cpu_usage_seconds_total{container!=""}[5m]) # CPU cores in use (the metric is a counter)
# share of CPU periods that were throttled
rate(container_cpu_cfs_throttled_periods_total[5m]) / rate(container_cpu_cfs_periods_total[5m])β Challenge 10: Test HPA behavior with different request values
apiVersion: apps/v1
kind: Deployment
metadata:
name: hpa-demo
spec:
replicas: 1
selector:
matchLabels:
app: hpa-demo
template:
metadata:
labels:
app: hpa-demo
spec:
containers:
- name: app
image: vish/stress
args: ["--cpu", "1"]
resources:
requests:
cpu: "200m"
limits:
cpu: "500m"
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hpa-demo
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: hpa-demo
minReplicas: 1
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
Monitor scaling:
kubectl get hpa
Challenge 11: Final Project β Resource-Constrained Application Environment
This challenge is about simulating a constrained Kubernetes environment using LimitRanges and ResourceQuotas to observe and analyze behaviors like OOMKilled, throttling, and failed scheduling.
β Step 1: Create Namespace
kubectl create ns test-env
β Step 2: Apply LimitRange
Apply a default per-container resource LimitRange.
# limitrange.yaml
apiVersion: v1
kind: LimitRange
metadata:
name: container-limits
namespace: test-env
spec:
limits:
- default:
cpu: "500m"
memory: "256Mi"
defaultRequest:
cpu: "200m"
memory: "128Mi"
type: Container
kubectl apply -f limitrange.yaml
β Step 3: Apply ResourceQuota
Restrict total CPU and memory across the namespace.
# resourcequota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-quota
namespace: test-env
spec:
hard:
requests.cpu: "1"
requests.memory: "512Mi"
limits.cpu: "2"
limits.memory: "1Gi"
kubectl apply -f resourcequota.yaml
β Step 4: Deploy Resource-Aware Apps
4.1 App A
# app-a.yaml
apiVersion: v1
kind: Pod
metadata:
name: app-a
namespace: test-env
spec:
containers:
- name: app
image: nginx
resources:
requests:
cpu: "200m"
memory: "128Mi"
limits:
cpu: "300m"
memory: "256Mi"
4.2 App B
# app-b.yaml
apiVersion: v1
kind: Pod
metadata:
name: app-b
namespace: test-env
spec:
containers:
- name: app
image: nginx
resources:
requests:
cpu: "300m"
memory: "128Mi"
limits:
cpu: "400m"
memory: "256Mi"
4.3 App C
# app-c.yaml
apiVersion: v1
kind: Pod
metadata:
name: app-c
namespace: test-env
spec:
containers:
- name: app
image: nginx
resources:
requests:
cpu: "200m"
memory: "128Mi"
limits:
cpu: "400m"
memory: "256Mi"
kubectl apply -f app-a.yaml
kubectl apply -f app-b.yaml
kubectl apply -f app-c.yaml
β Step 5: Deploy a Pod That Exceeds Quota
# app-over-quota.yaml
apiVersion: v1
kind: Pod
metadata:
name: app-over-quota
namespace: test-env
spec:
containers:
- name: app
image: nginx
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
cpu: "600m"
memory: "768Mi"
kubectl apply -f app-over-quota.yaml
Expect kubectl apply to fail with Error from server (Forbidden): ... exceeded quota. Quota is checked at admission, so the pod is never created; Pending is what you see when no node has room, not when a quota is exceeded.
β Step 6: Analyze Pod Behavior
Check Pod Status:
kubectl get pods -n test-env
Inspect the quota:
kubectl describe resourcequota compute-quota -n test-env # used vs hard limits
# app-over-quota was never created, so there is no pod to describe:
# the reason is in the kubectl apply error ("exceeded quota")Check for OOMKilled / Throttled:
kubectl get pod app-a -n test-env -o json | jq '.status.containerStatuses[].lastState.terminated.reason'
Or:
kubectl describe pod <pod-name> -n test-env | grep -i "oomkilled"
Monitor Usage:
kubectl top pod -n test-env
β Summary of Findings
- LimitRange applied default resource limits and requests to all pods that didn't define their own.
- ResourceQuota enforced an upper cap on total compute usage.
- The API server rejected app-over-quota at admission with an "exceeded quota" error, so the pod was never created.
- If a pod exceeds its memory limit during execution, it is OOMKilled.
- CPU bursts over limits are throttled (can be observed with
kubectl top).
β GitHub Push
- Add all YAML files:
git add *.yaml
- Commit and push:
git commit -m "Challenge 11: Resource-Constrained App Env"
git push origin main
π Bonus Tip
Use kubectl explain for LimitRange and ResourceQuota to understand the spec fields better:
kubectl explain limitrange.spec.limits
kubectl explain resourcequota.spec.hard
π€·π» How to Participate?
β
Complete the tasks and challenges.
β
Document your progress and key takeaways on GitHub ReadMe, Medium, or Hashnode.
β
Share the above in a LinkedIn post tagging me (Sandip Das), and use #60DaysOfDevOps to engage with the community!
β‘ Quick Reference
Keep the momentum going β everything you need on LearnXOps in one place:
- π Courses β Video courses & AI-built courses
- π Ebooks β Community ebooks & your own
- π§ͺ All Labs & Challenges β DevOps, Cloud, SRE & more
- βΈοΈ Kubernetes Labs & Playground β kind clusters, playground & challenges
- π€ AI Agent & Bot Labs β OpenClaw Β· Hermes Β· CrewAI Β· LangGraph Β· MCP

Member discussion