Bookmark this page

Guided Exercise: Project and Cluster Quotas

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

  1. Log in to your OpenShift cluster as the developer user with the developer password.

    1. 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...
  2. Create a selfservice-quotas project.

    1. 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...
  3. Create a deployment with a container that requests one CPU.

    1. 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 created
    2. Use 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 updated
    3. Use 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           49s

      Execute the command until the deployment and the pod are ready.

  4. Try to scale the deployment to eight replicas.

    1. Use the oc scale command to scale the deployment.

      [student@workstation ~]$ oc scale deployment test --replicas=8
      deployment.apps/test scaled
    2. Use 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           54m

      Out 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.

    3. Use the oc get command to list events. Sort the events by their creation timestamp.

      [student@workstation ~]$ oc get event --sort-by .metadata.creationTimestamp
      LAST 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
      3m57s       Warning   FailedScheduling    pod/test-6c66b55cb5-5n58r    0/1 nodes are available: 1 Insufficient 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.

  5. Examine the cluster as an administrator.

    1. 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...
    2. 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.

    3. Use the oc describe command to view the node details.

      [student@workstation ~]$ oc describe node/master01
      Name:               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
        --------           --------       ------
        cpu                4627m (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.

  6. Create a test project as an administrator, and verify that you cannot create new workloads that request a CPU.

    1. 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...
    2. 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 created
    3. Use 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 updated
    4. Use 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           100s

      The 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.

    5. Use the oc delete command to delete the test namespace.

      [student@workstation ~]$ oc delete namespace test
      namespace "test" deleted
  7. As an administrator, scale the deployment to one replica.

    1. 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".
    2. Use the oc scale command to scale the test deployment to one replica.

      [student@workstation ~]$ oc scale deployment test --replicas=1
      deployment.apps/test scaled
  8. Create a quota to prevent workloads in the selfservice-quotas namespace from requesting more than one CPU.

    1. Use the oc create command to create the quota.

      [student@workstation ~]$ oc create quota one-cpu --hard=requests.cpu=1
      resourcequota/one-cpu created
    2. Use 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.

  9. Try to scale the deployment to eight replicas and to create a second deployment.

    1. Use the oc scale command to scale the deployment.

      [student@workstation ~]$ oc scale deployment test --replicas=8
      deployment.apps/test scaled
    2. Use 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 created
    3. Use 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           14s

      The test deployment creates only two pods. The second deployment does not create any pods.

    4. 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.

    5. Use the oc get command to list events. Sort the events by their creation timestamp.

      [student@workstation ~]$ oc get event --sort-by .metadata.creationTimestamp
      LAST 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.cpu for: 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.

  10. Create a test project to verify that you can create new workloads in other namespaces that request CPU resources.

    1. 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...
    2. 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 created
    3. Use 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 updated
    4. Use 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           51s

      Even 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.

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish selfservice-quotas

Revision: do280-4.14-08d11e1