kubernetesJuly 24, 20265 min read

Kubernetes Resource Limits and Requests - Complete Guide | DevOps Duoo

Kubernetes resource limits and requests are essential components of cluster management. They help you allocate resources efficiently, prevent overcommit...

Kubernetes Resource Limits and Requests - Complete Guide

TL;DR

  • Kubernetes resource limits and requests are crucial for managing pod resources, ensuring efficient utilization, and preventing overcommitment.
  • You'll learn how to set CPU and memory limits, configure resource quotas, and define limit ranges to optimize your cluster's performance.
  • By the end of this guide, you'll understand how to implement quality of service (QoS) classes and troubleshoot common issues related to resource management.

What You'll Learn

Kubernetes resource limits and requests are essential components of cluster management. They help you allocate resources efficiently, prevent overcommitment, and ensure that your pods are running smoothly. In this guide, you'll learn about the different types of resource limits, how to configure them, and how to troubleshoot common issues. You'll also discover how to use resource quotas, limit ranges, and QoS classes to optimize your cluster's performance.

Understanding Resource Limits and Requests

Kubernetes provides two types of resource limits: requests and limits. requests define the amount of resources a pod is guaranteed to get, while limits define the maximum amount of resources a pod can use.

Setting CPU and Memory Limits

To set CPU and memory limits, you can use the resources section in your pod's configuration file. For example:
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: example-container
    image: example-image
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 200m
        memory: 256Mi
In this example, the example-container is guaranteed to get 100m CPU and 128Mi memory, but it can use up to 200m CPU and 256Mi memory.

Configuring Resource Quotas

Resource quotas allow you to limit the total amount of resources used by a namespace. You can create a resource quota using the following command:
kubectl create resourcequota example-quota --hard cpu=1,memory=1Gi --namespace example-namespace
This command creates a resource quota named example-quota in the example-namespace namespace, with a hard limit of 1 CPU and 1Gi memory.

Defining Limit Ranges

Limit ranges allow you to define default resource limits for pods in a namespace. You can create a limit range using the following command:
kubectl create limitrange example-limitrange --min cpu=100m,memory=128Mi --max cpu=200m,memory=256Mi --namespace example-namespace
This command creates a limit range named example-limitrange in the example-namespace namespace, with a minimum limit of 100m CPU and 128Mi memory, and a maximum limit of 200m CPU and 256Mi memory.

Implementing Quality of Service (QoS) Classes

Kubernetes provides three QoS classes: Guaranteed, Burstable, and BestEffort. You can use these classes to prioritize pods and ensure that critical pods get the resources they need.

Guaranteed QoS Class

The Guaranteed QoS class is the highest priority class. Pods with this class are guaranteed to get the resources they request. To use this class, you need to set the requests and limits to the same value.
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: example-container
    image: example-image
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 100m
        memory: 128Mi

Burstable QoS Class

The Burstable QoS class is the middle priority class. Pods with this class can use more resources than they request, but they are not guaranteed to get the resources they need. To use this class, you need to set the requests to a lower value than the limits.
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: example-container
    image: example-image
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 200m
        memory: 256Mi

BestEffort QoS Class

The BestEffort QoS class is the lowest priority class. Pods with this class do not have any resource guarantees and can be terminated at any time. To use this class, you do not need to set any resource requests or limits.
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: example-container
    image: example-image

Troubleshooting Common Issues

Common issues related to resource management include:
  • Overcommitment: When the total amount of resources requested by pods exceeds the available resources.
  • Underutilization: When the total amount of resources used by pods is less than the available resources.
  • Resource starvation: When a pod is unable to get the resources it needs due to other pods using more resources than they need.
To troubleshoot these issues, you can use the following commands:
kubectl top pod
kubectl describe pod
kubectl get pod -o yaml
You can also use to monitor your cluster's resources and identify potential issues.

Common Mistakes

Common mistakes related to resource management include:
  • Not setting resource requests and limits for pods.
  • Not configuring resource quotas and limit ranges.
  • Not using QoS classes to prioritize pods.
To avoid these mistakes, make sure to:
  • Set resource requests and limits for all pods.
  • Configure resource quotas and limit ranges for all namespaces.
  • Use QoS classes to prioritize critical pods.

Key Takeaways

  • Kubernetes resource limits and requests are crucial for managing pod resources and preventing overcommitment.
  • Resource quotas, limit ranges, and QoS classes can be used to optimize cluster performance and prioritize critical pods.
  • Common issues related to resource management include overcommitment, underutilization, and resource starvation, and can be troubleshooted using various commands and tools.
  • By following best practices and avoiding common mistakes, you can ensure efficient resource utilization and high performance in your Kubernetes cluster.
  • For more information on related topics, check out and .

Share this article

← Back to Blog

Related Articles