How to Set Resource Quota and Limits in Kubernetes

How to Set Resource Quota and Limits in Kubernetes

Kubernetes is an open-source container orchestration system used to manage containerized applications at scale. Resource management is a critical aspect of Kubernetes, ensuring that the application's resources are optimally used. In Kubernetes, Resource Quotas and Limits can be used to control the amount of CPU and memory resources used by pods in a namespace.

This article will guide you on how to set Resource Quotas and Limits in Kubernetes.

Step 1: Create a Namespace

To begin, create a namespace using the following command:

kubectl create namespace <namespace-name>

Step 2: Create a Resource Quota

To create a Resource Quota for the namespace, use the following command:

kubectl create quota <resource-quota-name> --hard=<cpu-limit>cpu,<memory-limit>memory --namespace=<namespace-name>

For example, to set a Resource Quota with a CPU limit of 1 CPU and a memory limit of 1Gi, use the following command:

kubectl create quota my-quota --hard=1cpu,1Gi --namespace=my-namespace

Step 3: Create a Limit Range

To create a Limit Range for the namespace, use the following command:

kubectl create -f <limit-range-definition-file> --namespace=<namespace-name>

A Limit Range can be defined in a YAML file, as shown below:

apiVersion: v1
kind: LimitRange
metadata:
name: my-limit-range
spec:
limits:
- max:
cpu: 2
memory: 2Gi
min:
cpu: 200m
memory: 100Mi
type: Pod

In the above example, the Limit Range specifies that a pod can use a maximum of 2 CPUs and 2Gi of memory, and a minimum of 200m CPU and 100Mi memory.

Step 4: Apply Resource Quota and Limit Range to a Pod

To apply the Resource Quota and Limit Range to a pod, add the following annotations to the pod's YAML file:

apiVersion: v1
kind: Pod
metadata:
name: my-pod
annotations:
quota.openshift.io/my-quota: "true"
limits.cpu: "1"
limits.memory: "1Gi"

In the above example, the pod will use a maximum of 1 CPU and 1Gi of memory, as specified by the Resource Quota. The Limit Range is used to ensure that the pod does not exceed the specified CPU and memory limits.

More Examples:

  • To view the Resource Quota for a namespace, use the following command:
kubectl describe quota --namespace=<namespace-name>
  • To view the Limit Range for a namespace, use the following command:
kubectl describe limitrange --namespace=<namespace-name>

I hope this article has provided you with a better understanding of how to set Resource Quota and Limits in Kubernetes. By following these steps, you can ensure that your application's resources are optimally used, improving its performance and reliability.

Related Searches and Questions asked:

  • How to Use Kubernetes Probes - Liveness, Readiness and Startup
  • Understanding Kubernetes Probes: Liveness, Readiness, and Startup
  • Kubernetes Pod Graceful Shutdown
  • What is Kubernetes DaemonSet?
  • That's it for this post. Keep practicing and have fun. Leave your comments if any.

    Post a Comment

    0 Comments