How to specify limits and requests for cpu and memory in OpenShift

Solution Verified - Updated

Environment

  • Red Hat OpenShift Container Platform (RHOCP)
    • 3
    • 4
  • CPU and memory requests and limits

Issue

  • How to specify limits.cpu, limits.memory, requests.cpu, requests.memory for builds or for any workload?

  • When applying resourcequota at project level builds fails with below error :

    Error creating: pods "test-php-1-build" is forbidden: Failed quota: ludo-quota: must specify limits.cpu,limits.memory,requests.cpu,requests.memory
    23s        23s         1         test-php-1   Build                 Warning   HandleBuildError   {build-controller }   Build has error: failed to create build pod: pods "test-php-1-build" is forbidden: Failed quota: ludo-quota: must specify limits.cpu,limits.memory,requests.cpu,requests.memory
    
  • Replicaset failed to start the Pod(s) with the condition:

    message: 'pods "httpd-5bc7bcb454-bbkj4" is forbidden: failed quota: compute-resources: must specify limits.cpu,limits.memory'
    

Resolution

It is generally recommended to configure requests to avoid pods to not be allocated evenly on worker nodes, and limits to prevent memory leaks or CPU spikes in specific pods that could impact the rest of the pods in the node or even the whole node. To prevent the whole node to be impacted, it is also recommended to configure the system resources reservation.

Also, if a quota has been defined for a project, it is required to configure requests and/or limits. It is possible to declare resources explicitly or namespace bounded limit range.

Declare explicit requests and/or limits

Configure the resources section set with explicit requests and limits:

    resources:
      limits: 
        cpu: "100m"
        memory: "256Mi"
      requests: 
        cpu: "100m"
        memory: "256Mi"

Note: check the error message limits.cpu,limits.memory,requests.cpu,requests.memory if it says limits, requests, or both.

  • The requests object contains the list of resources that correspond to the list of resources in the quota.

Configure LimitRange in the project

A limit range defined in the project, where the defaults from the LimitRange object apply to pods created during the build process. An example from the documentation is as follows:

apiVersion: "v1"
kind: "LimitRange"
metadata:
  name: "resource-limits"
spec:
  limits:
    - type: "Container"
      max:
        cpu: "2"
        memory: "1Gi"
      min:
        cpu: "100m"
        memory: "4Mi"
      default:
        cpu: "300m"
        memory: "200Mi"
      defaultRequest:
        cpu: "200m"
        memory: "100Mi"
      maxLimitRequestRatio:
        cpu: "10"

Root Cause

It is generally recommended to configure requests and limits for custom workloads.

For additional information about Quotas, refer to the OpenShift 3.11 documentation about Advanced Build Operations and Setting Quotas, and for OpenShift 4 to the documentation about Quotas.

For additional information about LimitRanges, refer to Setting Limit Ranges in OpenShift 3.11 or the restrict resource consumption with limit ranges in OpenShift 4.

SBR
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.