Skip to main content

Command Palette

Search for a command to run...

πŸš€ Set Resource Limits in Kubernetes Pods

Published
β€’3 min read
πŸš€ Set Resource Limits in Kubernetes Pods
B

I am Bittu Sharma, a DevOps & AI Engineer with a keen interest in building intelligent, automated systems. My goal is to bridge the gap between software engineering and data science, ensuring scalable deployments and efficient model operations in production.! π—Ÿπ—²π˜'π˜€ π—–π—Όπ—»π—»π—²π—°π˜ I would love the opportunity to connect and contribute. Feel free to DM me on LinkedIn itself or reach out to me at bittush9534@gmail.com. I look forward to connecting and networking with people in this exciting Tech World.

Resource management is one of the most important aspects of running applications in Kubernetes. By setting CPU and memory requests and limits, you ensure fair resource usage and prevent a single Pod from consuming all node resources.

In this guide, we’ll create an Apache HTTPD Pod with defined CPU and memory requests and limits.


🧠 Task Overview

We need to create a Kubernetes Pod with the following specifications:

ConfigurationValue
Pod Namehttpd-pod
Container Namehttpd-container
Imagehttpd:latest
CPU Request100m
Memory Request15Mi
CPU Limit100m
Memory Limit20Mi

ℹ️ Note: kubectl is already configured to interact with the Kubernetes cluster.


πŸ“Œ Why Resource Requests and Limits Matter

  • Requests define the minimum resources guaranteed to a Pod

  • Limits define the maximum resources a Pod can consume

  • Prevents resource starvation

  • Improves cluster stability and performance


🧩 Step 1: Create the Pod YAML File

Create a file named httpd-pod.yaml.

πŸ“„ Create the file

vi httpd-pod.yaml

πŸ“Œ Pod Definition

apiVersion: v1
kind: Pod
metadata:
  name: httpd-pod
spec:
  containers:
  - name: httpd-container
    image: httpd:latest
    resources:
      requests:
        memory: "15Mi"
        cpu: "100m"
      limits:
        memory: "20Mi"
        cpu: "100m"

πŸ” YAML Explanation

resources:
  requests:
    memory: "15Mi"   # Minimum memory guaranteed
    cpu: "100m"      # Minimum CPU guaranteed (0.1 core)
  limits:
    memory: "20Mi"   # Maximum memory allowed
    cpu: "100m"      # Maximum CPU allowed
  • Mi = Mebibytes

  • 100m CPU = 0.1 CPU core


πŸš€ Step 2: Apply the Pod Configuration

Deploy the Pod to the Kubernetes cluster:

kubectl apply -f httpd-pod.yaml

βœ… Expected Output

pod/httpd-pod created

πŸ”Ž Step 3: Verify the Pod

Check the Pod status:

kubectl get pod httpd-pod

🟒 Sample Output

NAME        READY   STATUS    RESTARTS   AGE
httpd-pod   1/1     Running   0          20s

🧾 Step 4: Verify Resource Limits

Describe the Pod and inspect resource limits:

kubectl describe pod httpd-pod | grep -A5 "Limits"

🟒 Sample Output

Limits:
  cpu:     100m
  memory:  20Mi
Requests:
  cpu:     100m
  memory:  15Mi

πŸ› οΈ Common Troubleshooting Tips

❌ Pod stuck in Pending

kubectl describe pod httpd-pod

βœ”οΈ Node may not have sufficient resources
βœ”οΈ Requests exceed available capacity


❌ Pod gets OOMKilled

βœ”οΈ Increase memory limit
βœ”οΈ Optimize application memory usage


🎯 Conclusion

In this guide, you learned how to:

  • Define CPU and memory requests

  • Set resource limits for a Kubernetes Pod

  • Deploy and verify a resource-constrained Pod

  • Inspect Pod resource configurations

Setting resource limits is a best practice in production Kubernetes clusters to ensure reliability and fair scheduling.


πŸš€ Next Steps

Here are some excellent follow-up topics:

  • Resource Quotas per Namespace

  • LimitRange in Kubernetes

  • Horizontal Pod Autoscaler (HPA)

  • Monitoring resource usage with metrics-server