Configure quotas for a project so that applications cannot scale to consume all capacity of a cluster node.
Outcomes
Verify that requesting resources in one namespace can prevent creation of workloads in different namespaces.
Set a quota to prevent workloads in a namespace from requesting excessive resources.
Verify that you can continue to create workloads in different namespaces.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that the cluster API is reachable and deletes the namespaces that you use in this exercise.
[student@workstation ~]$ lab start selfservice-quotas
Instructions
Log in to your OpenShift cluster as the developer user with the developer password.
Log in to the cluster as the developer user.
[student@workstation ~]$ oc login -u developer -p developer \
https://api.ocp4.example.com:6443
Login successful.
...output omitted...Create a selfservice-quotas project.
Use the oc new-project command to create the project.
[student@workstation ~]$ oc new-project selfservice-quotas
Now using project "selfservice-quotas" on server "https://api.ocp4.example.com:6443".
...output omitted...Create a deployment with a container that requests one CPU.
Use the oc create command to create the deployment.
[student@workstation ~]$ oc create deployment test \
--image registry.ocp4.example.com:8443/redhattraining/hello-world-nginx
deployment.apps/test createdUse the oc set resources command to request one CPU in the container specification.
[student@workstation ~]$ oc set resources deployment test --requests=cpu=1
deployment.apps/test resource requirements updatedUse the oc get command to ensure that the deployment starts a pod correctly.
[student@workstation ~]$ oc get pod,deployment
NAME READY STATUS RESTARTS AGE
pod/test-8b9fdfbd9-bltlc 1/1 Running 0 13s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/test 1/1 1 1 49sExecute the command until the deployment and the pod are ready.
Try to scale the deployment to eight replicas.
Use the oc scale command to scale the deployment.
[student@workstation ~]$ oc scale deployment test --replicas=8
deployment.apps/test scaledUse the oc get command to view pods and deployments.
[student@workstation ~]$ oc get pod,deployment
NAME READY STATUS RESTARTS AGE
pod/test-6c66b55cb5-2kclt 1/1 Running 0 48m
pod/test-6c66b55cb5-5n58r 0/1 Pending 0 5s
pod/test-6c66b55cb5-8x929 0/1 Pending 0 5s
pod/test-6c66b55cb5-blgms 0/1 Pending 0 5s
pod/test-6c66b55cb5-d6z42 1/1 Running 0 6s
pod/test-6c66b55cb5-fc8bk 0/1 Pending 0 5s
pod/test-6c66b55cb5-t29dh 0/1 Pending 0 6s
pod/test-6c66b55cb5-xqr66 0/1 Pending 0 6s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/test 2/8 8 2 54mOut of eight pods that the deployment creates, only some of them change to Running status.
The other pods stay in Pending status.
Not all replicas of the deployment are ready and available.
Use the oc get command to list events.
Sort the events by their creation timestamp.
[student@workstation ~]$oc get event --sort-by .metadata.creationTimestampLAST SEEN TYPE REASON OBJECT MESSAGE ...output omitted... 3m58s Normal ScalingReplicaSet deployment/test Scaled up replica set test-6c66b55cb5 to 8 3m58s Normal Scheduled pod/test-6c66b55cb5-d6z42 Successfully assigned selfservice-quotas/test-6c66b55cb5-d6z42 to master01 3m57sWarning FailedSchedulingpod/test-6c66b55cb5-5n58r 0/1 nodes are available: 1Insufficient cpu. preemption: 0/1 nodes are available:1 No preemption victims found for incoming pod.. ...output omitted...
Replicas fail to schedule, because the cluster has insufficient CPU.
Examine the cluster as an administrator.
Log in to the cluster as the admin user with the redhatocp password.
[student@workstation ~]$ oc login -u admin -p redhatocp
Login successful.
...output omitted...Use the oc adm top command to display the resource usage of nodes.
[student@workstation ~]$ oc adm top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
master01 772m 14% 10185Mi 68%The cluster does not show high CPU usage.
Use the oc describe command to view the node details.
[student@workstation ~]$oc describe node/master01Name: master01 ...output omitted... Capacity:cpu: 6...output omitted... Allocatable:cpu: 5500m...output omitted... Allocated resources: (Total limits may be over 100 percent, i.e., overcommitted.) Resource Requests Limits -------- -------- ------ cpu4627m (84%)0 (0%) memory 12102Mi (81%) 0 (0%) ephemeral-storage 0 (0%) 0 (0%) hugepages-1Gi 0 (0%) 0 (0%) hugepages-2Mi 0 (0%) 0 (0%) ...output omitted...
The node has a capacity of six CPUs, and has more than five allocatable CPUs. However, over five CPUs are requested, so less than one CPU is available for new workloads.
Create a test project as an administrator, and verify that you cannot create new workloads that request a CPU.
Use the oc new-project command to create the project.
[student@workstation ~]$ oc new-project test
Now using project "test" on server "https://api.ocp4.example.com:6443".
...output omitted...Use the oc create command to create the deployment.
[student@workstation ~]$ oc create deployment test \
--image registry.ocp4.example.com:8443/redhattraining/hello-world-nginx
deployment.apps/test createdUse the oc set resources command to request one CPU in the container specification.
[student@workstation ~]$ oc set resources deployment test --requests=cpu=1
deployment.apps/test resource requirements updatedUse the oc get command to review the pods and deployments in the test namespace.
[student@workstation ~]$ oc get pod,deployment
NAME READY STATUS RESTARTS AGE
pod/test-8b9fdfbd9-rrn7t 0/1 Pending 0 8s
pod/test-c454765f-vkt96 1/1 Running 0 100s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/test 1/1 1 1 100sThe deployment created one pod before adding the CPU request.
When you updated the deployment to request a CPU, the deployment tried to replace the pod to add the CPU request.
The new pod is in the Pending state, because the cluster has less than one CPU available to request.
The workload in the selfservice-quotas namespace prevents the creation of workloads in other namespaces.
Use the oc delete command to delete the test namespace.
[student@workstation ~]$ oc delete namespace test
namespace "test" deletedAs an administrator, scale the deployment to one replica.
Use the oc project command to switch to the selfservice-quotas project.
[student@workstation ~]$ oc project selfservice-quotas
Now using project "selfservice-quotas" on server "https://api.ocp4.example.com:6443".Use the oc scale command to scale the test deployment to one replica.
[student@workstation ~]$ oc scale deployment test --replicas=1
deployment.apps/test scaledCreate a quota to prevent workloads in the selfservice-quotas namespace from requesting more than one CPU.
Use the oc create command to create the quota.
[student@workstation ~]$ oc create quota one-cpu --hard=requests.cpu=1
resourcequota/one-cpu createdUse the oc get command to verify the quota.
[student@workstation ~]$ oc get quota one-cpu -o yaml
apiVersion: v1
kind: ResourceQuota
metadata:
creationTimestamp: "2024-01-30T18:26:49Z"
name: one-cpu
namespace: selfservice-quotas
...output omitted...
spec:
hard:
requests.cpu: "1"
status:
hard:
requests.cpu: "1"
used:
requests.cpu: "1"The test deployment already requests one CPU.
Try to scale the deployment to eight replicas and to create a second deployment.
Use the oc scale command to scale the deployment.
[student@workstation ~]$ oc scale deployment test --replicas=8
deployment.apps/test scaledUse the oc create command to create a second deployment.
[student@workstation ~]$ oc create deployment test2 \
--image registry.ocp4.example.com:8443/redhattraining/hello-world-nginx
deployment.apps/test2 createdUse the oc get command to review pods and deployments.
[student@workstation ~]$ oc get pod,deployment
NAME READY STATUS RESTARTS AGE
pod/test-6c66b55cb5-mdxjl 1/1 Running 0 2m58s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/test 1/8 1 1 3m20s
deployment.apps/test2 0/1 0 0 14sThe test deployment creates only two pods. The second deployment does not create any pods.
Use the oc get command to examine the quota status.
[student@workstation ~]$ oc get quota one-cpu -o yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: one-cpu
namespace: selfservice-quotas
...output omitted...
spec:
hard:
requests.cpu: "1"
status:
hard:
requests.cpu: "1"
used:
requests.cpu: "1"The used status is kept at 1 because the test2 deployment can't request more resources in the quota.
Use the oc get command to list events.
Sort the events by their creation timestamp.
[student@workstation ~]$oc get event --sort-by .metadata.creationTimestampLAST SEEN TYPE REASON OBJECT MESSAGE ...output omitted... 4m42s Warning FailedCreate replicaset/test-6c66b55cb5 (combined from similar events): Error creating: pods "`test`-6c66b55cb5-djrr9" is forbidden:exceeded quota: one-cpu, requested: requests.cpu=1, used: requests.cpu=2, limited: requests.cpu=2 9m3s Warning FailedCreate replicaset/test2-7b9df44445 Error creating: pods "test2-7b9df44445-98wxp" is forbidden:failed quota: one-cpu: must specify requests.cpufor: hello-world-nginx ...output omitted...
The test deployment cannot create further pods, because the new pods would exceed the quota.
The test2 deployment cannot create pods, because the deployment does not set a CPU request.
Create a test project to verify that you can create new workloads in other namespaces that request CPU resources.
Use the oc new-project command to create the project.
[student@workstation ~]$ oc new-project test
Now using project "test" on server "https://api.ocp4.example.com:6443".
...output omitted...Use the oc create command to create the deployment.
[student@workstation ~]$ oc create deployment test --image \
registry.ocp4.example.com:8443/redhattraining/hello-world-nginx
deployment.apps/test createdUse the oc set resources command to request one CPU in the container specification.
[student@workstation ~]$ oc set resources deployment test --requests=cpu=1
deployment.apps/test resource requirements updatedUse the oc get command to review the pods and deployments in the test namespace.
[student@workstation ~]$ oc get pod,deployment
NAME READY STATUS RESTARTS AGE
pod/test-8b9fdfbd9-447w9 1/1 Running 0 21s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/test 1/1 1 1 51sEven though you cannot create further workloads in the selfservice-quotas namespace, you can create workloads that request CPUs in other namespaces when the node has CPUs and memory available.