How to implement Kubernetes resource quotas

Clusters in Kubernetes have limited hardware resources available to them. Resource quotas in Kubernetes allow administrators to control resource usage per namespaceA pre-defined group that exists for isolation of resources and objects within a cluster., ensuring efficient cluster management. Here’s how to set up resource quotas in Kubernetes:

Setting the quota

First, prepare a YAML file to configure resource limits and requests. Below is a demo YAML file that limits CPU usage:

apiVersion: v1
kind: ResourceQuota
metadata:
name: cpu-quota
namespace: quota-test
spec:
hard:
requests.cpu: 200m
limits.cpu: 400m
A ResourceQuota with a requests quota for the given namespace of 200m and a total limit quota of 400m

Then, we create a namespace where we’ll reserve the specified CPU quota, and create the ResourceQuota object:

kubectl create namespace quota-test
kubectl create -f cpu-quota.yaml

Creating some pods

To test this quota, we’re going to make some test pods on this namespace:

apiVersion: v1
kind: Pod
metadata:
name: demopod
spec:
containers:
- name: resource-test
image: busybox
imagePullPolicy: IfNotPresent
command:
- sh
- -c
- echo Pod now Running ; sleep 5000
resources:
requests:
cpu: 100m
limits:
cpu: 200m
restartPolicy: Never
A pod requesting 100m of resources with an upper limit of 200m

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
The current resource usage is still within the pre-defined limits
The current resource usage is still within the pre-defined limits

Now we’re going to create a similar pod that will exceed the total CPU requests limit.

apiVersion: v1
kind: Pod
metadata:
name: exceedpod
spec:
containers:
- name: resource-test
image: busybox
imagePullPolicy: IfNotPresent
command:
- sh
- -c
- echo Pod now Running ; sleep 5000
resources:
requests:
cpu: 150m
limits:
cpu: 200m
restartPolicy: Never
This pod requests 150m of resources.

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.

Requested resources in the new pod exceed our limit, so the server returns an error
Requested resources in the new pod exceed our limit, so the server returns an error

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

Conclusion

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.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved