🎯 Scenario #11 β€” Set Resource Requests and Limits for CPU/Memory in Kubernetes Pods



This content originally appeared on DEV Community and was authored by Latchu@DevOps

In this scenario, you will:

  • Create a Pod with CPU/memory requests (minimum guaranteed)
  • Set limits (maximum allowed)
  • Verify how Kubernetes schedules and restricts resource usage
  • Inspect assigned resources in running Pod

This is a core Kubernetes skill that every DevOps/SRE must master.

✅ Step 1 β€” Create a Pod With Resource Requests & Limits

Create file:

# resources-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: resource-demo
spec:
  containers:
  - name: nginx
    image: nginx:1.25
    resources:
      requests:
        memory: "128Mi"
        cpu: "100m"       # 0.1 CPU core
      limits:
        memory: "256Mi"
        cpu: "500m"       # 0.5 CPU core
    ports:
    - containerPort: 80

Apply:

kubectl apply -f resources-pod.yaml

🧪 Step 2 β€” Verify Pod Status

kubectl get pod resource-demo -o wide

Pod should be running.

1

🔍 Step 3 β€” Inspect Resource Configuration

Run:

kubectl describe pod resource-demo

Look for:

Limits:
  cpu:     500m
  memory:  256Mi
Requests:
  cpu:     100m
  memory:  128Mi

2

This confirms:

βœ“ Requests = Minimum guaranteed
βœ“ Limits = Maximum allowed

💡 What This Means in Real Life

  • Kubernetes schedules the Pod only on nodes that have 128Mi RAM + 100m CPU free
  • The Pod cannot exceed 256Mi RAM or it will be OOMKilled
  • The Pod cannot use more than 500m CPU β€” throttling will occur

🧪 Step 4 β€” Test OOMKill (Optional Challenge)

Exec into the container:

kubectl exec -it resource-demo -- bash

Run a memory stress test (if you have stress inside containerβ€”it won’t be there in nginx).

So instead run a simple memory loop:

head -c 300M </dev/zero | tail

Pod will exceed 256Mi β†’ Kubernetes will kill it with OOMKilled.

Check:

kubectl get pod resource-demo
kubectl describe pod resource-demo | grep -i oom

You’ll see:

OOMKilled

3

💻 Step 5 β€” Clean Up

kubectl delete pod resource-demo

🌟 Thanks for reading! If this post added value, a like ❤, follow, or share would encourage me to keep creating more content.

β€” Latchu | Senior DevOps & Cloud Engineer

☁ AWS | GCP | ☸ Kubernetes | 🔐 Security | ⚡ Automation
📌 Sharing hands-on guides, best practices & real-world cloud solutions


This content originally appeared on DEV Community and was authored by Latchu@DevOps