Clusters in Kubernetes have limited hardware resources available to them. Resource quotas in Kubernetes allow administrators to control resource usage per
First, prepare a YAML file to configure resource limits and requests. Below is a demo YAML file that limits CPU usage:
apiVersion: v1kind: ResourceQuotametadata:name: cpu-quotanamespace: quota-testspec:hard:requests.cpu: 200mlimits.cpu: 400m
Then, we create a namespace where we’ll reserve the specified CPU quota, and create the ResourceQuota
object:
kubectl create namespace quota-testkubectl create -f cpu-quota.yaml
To test this quota, we’re going to make some test pods on this namespace:
apiVersion: v1kind: Podmetadata:name: demopodspec:containers:- name: resource-testimage: busyboximagePullPolicy: IfNotPresentcommand:- sh- -c- echo Pod now Running ; sleep 5000resources:requests:cpu: 100mlimits:cpu: 200mrestartPolicy: Never
We use the following command to create a pod from a YAML file:
kubectl create -n quota-test -f demopod.yaml
Using the following command, we can see now that some amount of resources are being used within our defined limits:
kubectl describe resourcequota/cpu-quota --namespace quota-test
Now we’re going to create a similar pod that will exceed the total CPU requests limit.
apiVersion: v1kind: Podmetadata:name: exceedpodspec:containers:- name: resource-testimage: busyboximagePullPolicy: IfNotPresentcommand:- sh- -c- echo Pod now Running ; sleep 5000resources:requests:cpu: 150mlimits:cpu: 200mrestartPolicy: Never
We’ll try to create a pod as before:
kubectl create -n quota-test -f exceedpod.yaml
However, when we try to create this pod, it fails because the requested resources exceed our allocated CPU usage.
Below is an interactive folder and terminal where you can try out this demo and the commands given above. All required files are already there, so just click the "Run" button, and the terminal will open and set up a Kubernetes cluster for you to test with.
apiVersion: v1 kind: ResourceQuota metadata: name: cpu-quota namespace: quota-test spec: hard: requests.cpu: 200m limits.cpu: 400m
Setting up a resource quota in Kubernetes simply requires defining a namespace and a YAML file that assigns a quota to that namespace. Pods that are then run under that namespace will be limited to the quota that particular namespace has been defined with.
Unlock your potential: Kubernetes Deployment and Advanced Operations series, all in one place!
To deepen your understanding of Kubernetes deployment and management, explore our series of Answers below:
How to create a Kubernetes cluster locally with Minikube
Learn how to set up a Kubernetes cluster on your local machine using Minikube for development and testing.
How to create deployment on a Kubernetes cluster via YAML files
Master the process of defining and managing deployments in Kubernetes using YAML configuration files.
What is an Ingress controller in Kubernetes?
Understand the role of an Ingress controller in managing external access and routing traffic within a Kubernetes cluster.
How to implement namespace quotas in Kubernetes
Discover how to apply resource limits to specific namespaces to optimize resource allocation and prevent overuse.
How to implement Kubernetes resource quotas
Learn how to enforce limits on CPU, memory, and storage consumption across your Kubernetes cluster.
Add users using certificates in a Kubernetes cluster
Explore the process of adding and authenticating users in Kubernetes by issuing and managing certificates.
What is service discovery in Kubernetes?
Understand how Kubernetes enables seamless service discovery, allowing applications to communicate efficiently.
How to put a database in Kubernetes
Learn the best practices for deploying and managing databases within a Kubernetes environment.
How to run WordPress on Kubernetes
Follow a step-by-step guide to deploying a scalable and resilient WordPress site on Kubernetes.
Free Resources