2017-08-07 22:30:29 +00:00
|
|
|
---
|
|
|
|
title: Configure Minimum and Maximum CPU Constraints for a Namespace
|
2018-05-05 16:00:51 +00:00
|
|
|
content_template: templates/task
|
2018-05-20 03:35:51 +00:00
|
|
|
weight: 40
|
2017-08-07 22:30:29 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture overview %}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
This page shows how to set minimum and maximum values for the CPU resources used by Containers
|
|
|
|
and Pods in a namespace. You specify minimum and maximum CPU values in a
|
2018-05-05 16:00:51 +00:00
|
|
|
[LimitRange](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#limitrange-v1-core)
|
2017-08-07 22:30:29 +00:00
|
|
|
object. If a Pod does not meet the constraints imposed by the LimitRange, it cannot be created
|
|
|
|
in the namespace.
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture prerequisites %}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
Each node in your cluster must have at least 1 CPU.
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture steps %}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
## 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 the configuration file for a LimitRange:
|
|
|
|
|
2018-07-03 20:39:21 +00:00
|
|
|
{{< codenew file="admin/resource/cpu-constraints.yaml" >}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
Create the LimitRange:
|
|
|
|
|
|
|
|
```shell
|
2018-07-03 20:39:21 +00:00
|
|
|
kubectl create -f https://k8s.io/examples/admin/resource/cpu-constraints.yaml --namespace=constraints-cpu-example
|
2017-08-07 22:30:29 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
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 a Container is created in the constraints-cpu-example namespace, Kubernetes
|
|
|
|
performs these steps:
|
|
|
|
|
|
|
|
* If the Container does not specify its own CPU request and limit, assign the default
|
|
|
|
CPU request and limit to the Container.
|
|
|
|
|
|
|
|
* Verify that the Container specifies a CPU request that is greater than or equal to 200 millicpu.
|
|
|
|
|
2017-08-24 20:59:59 +00:00
|
|
|
* Verify that the Container specifies a CPU limit that is less than or equal to 800 millicpu.
|
2017-08-07 22:30:29 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< note >}}
|
2017-11-16 08:43:06 +00:00
|
|
|
**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.
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< /note >}}
|
2017-11-16 08:43:06 +00:00
|
|
|
|
2017-08-07 22:30:29 +00:00
|
|
|
Here's the configuration file 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.
|
|
|
|
|
2018-07-03 20:39:21 +00:00
|
|
|
{{< codenew file="admin/resource/cpu-constraints-pod.yaml" >}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
Create the Pod:
|
|
|
|
|
|
|
|
```shell
|
2018-07-03 20:39:21 +00:00
|
|
|
kubectl create -f https://k8s.io/examples/admin/resource/cpu-constraints-pod.yaml --namespace=constraints-cpu-example
|
2017-08-07 22:30:29 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Verify that the Pod's Container is running:
|
|
|
|
|
|
|
|
```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 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 the configuration file for a Pod that has one Container. The Container specifies a
|
|
|
|
CPU request of 500 millicpu and a cpu limit of 1.5 cpu.
|
|
|
|
|
2018-07-03 20:39:21 +00:00
|
|
|
{{< codenew file="admin/resource/cpu-constraints-pod-2.yaml" >}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
Attempt to create the Pod:
|
|
|
|
|
|
|
|
```shell
|
2018-07-03 20:39:21 +00:00
|
|
|
kubectl create -f https://k8s.io/examples/admin/resource/cpu-constraints-pod-2.yaml --namespace=constraints-cpu-example
|
2017-08-07 22:30:29 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
The output shows that the Pod does not get created, because the Container specifies a CPU limit that is
|
|
|
|
too large:
|
|
|
|
|
|
|
|
```
|
2018-07-03 20:39:21 +00:00
|
|
|
Error from server (Forbidden): error when creating "examples/admin/resource/cpu-constraints-pod-2.yaml":
|
2017-08-07 22:30:29 +00:00
|
|
|
pods "constraints-cpu-demo-2" is forbidden: maximum cpu usage per Container is 800m, but limit is 1500m.
|
|
|
|
```
|
|
|
|
|
|
|
|
## Attempt to create a Pod that does not meet the minimum CPU request
|
|
|
|
|
|
|
|
Here's the configuration file for a Pod that has one Container. The Container specifies a
|
|
|
|
CPU request of 100 millicpu and a CPU limit of 800 millicpu.
|
|
|
|
|
2018-07-03 20:39:21 +00:00
|
|
|
{{< codenew file="admin/resource/cpu-constraints-pod-3.yaml" >}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
Attempt to create the Pod:
|
|
|
|
|
|
|
|
```shell
|
2018-07-03 20:39:21 +00:00
|
|
|
kubectl create -f https://k8s.io/examples/admin/resource/cpu-constraints-pod-3.yaml --namespace=constraints-cpu-example
|
2017-08-07 22:30:29 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
The output shows that the Pod does not get created, because the Container specifies a CPU
|
|
|
|
request that is too small:
|
|
|
|
|
|
|
|
```
|
2018-07-03 20:39:21 +00:00
|
|
|
Error from server (Forbidden): error when creating "examples/admin/resource/cpu-constraints-pod-3.yaml":
|
2017-08-07 22:30:29 +00:00
|
|
|
pods "constraints-cpu-demo-4" is forbidden: minimum cpu usage per Container is 200m, but request is 100m.
|
|
|
|
```
|
|
|
|
|
|
|
|
## Create a Pod that does not specify any CPU request or limit
|
|
|
|
|
|
|
|
Here's the configuration file for a Pod that has one Container. The Container does not
|
|
|
|
specify a CPU request, and it does not specify a CPU limit.
|
|
|
|
|
2018-07-03 20:39:21 +00:00
|
|
|
{{< codenew file="admin/resource/cpu-constraints-pod-4.yaml" >}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
Create the Pod:
|
|
|
|
|
|
|
|
```shell
|
2018-07-03 20:39:21 +00:00
|
|
|
kubectl create -f https://k8s.io/examples/admin/resource/cpu-constraints-pod-4.yaml --namespace=constraints-cpu-example
|
2017-08-07 22:30:29 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
View detailed information about the Pod:
|
|
|
|
|
|
|
|
```
|
|
|
|
kubectl get pod constraints-cpu-demo-4 --namespace=constraints-cpu-example --output=yaml
|
|
|
|
```
|
|
|
|
|
|
|
|
The output shows that the Pod's Container has a CPU request of 800 millicpu and a CPU limit of 800 millicpu.
|
|
|
|
How did the Container get those values?
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
resources:
|
|
|
|
limits:
|
|
|
|
cpu: 800m
|
|
|
|
requests:
|
|
|
|
cpu: 800m
|
|
|
|
```
|
|
|
|
|
|
|
|
Because your Container did not specify its own CPU request and limit, it was given the
|
2017-08-22 23:45:59 +00:00
|
|
|
[default CPU request and limit](/docs/tasks/administer-cluster/cpu-default-namespace/)
|
2017-08-07 22:30:29 +00:00
|
|
|
from the LimitRange.
|
2017-09-11 16:09:46 +00:00
|
|
|
|
2017-08-07 22:30:29 +00:00
|
|
|
At this point, your Container might be running or it might not be running. Recall that a prerequisite
|
|
|
|
for this task is that your Nodes have at least 1 CPU. If each of your Nodes has only
|
|
|
|
1 CPU, then there might not be enough allocatable CPU on any Node to accommodate a request
|
|
|
|
of 800 millicpu. If you happen to be using Nodes with 2 CPU, then you probably have
|
|
|
|
enough CPU to accommodate the 800 millicpu request.
|
|
|
|
|
|
|
|
Delete your Pod:
|
|
|
|
|
|
|
|
```
|
|
|
|
kubectl delete pod constraints-cpu-demo-4 --namespace=constraints-cpu-example
|
|
|
|
```
|
|
|
|
|
|
|
|
## Enforcement of minimum and maximum CPU constraints
|
|
|
|
|
|
|
|
The maximum and minimum CPU constraints imposed on a namespace by a LimitRange are enforced only
|
|
|
|
when a Pod is created or updated. If you change the LimitRange, it does not affect
|
|
|
|
Pods that were created previously.
|
|
|
|
|
|
|
|
## Motivation for minimum and maximum CPU constraints
|
|
|
|
|
|
|
|
As a cluster administrator, you might want to impose restrictions on the CPU resources that Pods can use.
|
|
|
|
For example:
|
|
|
|
|
2017-09-11 16:09:46 +00:00
|
|
|
* Each Node in a cluster has 2 CPU. You do not want to accept any Pod that requests
|
|
|
|
more than 2 CPU, because no Node in the cluster can support the request.
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
* A cluster is shared by your production and development departments.
|
2017-09-11 16:09:46 +00:00
|
|
|
You want to allow production workloads to consume up to 3 CPU, but you want development workloads to be limited
|
|
|
|
to 1 CPU. You create separate namespaces for production and development, and you apply CPU constraints to
|
2017-08-07 22:30:29 +00:00
|
|
|
each namespace.
|
|
|
|
|
|
|
|
## Clean up
|
|
|
|
|
|
|
|
Delete your namespace:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
kubectl delete namespace constraints-cpu-example
|
|
|
|
```
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture whatsnext %}}
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
### For cluster administrators
|
|
|
|
|
2017-08-16 02:19:58 +00:00
|
|
|
* [Configure Default Memory Requests and Limits for a Namespace](/docs/tasks/administer-cluster/memory-default-namespace/)
|
2017-08-07 22:30:29 +00:00
|
|
|
|
2017-08-16 01:18:48 +00:00
|
|
|
* [Configure Default CPU Requests and Limits for a Namespace](/docs/tasks/administer-cluster/cpu-default-namespace/)
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
* [Configure Minimum and Maximum Memory Constraints for a Namespace](/docs/tasks/administer-cluster/memory-constraint-namespace/)
|
|
|
|
|
|
|
|
* [Configure Memory and CPU Quotas for a Namespace](/docs/tasks/administer-cluster/quota-memory-cpu-namespace/)
|
|
|
|
|
|
|
|
* [Configure a Pod Quota for a Namespace](/docs/tasks/administer-cluster/quota-pod-namespace/)
|
|
|
|
|
|
|
|
* [Configure Quotas for API Objects](/docs/tasks/administer-cluster/quota-api-object/)
|
|
|
|
|
|
|
|
### For app developers
|
|
|
|
|
|
|
|
* [Assign Memory Resources to Containers and Pods](/docs/tasks/configure-pod-container/assign-memory-resource/)
|
|
|
|
|
2017-08-15 05:39:25 +00:00
|
|
|
* [Assign CPU Resources to Containers and Pods](/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
* [Configure Quality of Service for Pods](/docs/tasks/configure-pod-container/quality-service-pod/)
|
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
|
|
|
|
2017-08-07 22:30:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|