DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on

Project: Resource Limits & OOMKill in Kubernetes

Architecture

User → Pod (stress container)
        ├─ CPU stress
        └─ Memory stress
Enter fullscreen mode Exit fullscreen mode

1️⃣ Create Namespace

kubectl create namespace limits-lab
Enter fullscreen mode Exit fullscreen mode

2️⃣ Deployment with STRICT Limits

file: stress-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: stress-app
  namespace: limits-lab
spec:
  replicas: 1
  selector:
    matchLabels:
      app: stress
  template:
    metadata:
      labels:
        app: stress
    spec:
      containers:
      - name: stress
        image: polinux/stress
        resources:
          requests:
            memory: "64Mi"
            cpu: "100m"
          limits:
            memory: "128Mi"
            cpu: "200m"
        command:
        - /bin/sh
        - -c
        - |
          echo "Starting stress..."
          stress --vm 1 --vm-bytes 200M --cpu 2 --timeout 300
Enter fullscreen mode Exit fullscreen mode

Apply it:

kubectl apply -f stress-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

3️⃣ Observe Pod Behavior

kubectl get pods -n limits-lab -w
Enter fullscreen mode Exit fullscreen mode

Expected result

STATUS: OOMKilled → Restarting
Enter fullscreen mode Exit fullscreen mode

4️⃣ Explain What Happened (IMPORTANT)

Memory

  • Limit = 128Mi
  • App tried to allocate 200Mi
  • Linux kernel kills container
  • Kubernetes restarts pod
kubectl describe pod <pod-name> -n limits-lab
Enter fullscreen mode Exit fullscreen mode

You will see:

Reason: OOMKilled
Exit Code: 137
Enter fullscreen mode Exit fullscreen mode

5️⃣ CPU Throttling (No Restart)

Update deployment:

stress --cpu 2 --timeout 300
Enter fullscreen mode Exit fullscreen mode

Then watch:

kubectl top pod -n limits-lab
Enter fullscreen mode Exit fullscreen mode

Key learning

  • CPU limit does NOT kill pod
  • Kernel throttles CPU via CFS
  • App becomes slow but stays Running

6️⃣ Remove Memory Limit (Compare)

Change:

limits:
  cpu: "200m"
Enter fullscreen mode Exit fullscreen mode

(no memory limit)

Result

  • Pod does NOT get OOMKilled
  • Can consume large memory
  • Node risk increases

7️⃣ Interview-Ready Explanation

Requests

  • Used by scheduler
  • Guarantees minimum resources
  • Pod may run without them

Limits

  • Enforced by kernel
  • Memory limit → OOMKill
  • CPU limit → Throttling

8️⃣ Production Scenario (SRE Question)

Q: Pod restarts randomly, no app logs
A:

  • Check kubectl describe pod
  • Look for OOMKilled
  • Compare memory usage vs limit
  • Increase limit or fix memory leak

9️⃣ Bonus: HPA Interaction

Why HPA fails sometimes:

  • HPA scales on requests
  • Low CPU request + high usage = aggressive scaling

10️⃣ Cleanup

kubectl delete namespace limits-lab
Enter fullscreen mode Exit fullscreen mode

Final Summary (Say This in Interviews)

“Kubernetes requests affect scheduling, limits affect runtime.
Memory limit violations cause OOMKills and restarts,
CPU limits cause throttling without restarts.
I debug resource issues using describe, events, and metrics.”

Top comments (0)