DEV Community

Cover image for 🎯 Scenario #11 β€” Set Resource Requests and Limits for CPU/Memory in Kubernetes Pods
Latchu@DevOps
Latchu@DevOps

Posted on

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

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
Enter fullscreen mode Exit fullscreen mode

Apply:

kubectl apply -f resources-pod.yaml
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Step 2 β€” Verify Pod Status

kubectl get pod resource-demo -o wide
Enter fullscreen mode Exit fullscreen mode

Pod should be running.

1


πŸ” Step 3 β€” Inspect Resource Configuration

Run:

kubectl describe pod resource-demo
Enter fullscreen mode Exit fullscreen mode

Look for:

Limits:
  cpu:     500m
  memory:  256Mi
Requests:
  cpu:     100m
  memory:  128Mi
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Check:

kubectl get pod resource-demo
kubectl describe pod resource-demo | grep -i oom
Enter fullscreen mode Exit fullscreen mode

You'll see:

OOMKilled
Enter fullscreen mode Exit fullscreen mode

3


πŸ’» Step 5 β€” Clean Up

kubectl delete pod resource-demo
Enter fullscreen mode Exit fullscreen mode

🌟 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

Top comments (0)