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)