If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.
- LimitRange can be used to set the default amount of CPU/memory/ephemeral-storage as well as the minimum and maximum amount of CPU/memory/ephemeral-storage for all containers or pods in a namespace
You can set both requests and limits.
- requests
- the amount of memory / CPU / ephemeral-storage that is reserved for containers / pods. For example, if a node has 10 GB of memory and a pod requests 1 GB of memory, then 1 of the 10 GB of memory on the node would be reserved for the pod, meaning the node would then have 9 remaining GB of memory for other containers / pods. This is why it's important that containers / pods only request the minimum amount of CPU / memory that the container / pod require.
- limit
- the maximum amount of memory / CPU / ephemeral-storage a container or pod can use
- if a container or pod reaches the CPU limit, the container or pod will be throttled (won’t let it consume any more CPU)
- if a container or pod reaches the memory limit, Out Of Memory (OOM) should occur and the pod should be killed
- if a container or pod reaches the storage limit, the pod should be evicted
Before configuring Requests and Limits for CPU and memory, you will probably want to
- determine the average amount of CPU and memory being used by pods
- determine the amount of CPU and memory available on your nodes
Check out my article FreeKB - OpenShift - CPU and memory usage.
Before updating a resource such as a deployment to have requests/limits, you may want to first issue the oc get limits command to determine if there is a LimitRange resource in the namespace.
~]$ oc get limits
NAME CREATED AT
my-namespace-limits 2025-03-13T03:05:10Z
If there is a LimitRange resource in the namespace, the oc describe command can be used to display the configuration of the LimitRange resource in the namespace.
~]$ oc describe limits my-namespace-limits
Name: my-namespace-limits
Namespace: my-project
Type Resource Min Max Default Request Default Limit Max Limit/Request Ratio
---- -------- --- --- --------------- ------------- -----------------------
Container cpu 1m 1 5m 300m -
Container memory 4Mi 2Gi 16Mi 256Mi -
Container ephemeral-storage - - 1Mi 1Gi -
Pod ephemeral-storage - 3Gi - - -
If there is a LimitRange resource in the namespace, and if the resource managing the pod (such as a deployment) does not have requests/limits, then the pods in the namespace will have the values defined in the LimitRange resource. In this example, since the LimitRange sets cpu, memory and ephemeral-storage requests and limits for containers, each container in the pod should have the cpu, memory and ephemeral-storage requests and limits defined in the LimitRange resource in the namespace.
~]$ oc get pod/my-deployment-5b544f4785-hxkp4 --output yaml
spec:
containers:
- resources:
limits:
cpu: 300m
ephemeral-storage: 1Gi
memory: 256Mi
requests:
cpu: 5m
ephemeral-storage: 1Mi
memory: 16Mi
Setting the requests/limits in a pod or deployment will take precedence over the requests/limits in the LimitRange resource.
The oc set resources command can be used to update a deployment or deployment config to have certain requests / limits. This will update the deployment or pod YAML to have the requests / limits.
AVOID TROUBLE
This will set the CPU and memory requests / limits for a single deployment / container / pod in the project. If you want to set the combined CPU and memory limits of all of the containers or pods in a namespace, refer to Create CPU Memory Limits using a YAML template file.
~]$ oc set resources deployment my-deployment --limits=cpu=200m,memory=512Mi --requests=cpu=100m,memory=256Mi
deployment.apps/my-deployment resource requirements updated
Or you can edit the deployment, deployment config, replica set, replication controller, stateful set, or pod YAML with requests / limits.
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-openshift
spec:
replicas: 1
selector:
matchLabels:
app: hello-openshift
template:
metadata:
labels:
app: hello-openshift
spec:
containers:
- name: hello-openshift
image: openshift/hello-openshift:latest
ports:
- containerPort: 80
resources:
requests:
cpu: 10m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
The oc apply or oc create command with the -f or --filename option can be used to create the deployment using the template JSON or YAML file.
The oc replace command can be used to replace a deployment using a new or updated template JSON or YAML file.
The oc edit command can be used to update a deployments template YAML file.
~]$ oc create --filename deployment.yml
deployment.apps/hello-openshift created
The oc get deployments command can be used to list the deployments.
~]# oc get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-openshift 1 1 1 1 23s
The oc get pods command can be used to determine if the pod is up and running.
~]$ oc get pods
NAME READY STATUS RESTARTS AGE
hello-openshift 1/1 Running 0 23s
The oc describe deployment or oc describe pod command can be used to validate that the deployment or pod has CPU and memory limits.
~]$ oc describe deployment my-deployment
Pod Template:
Containers:
my-container:
Limits:
cpu: 200m
memory: 512Mi
Requests:
cpu: 100m
memory: 256Mi
Did you find this article helpful?
If so, consider buying me a coffee over at 