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.
Free Resources