DEV Community

Cover image for Understanding Resource Requests and Limits in Kubernetes
Jensen Jose
Jensen Jose

Posted on

Understanding Resource Requests and Limits in Kubernetes

Welcome back to the CK 2024 blog series! In this post, we’ll delve into the critical concept of resource requests and limits in Kubernetes, a mechanism the scheduler uses to allocate pods to nodes efficiently. If you missed any of the previous posts in this series, I recommend checking those out to build a strong foundation.

Recap

In the previous blog in this series, we explored how Kubernetes uses resource requests and limits to manage pod scheduling and ensure optimal resource utilization across nodes. Let’s break down this concept and see it in action through examples and exercises.

Why Resource Requests and Limits Matter

In Kubernetes, each pod requires a certain amount of CPU and memory to run. Without proper resource allocation, a pod can monopolize node resources, leading to performance issues and potential crashes. This is where resource requests and limits come into play:

  • Resource Requests: The minimum amount of CPU and memory guaranteed to the pod.
  • Resource Limits: The maximum amount of CPU and memory the pod can use.

How It Works

  1. Node Specifications:
  • Node 1: 4 CPUs, 4 GB of memory.
  • Node 2: 4 CPUs, 4 GB of memory.
  1. Pod Scheduling:
  • Each pod requests 1 CPU and 1 GB of memory.
  • The scheduler checks if the node has sufficient resources.
  • If the node has enough resources, the pod is scheduled.
  • Once the node is full, the scheduler moves to the next node.
  • If no nodes have sufficient resources, the pod remains unscheduled.

Example Scenario

Let’s consider a node with 4 CPUs and 4 GB of memory. We have a pod that requires 1 CPU and 1 GB of memory. Initially, the pod is allocated the requested resources. However, if the load increases, the pod might try to consume all available resources, leading to potential crashes. To prevent this, we set resource limits.

Setting Resource Requests and Limits

Here’s a YAML configuration for a pod with resource requests and limits:

apiVersion: v1
kind: Pod
metadata:
  name: memory-demo
spec:
  containers:
  - name: memory-demo-ctr
    image: polinux/stress
    resources:
      requests:
        memory: "100Mi"
      limits:
        memory: "200Mi"
    command: ["stress"]
    args: ["--vm", "1", "--vm-bytes", "150M", "--vm-hang", "1"]
Enter fullscreen mode Exit fullscreen mode
  • Requests: 100 Mi of memory.
  • Limits: 200 Mi of memory.
  • Command: The pod will use 150 Mi of memory, staying within the defined limits.

Demonstration

  1. Apply the YAML:
kubectl apply -f memory-demo.yaml
Enter fullscreen mode Exit fullscreen mode
  1. Check the Pod:
kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Verify that the pod is running within the specified limits.

  1. Stress Testing:
  • Increase the pod’s memory usage beyond the limit to observe behavior.
  • The pod will be terminated with an Out of Memory (OOM) error if it exceeds the limit.

Conclusion

Resource requests and limits are essential for maintaining the stability and performance of your Kubernetes cluster. By defining these boundaries, you ensure that pods do not consume more resources than allowed, preventing potential node failures and ensuring a smooth operation.

Remember to practice these configurations and refer to the Kubernetes documentation for further details. In the next post, we’ll explore autoscaling and how it leverages these metrics for efficient resource management.

For further reference, check out the detailed YouTube video here:

Top comments (0)