Configure Minimum and Maximum CPU Constraints for a Namespace [page]deterministic
Define a range of valid CPU resource limits for a namespace, so that every new Pod in that namespace falls within the range you configure.
This page shows how to set minimum and maximum values for the CPU resources used by containers and Pods in a [namespace](#gloss:namespace). You specify minimum and maximum CPU values in a [LimitRange](/docs/reference/kubernetes-api/policy-resources/limit-range-v1/) object. If a Pod does not meet the constraints imposed by the LimitRange, it cannot be created in the namespace.
##
You must have access to create namespaces in your cluster.
Each node in your cluster must have at least 1.0 CPU available for Pods. See [meaning of CPU](/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu) to learn what Kubernetes means by “1 CPU”.
## Create a namespace
Create a namespace so that the resources you create in this exercise are isolated from the rest of your cluster.
```shell kubectl create namespace constraints-cpu-example ```
## Create a LimitRange and a Pod
Here's a manifest for an example [LimitRange](#gloss:limitrange):
Create the LimitRange:
```shell kubectl apply -f https://k8s.io/examples/admin/resource/cpu-constraints.yaml --namespace=constraints-cpu-example ```
View detailed information about the LimitRange:
```shell kubectl get limitrange cpu-min-max-demo-lr --output=yaml --namespace=constraints-cpu-example ```
The output shows the minimum and maximum CPU constraints as expected. But notice that even though you didn't specify default values in the configuration file for the LimitRange, they were created automatically.
```yaml limits: - default: cpu: 800m defaultRequest: cpu: 800m max: cpu: 800m min: cpu: 200m type: Container ```
Now whenever you create a Pod in the constraints-cpu-example namespace (or some other client of the Kubernetes API creates an equivalent Pod), Kubernetes performs these steps:
* If any container in that Pod does not specify its own CPU request and limit, the control plane assigns the default CPU request and limit to that container.
* Verify that every container in that Pod specifies a CPU request that is greater than or equal to 200 millicpu.
* Verify that every container in that Pod specifies a CPU limit that is less than or equal to 800 millicpu.
> Note: When creating a `LimitRange` object, you can specify limits on huge-pages or GPUs as well. However, when both `default` and `defaultRequest` are specified on these resources, the two values must be the same.
Here's a manifest for a Pod that has one container. The container manifest specifies a CPU request of 500 millicpu and a CPU limit of 800 millicpu. These satisfy the minimum and maximum CPU constraints imposed by the LimitRange for this namespace.
Create the Pod:
```shell kubectl apply -f https://k8s.io/examples/admin/resource/cpu-constraints-pod.yaml --namespace=constraints-cpu-example ```
Verify that the Pod is running and that its container is healthy:
```shell kubectl get pod constraints-cpu-demo --namespace=constraints-cpu-example ```
View detailed information about the Pod:
```shell kubectl get pod constraints-cpu-demo --output=yaml --namespace=constraints-cpu-example ```
The output shows that the Pod's only container has a CPU request of 500 millicpu and CPU limit of 800 millicpu. These satisfy the constraints imposed by the LimitRange.
```yaml resources: limits: cpu: 800m requests: cpu: 500m ```
## Delete the Pod
```shell kubectl delete pod constraints-cpu-demo --namespace=constraints-cpu-example ```
## Attempt to create a Pod that exceeds the maximum CPU constraint
Here's a manifest for a Pod that has one container. The container specifies a CPU request of 500 millicpu and a cpu limit of 1.5 cpu.
Attempt to create the Pod:
```shell kubectl apply -f https://k8s.io/examples/admin/resource/cpu-constraints-pod-2.yaml --namespace=constraints-cpu-example ```
The output shows that the Pod does not get created, because it defines an unacceptable container. That container is not acceptable because it specifies a CPU limit that is t …(trimmed)