Switch language name 'zh' to 'zh-cn'
This is the first step to rename 'zh' to 'zh-cn'. There are several reasons why we rename the language name.
- The upstream docsy theme changed the language name, leading to many warnings during site build;
The side-effect is that the i18n strings are no longer working.
- We believe renaming the language is the right thing to do, because this move can make room for other variants of Chinese language, such as 'zh-tw', 'zh-sg' etc.
There would be several follow-ups to this PR, such as fixing the intra-site links, adding redirects etc.
We will lock up changes to zh/zh-cn pages for the moment, until this one gets in.
This PR is based on commit cdad0a7342.
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
---
|
||||
title: 管理内存,CPU 和 API 资源
|
||||
weight: 20
|
||||
---
|
||||
+444
@@ -0,0 +1,444 @@
|
||||
---
|
||||
title: 为命名空间配置 CPU 最小和最大约束
|
||||
content_type: task
|
||||
weight: 40
|
||||
description: >-
|
||||
为命名空间定义一个有效的 CPU 资源限制范围,使得在该命名空间中所有新建 Pod 的 CPU 资源是在你所设置的范围内。
|
||||
---
|
||||
|
||||
<!--
|
||||
title: Configure Minimum and Maximum CPU Constraints for a Namespace
|
||||
content_type: task
|
||||
weight: 40
|
||||
description: >-
|
||||
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.
|
||||
-->
|
||||
|
||||
<!-- overview -->
|
||||
|
||||
<!--
|
||||
This page shows how to set minimum and maximum values for the CPU resources used by containers
|
||||
and Pods in a {{< glossary_tooltip text="namespace" term_id="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.
|
||||
-->
|
||||
本页介绍如何为{{< glossary_tooltip text="命名空间" term_id="namespace" >}}中的容器和 Pod
|
||||
设置其所使用的 CPU 资源的最小和最大值。
|
||||
你可以通过
|
||||
[LimitRange](/docs/reference/kubernetes-api/policy-resources/limit-range-v1/)
|
||||
对象声明 CPU 的最小和最大值.
|
||||
如果 Pod 不能满足 LimitRange 的限制,就无法在该命名空间中被创建。
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}}
|
||||
|
||||
<!--
|
||||
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”.
|
||||
-->
|
||||
在你的集群里你必须要有创建命名空间的权限。
|
||||
|
||||
集群中的每个节点都必须至少有 1.0 个 CPU 可供 Pod 使用。
|
||||
|
||||
请阅读 [CPU 的含义](/zh/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu)
|
||||
理解 "1 CPU" 在 Kubernetes 中的含义。
|
||||
|
||||
<!-- steps -->
|
||||
|
||||
<!--
|
||||
## 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 {{< glossary_tooltip text="LimitRange" term_id="limitrange" >}}:
|
||||
-->
|
||||
## 创建 LimitRange 和 Pod
|
||||
|
||||
以下为 {{< glossary_tooltip text="LimitRange" term_id="limitrange" >}} 的示例清单:
|
||||
|
||||
{{< codenew file="admin/resource/cpu-constraints.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the LimitRange:
|
||||
-->
|
||||
创建 LimitRange:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/cpu-constraints.yaml --namespace=constraints-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View detailed information about the LimitRange:
|
||||
-->
|
||||
查看 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.
|
||||
-->
|
||||
输出结果显示 CPU 的最小和最大限制符合预期。但需要注意的是,尽管你在 LimitRange
|
||||
的配置文件中你没有声明默认值,默认值也会被自动创建。
|
||||
|
||||
```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.
|
||||
-->
|
||||
|
||||
现在,每当你在 constraints-mem-example 命名空间中创建 Pod 时,或者某些其他的
|
||||
Kubernetes API 客户端创建了等价的 Pod 时,Kubernetes 就会执行下面的步骤:
|
||||
|
||||
* 如果 Pod 中的任何容器未声明自己的 CPU 请求和限制,控制面将为该容器设置默认的 CPU 请求和限制。
|
||||
|
||||
* 确保该 Pod 中的每个容器的 CPU 请求至少 200 millicpu。
|
||||
|
||||
* 确保该 Pod 中每个容器 CPU 请求不大于 800 millicpu。
|
||||
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
{{< note >}}
|
||||
当创建 LimitRange 对象时,你也可以声明大页面和 GPU 的限制。
|
||||
当这些资源同时声明了 'default' 和 'defaultRequest' 参数时,两个参数值必须相同。
|
||||
{{< /note >}}
|
||||
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
以下为某个仅包含一个容器的 Pod 的清单。
|
||||
该容器声明了 CPU 请求 500 millicpu 和 CPU 限制 800 millicpu 。
|
||||
这些参数满足了 LimitRange 对象规定的 CPU 最小和最大限制。
|
||||
|
||||
{{< codenew file="admin/resource/cpu-constraints-pod.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the Pod:
|
||||
-->
|
||||
创建 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:
|
||||
-->
|
||||
确认 Pod 正在运行,并且其容器处于健康状态:
|
||||
|
||||
```shell
|
||||
kubectl get pod constraints-cpu-demo --namespace=constraints-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View detailed information about the Pod:
|
||||
-->
|
||||
查看 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.
|
||||
-->
|
||||
输出结果显示该 Pod 的容器的 CPU 请求为 500 millicpu,CPU 限制为 800 millicpu。
|
||||
这些参数满足 LimitRange 规定的限制范围。
|
||||
|
||||
```yaml
|
||||
resources:
|
||||
limits:
|
||||
cpu: 800m
|
||||
requests:
|
||||
cpu: 500m
|
||||
```
|
||||
|
||||
<!--
|
||||
## Delete the Pod
|
||||
-->
|
||||
## 删除 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.
|
||||
-->
|
||||
## 尝试创建一个超过最大 CPU 限制的 Pod
|
||||
|
||||
这里给出了包含一个容器的 Pod 的配置文件。容器声明了 500 millicpu 的 CPU
|
||||
请求和 1.5 CPU 的 CPU 限制。
|
||||
|
||||
{{< codenew file="admin/resource/cpu-constraints-pod-2.yaml" >}}
|
||||
|
||||
<!--
|
||||
Attempt to create the Pod:
|
||||
-->
|
||||
尝试创建 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 too large:
|
||||
-->
|
||||
输出结果表明 Pod 没有创建成功,因为其中定义了一个无法被接受的容器。
|
||||
该容器之所以无法被接受是因为其中设定了过高的 CPU 限制值:
|
||||
|
||||
```
|
||||
Error from server (Forbidden): error when creating "examples/admin/resource/cpu-constraints-pod-2.yaml":
|
||||
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 a manifest for a Pod that has one container. The container specifies a
|
||||
CPU request of 100 millicpu and a CPU limit of 800 millicpu.
|
||||
-->
|
||||
## 尝试创建一个不满足最小 CPU 请求的 Pod
|
||||
|
||||
以下为某个只有一个容器的 Pod 的清单。该容器声明了 CPU 请求 100 millicpu 和 CPU 限制 800 millicpu。
|
||||
|
||||
{{< codenew file="admin/resource/cpu-constraints-pod-3.yaml" >}}
|
||||
|
||||
<!--
|
||||
Attempt to create the Pod:
|
||||
-->
|
||||
尝试创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/cpu-constraints-pod-3.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 request that is lower than the
|
||||
enforced minimum:
|
||||
-->
|
||||
输出结果显示 Pod 没有创建成功,因为其中定义了一个无法被接受的容器。
|
||||
该容器无法被接受的原因是其中所设置的 CPU 请求小于最小值的限制:
|
||||
|
||||
```
|
||||
Error from server (Forbidden): error when creating "examples/admin/resource/cpu-constraints-pod-3.yaml":
|
||||
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 a manifest for a Pod that has one container. The container does not
|
||||
specify a CPU request, nor does it specify a CPU limit.
|
||||
-->
|
||||
## 创建一个没有声明 CPU 请求和 CPU 限制的 Pod
|
||||
|
||||
以下为一个只有一个容器的 Pod 的清单。该容器没有声明 CPU 请求,也没有声明 CPU 限制。
|
||||
|
||||
{{< codenew file="admin/resource/cpu-constraints-pod-4.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the Pod:
|
||||
-->
|
||||
创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/cpu-constraints-pod-4.yaml --namespace=constraints-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View detailed information about the Pod:
|
||||
-->
|
||||
查看 Pod 的详情:
|
||||
|
||||
```
|
||||
kubectl get pod constraints-cpu-demo-4 --namespace=constraints-cpu-example --output=yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the Pod's single container has a CPU request of 800 millicpu and a
|
||||
CPU limit of 800 millicpu.
|
||||
How did that container get those values?
|
||||
-->
|
||||
输出结果显示 Pod 的唯一容器的 CPU 请求为 800 millicpu,CPU 限制为 800 millicpu。
|
||||
|
||||
容器是怎样获得这些数值的呢?
|
||||
|
||||
|
||||
```yaml
|
||||
resources:
|
||||
limits:
|
||||
cpu: 800m
|
||||
requests:
|
||||
cpu: 800m
|
||||
```
|
||||
|
||||
<!--
|
||||
Because that container did not specify its own CPU request and limit, the control plane
|
||||
applied the
|
||||
[default CPU request and limit](/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/)
|
||||
from the LimitRange for this namespace.
|
||||
-->
|
||||
因为这一容器没有声明自己的 CPU 请求和限制,
|
||||
控制面会根据命名空间中配置 LimitRange
|
||||
设置[默认的 CPU 请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/)。
|
||||
|
||||
<!--
|
||||
At this point, your Pod might be running or it might not be running. Recall that a prerequisite for
|
||||
this task is that your Nodes must have at least 1 CPU available for use. 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:
|
||||
-->
|
||||
此时,你的 Pod 可能已经运行起来也可能没有运行起来。
|
||||
回想一下我们本次任务的先决条件是你的每个节点都至少有 1 CPU。
|
||||
如果你的每个节点都只有 1 CPU,那将没有一个节点拥有足够的可分配 CPU 来满足 800 millicpu 的请求。
|
||||
如果你在用的节点恰好有 2 CPU,那么有可能有足够的 CPU 来满足 800 millicpu 的请求。
|
||||
|
||||
```
|
||||
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.
|
||||
-->
|
||||
## CPU 最小和最大限制的强制执行
|
||||
|
||||
只有当 Pod 创建或者更新时,LimitRange 为命名空间规定的 CPU 最小和最大限制才会被强制执行。
|
||||
如果你对 LimitRange 进行修改,那不会影响此前创建的 Pod。
|
||||
|
||||
<!--
|
||||
## 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:
|
||||
-->
|
||||
## 最小和最大 CPU 限制范围的动机
|
||||
|
||||
作为集群管理员,你可能想设定 Pod 可以使用的 CPU 资源限制。例如:
|
||||
|
||||
<!--
|
||||
* 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.
|
||||
|
||||
* A cluster is shared by your production and development departments.
|
||||
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
|
||||
each namespace.
|
||||
-->
|
||||
* 集群中的每个节点有两个 CPU。你不想接受任何请求超过 2 个 CPU 的 Pod,因为集群中没有节点可以支持这种请求。
|
||||
* 你的生产和开发部门共享一个集群。你想允许生产工作负载消耗 3 个 CPU,
|
||||
而开发部门工作负载的消耗限制为 1 个 CPU。
|
||||
你可以为生产和开发创建不同的命名空间,并且为每个命名空间都应用 CPU 限制。
|
||||
|
||||
<!--
|
||||
## Clean up
|
||||
|
||||
Delete your namespace:
|
||||
-->
|
||||
## 清理
|
||||
|
||||
删除你的命名空间:
|
||||
|
||||
```shell
|
||||
kubectl delete namespace constraints-cpu-example
|
||||
```
|
||||
|
||||
## {{% heading "whatsnext" %}}
|
||||
|
||||
<!--
|
||||
### For cluster administrators
|
||||
|
||||
* [Configure Default Memory Requests and Limits for a Namespace](/docs/tasks/administer-cluster/memory-default-namespace/)
|
||||
|
||||
* [Configure Default CPU Requests and Limits for a Namespace](/docs/tasks/administer-cluster/cpu-default-namespace/)
|
||||
|
||||
* [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/)
|
||||
-->
|
||||
|
||||
### 集群管理员参考:
|
||||
|
||||
* [为命名空间配置默认内存请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/memory-default-namespace/)
|
||||
* [为命名空间配置内存限制的最小值和最大值](/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace/)
|
||||
* [为命名空间配置 CPU 限制的最小值和最大值](/zh/docs/tasks/administer-cluster/manage-resources/cpu-constraint-namespace/)
|
||||
* [为命名空间配置内存和 CPU 配额](/zh/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace/)
|
||||
* [为命名空间配置 Pod 配额](/zh/docs/tasks/administer-cluster/manage-resources/quota-pod-namespace/)
|
||||
* [为 API 对象配置配额](/zh/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/)
|
||||
* [Assign CPU Resources to Containers and Pods](/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
||||
* [Configure Quality of Service for Pods](/docs/tasks/configure-pod-container/quality-service-pod/)
|
||||
-->
|
||||
|
||||
### 应用开发者参考:
|
||||
|
||||
* [为容器和 Pod 分配内存资源](/zh/docs/tasks/configure-pod-container/assign-memory-resource/)
|
||||
* [为容器和 Pod 分配 CPU 资源](/zh/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
||||
* [为 Pod 配置服务质量](/zh/docs/tasks/configure-pod-container/quality-service-pod/)
|
||||
|
||||
+331
@@ -0,0 +1,331 @@
|
||||
---
|
||||
title: 为命名空间配置默认的 CPU 请求和限制
|
||||
content_type: task
|
||||
weight: 20
|
||||
description: >-
|
||||
为命名空间定义默认的 CPU 资源限制,在该命名空间中每个新建的 Pod 都会被配置上 CPU 资源限制。
|
||||
---
|
||||
|
||||
<!--
|
||||
title: Configure Default CPU Requests and Limits for a Namespace
|
||||
content_type: task
|
||||
weight: 20
|
||||
-->
|
||||
|
||||
<!-- overview -->
|
||||
<!--
|
||||
This page shows how to configure default CPU requests and limits for a
|
||||
{{< glossary_tooltip text="namespace" term_id="namespace" >}}.
|
||||
|
||||
A Kubernetes cluster can be divided into namespaces. If you create a Pod within a
|
||||
namespace that has a default CPU
|
||||
[limit](/docs/concepts/configuration/manage-resources-containers/#requests-and-limits), and any container in that Pod does not specify
|
||||
its own CPU limit, then the
|
||||
{{< glossary_tooltip text="control plane" term_id="control-plane" >}} assigns the default
|
||||
CPU limit to that container.
|
||||
|
||||
Kubernetes assigns a default CPU
|
||||
[request](/docs/concepts/configuration/manage-resources-containers/#requests-and-limits),
|
||||
but only under certain conditions that are explained later in this page.
|
||||
|
||||
-->
|
||||
本章介绍如何为{{< glossary_tooltip text="命名空间" term_id="namespace" >}}配置默认的 CPU 请求和限制。
|
||||
|
||||
一个 Kubernetes 集群可被划分为多个命名空间。
|
||||
如果你在具有默认 CPU[限制](/zh/docs/concepts/configuration/manage-resources-containers/#requests-and-limits)
|
||||
的命名空间内创建一个 Pod,并且这个 Pod 中任何容器都没有声明自己的 CPU 限制,
|
||||
那么{{< glossary_tooltip text="控制面" term_id="control-plane" >}}会为容器设定默认的 CPU 限制。
|
||||
|
||||
Kubernetes 在一些特定情况还可以设置默认的 CPU 请求,本文后续章节将会对其进行解释。
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}}
|
||||
|
||||
<!--
|
||||
You must have access to create namespaces in your cluster.
|
||||
|
||||
If you're not already familiar with what Kubernetes means by 1.0 CPU,
|
||||
read [meaning of CPU](/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu).
|
||||
-->
|
||||
在你的集群里你必须要有创建命名空间的权限。
|
||||
|
||||
如果你还不熟悉 Kubernetes 中 1.0 CPU 的含义,
|
||||
请阅读 [CPU 的含义](/zh/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu)。
|
||||
|
||||
<!-- steps -->
|
||||
|
||||
<!--
|
||||
## 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 default-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
## Create a LimitRange and a Pod
|
||||
|
||||
Here's a manifest for an example {{< glossary_tooltip text="LimitRange" term_id="limitrange" >}}.
|
||||
The manifest specifies a default CPU request and a default CPU limit.
|
||||
-->
|
||||
## 创建 LimitRange 和 Pod
|
||||
|
||||
以下为 {{< glossary_tooltip text="LimitRange" term_id="limitrange" >}} 的示例清单。
|
||||
清单中声明了默认 CPU 请求和默认 CPU 限制。
|
||||
|
||||
{{< codenew file="admin/resource/cpu-defaults.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the LimitRange in the default-cpu-example namespace:
|
||||
-->
|
||||
在命名空间 default-cpu-example 中创建 LimitRange 对象:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/cpu-defaults.yaml --namespace=default-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
Now if you create a Pod in the default-cpu-example namespace, and any container
|
||||
in that Pod does not specify its own values for CPU request and CPU limit,
|
||||
then the control plane applies default values: a CPU request of 0.5 and a default
|
||||
CPU limit of 1.
|
||||
|
||||
Here's a manifest for a Pod that has one container. The container
|
||||
does not specify a CPU request and limit.
|
||||
-->
|
||||
现在如果你在 default-cpu-example 命名空间中创建一个 Pod,
|
||||
并且该 Pod 中所有容器都没有声明自己的 CPU 请求和 CPU 限制,
|
||||
控制面会将 CPU 的默认请求值 0.5 和默认限制值 1 应用到 Pod 上。
|
||||
|
||||
以下为只包含一个容器的 Pod 的清单。该容器没有声明 CPU 请求和限制。
|
||||
|
||||
{{< codenew file="admin/resource/cpu-defaults-pod.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the Pod.
|
||||
-->
|
||||
创建 Pod。
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/cpu-defaults-pod.yaml --namespace=default-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View the Pod's specification:
|
||||
-->
|
||||
查看该 Pod 的声明:
|
||||
|
||||
```shell
|
||||
kubectl get pod default-cpu-demo --output=yaml --namespace=default-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the Pod's only container has a CPU request of 500m `cpu`
|
||||
(which you can read as “500 millicpu”), and a CPU limit of 1 `cpu`.
|
||||
These are the default values specified by the LimitRange.
|
||||
-->
|
||||
输出显示该 Pod 的唯一的容器有 500m `cpu` 的 CPU 请求和 1 `cpu` 的 CPU 限制。
|
||||
这些是 LimitRange 声明的默认值。
|
||||
|
||||
```shell
|
||||
containers:
|
||||
- image: nginx
|
||||
imagePullPolicy: Always
|
||||
name: default-cpu-demo-ctr
|
||||
resources:
|
||||
limits:
|
||||
cpu: "1"
|
||||
requests:
|
||||
cpu: 500m
|
||||
```
|
||||
|
||||
<!--
|
||||
## What if you specify a container's limit, but not its request?
|
||||
|
||||
Here's a manifest for a Pod that has one container. The container
|
||||
specifies a CPU limit, but not a request:
|
||||
-->
|
||||
## 你只声明容器的限制,而不声明请求会怎么样?
|
||||
|
||||
以下为只包含一个容器的 Pod 的清单。该容器声明了 CPU 限制,而没有声明 CPU 请求。
|
||||
|
||||
{{< codenew file="admin/resource/cpu-defaults-pod-2.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the Pod:
|
||||
-->
|
||||
创建 Pod
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/cpu-defaults-pod-2.yaml --namespace=default-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View the [specification](/docs/concepts/overview/working-with-objects/kubernetes-objects/#object-spec-and-status)
|
||||
of the Pod that you created:
|
||||
-->
|
||||
查看你所创建的 Pod 的[规约](/zh/docs/concepts/overview/working-with-objects/kubernetes-objects/#object-spec-and-status):
|
||||
|
||||
```
|
||||
kubectl get pod default-cpu-demo-2 --output=yaml --namespace=default-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the Container's CPU request is set to match its CPU limit.
|
||||
Notice that the container was not assigned the default CPU request value of 0.5 `cpu`:
|
||||
-->
|
||||
输出显示该容器的 CPU 请求和 CPU 限制设置相同。注意该容器没有被指定默认的 CPU 请求值 0.5 `cpu`:
|
||||
|
||||
```
|
||||
resources:
|
||||
limits:
|
||||
cpu: "1"
|
||||
requests:
|
||||
cpu: "1"
|
||||
```
|
||||
|
||||
<!--
|
||||
## What if you specify a container's request, but not its limit?
|
||||
|
||||
Here's an example manifest for a Pod that has one container. The container
|
||||
specifies a CPU request, but not a limit:
|
||||
-->
|
||||
## 你只声明容器的请求,而不声明它的限制会怎么样?
|
||||
|
||||
这里给出了包含一个容器的 Pod 的示例清单。该容器声明了 CPU 请求,而没有声明 CPU 限制。
|
||||
|
||||
{{< codenew file="admin/resource/cpu-defaults-pod-3.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the Pod:
|
||||
-->
|
||||
创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/cpu-defaults-pod-3.yaml --namespace=default-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View the specification of the Pod that you created:
|
||||
-->
|
||||
查看所你创建的 Pod 的规约:
|
||||
|
||||
```
|
||||
kubectl get pod default-cpu-demo-3 --output=yaml --namespace=default-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the container's CPU request is set to the value you specified at
|
||||
the time you created the Pod (in other words: it matches the manifest).
|
||||
However, the same container's CPU limit is set to 1 `cpu`, which is the default CPU limit
|
||||
for that namespace.
|
||||
-->
|
||||
输出显示你所创建的 Pod 中,容器的 CPU 请求为 Pod 清单中声明的值。
|
||||
然而同一容器的 CPU 限制被设置为 1 `cpu`,此值是该命名空间的默认 CPU 限制值。
|
||||
|
||||
```
|
||||
resources:
|
||||
limits:
|
||||
cpu: "1"
|
||||
requests:
|
||||
cpu: 750m
|
||||
```
|
||||
|
||||
<!--
|
||||
## Motivation for default CPU limits and requests
|
||||
|
||||
If your namespace has a CPU {{< glossary_tooltip text="resource quota" term_id="resource-quota" >}}
|
||||
configured,
|
||||
it is helpful to have a default value in place for CPU limit.
|
||||
Here are two of the restrictions that a CPU resource quota imposes on a namespace:
|
||||
|
||||
* For every Pod that runs in the namespace, each of its containers must have a CPU limit.
|
||||
* CPU request apply a resource reservation on the node where the Pod in question is scheduled.
|
||||
The total amount of CPU that is reserved for use by all Pods in the namespace must not
|
||||
exceed a specified limit.
|
||||
|
||||
-->
|
||||
## 默认 CPU 限制和请求的动机
|
||||
|
||||
如果你的命名空间设置了 CPU {{< glossary_tooltip text="资源配额" term_id="resource-quota" >}},
|
||||
为 CPU 限制设置一个默认值会很有帮助。
|
||||
以下是 CPU 资源配额对命名空间的施加的两条限制:
|
||||
|
||||
* 命名空间中运行的每个 Pod 中的容器都必须有 CPU 限制。
|
||||
|
||||
* CPU 限制用来在 Pod 被调度到的节点上执行资源预留。
|
||||
|
||||
预留给命名空间中所有 Pod 使用的 CPU 总量不能超过规定的限制。
|
||||
|
||||
<!--
|
||||
When you add a LimitRange:
|
||||
|
||||
If any Pod in that namespace that includes a container does not specify its own CPU limit,
|
||||
the control plane applies the default CPU limit to that container, and the Pod can be
|
||||
allowed to run in a namespace that is restricted by a CPU ResourceQuota.
|
||||
-->
|
||||
当你添加 LimitRange 时:
|
||||
|
||||
如果该命名空间中的任何 Pod 的容器未指定 CPU 限制,
|
||||
控制面将默认 CPU 限制应用于该容器,
|
||||
这样 Pod 可以在受到 CPU ResourceQuota 限制的命名空间中运行。
|
||||
|
||||
<!--
|
||||
## Clean up
|
||||
|
||||
Delete your namespace:
|
||||
|
||||
```shell
|
||||
kubectl delete namespace default-cpu-example
|
||||
```
|
||||
-->
|
||||
## 清理
|
||||
|
||||
删除你的命名空间:
|
||||
|
||||
```shell
|
||||
kubectl delete namespace constraints-cpu-example
|
||||
```
|
||||
|
||||
## {{% heading "whatsnext" %}}
|
||||
|
||||
<!--
|
||||
### For cluster administrators
|
||||
|
||||
* [Configure Default Memory Requests and Limits for a Namespace](/docs/tasks/administer-cluster/memory-default-namespace/)
|
||||
* [Configure Minimum and Maximum Memory Constraints for a Namespace](/docs/tasks/administer-cluster/memory-constraint-namespace/)
|
||||
* [Configure Minimum and Maximum CPU Constraints for a Namespace](/docs/tasks/administer-cluster/cpu-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/)
|
||||
-->
|
||||
### 集群管理员参考
|
||||
|
||||
* [为命名空间配置默认内存请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/memory-default-namespace/)
|
||||
* [为命名空间配置内存限制的最小值和最大值](/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace/)
|
||||
* [为命名空间配置 CPU 限制的最小值和最大值](/zh/docs/tasks/administer-cluster/manage-resources/cpu-constraint-namespace/)
|
||||
* [为命名空间配置内存和 CPU 配额](/zh/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace/)
|
||||
* [为命名空间配置 Pod 配额](/zh/docs/tasks/administer-cluster/manage-resources/quota-pod-namespace/)
|
||||
* [为 API 对象配置配额](/zh/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/)
|
||||
* [Assign CPU Resources to Containers and Pods](/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
||||
* [Configure Quality of Service for Pods](/docs/tasks/configure-pod-container/quality-service-pod/)
|
||||
-->
|
||||
### 应用开发者参考
|
||||
|
||||
* [为容器和 Pod 分配内存资源](/zh/docs/tasks/configure-pod-container/assign-memory-resource/)
|
||||
* [为容器和 Pod 分配 CPU 资源](/zh/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
||||
* [为 Pod 配置服务质量](/zh/docs/tasks/configure-pod-container/quality-service-pod/)
|
||||
|
||||
|
||||
+424
@@ -0,0 +1,424 @@
|
||||
---
|
||||
title: 配置命名空间的最小和最大内存约束
|
||||
content_type: task
|
||||
weight: 30
|
||||
description: >-
|
||||
为命名口空间定义一个有效的内存资源限制范围,在该命名空间中每个新创建 Pod 的内存资源是在设置的范围内。
|
||||
---
|
||||
|
||||
<!--
|
||||
title: Configure Minimum and Maximum Memory Constraints for a Namespace
|
||||
content_type: task
|
||||
weight: 30
|
||||
-->
|
||||
|
||||
<!-- overview -->
|
||||
|
||||
<!--
|
||||
This page shows how to set minimum and maximum values for memory used by containers
|
||||
running in a namespace. You specify minimum and maximum memory values in a
|
||||
[LimitRange](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#limitrange-v1-core)
|
||||
object. If a Pod does not meet the constraints imposed by the LimitRange,
|
||||
it cannot be created in the namespace.
|
||||
-->
|
||||
本页介绍如何设置在命名空间中运行的容器使用的内存的最小值和最大值。 你可以在
|
||||
[LimitRange](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#limitrange-v1-core)
|
||||
对象中指定最小和最大内存值。如果 Pod 不满足 LimitRange 施加的约束,则无法在命名空间中创建它。
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}}
|
||||
|
||||
<!--
|
||||
You must have access to create namespaces in your cluster.
|
||||
Each node in your cluster must have at least 1 GiB of memory available for Pods.
|
||||
|
||||
-->
|
||||
在你的集群里你必须要有创建命名空间的权限。
|
||||
|
||||
集群中的每个节点都必须至少有 1 GiB 的内存可供 Pod 使用。
|
||||
|
||||
<!-- steps -->
|
||||
|
||||
<!--
|
||||
## 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-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
## Create a LimitRange and a Pod
|
||||
|
||||
Here's an example manifest for a LimitRange:
|
||||
-->
|
||||
## 创建 LimitRange 和 Pod
|
||||
|
||||
下面是 LimitRange 的示例清单:
|
||||
|
||||
{{< codenew file="admin/resource/memory-constraints.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the LimitRange:
|
||||
-->
|
||||
创建 LimitRange:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/memory-constraints.yaml --namespace=constraints-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View detailed information about the LimitRange:
|
||||
-->
|
||||
查看 LimitRange 的详情:
|
||||
|
||||
```shell
|
||||
kubectl get limitrange mem-min-max-demo-lr --namespace=constraints-mem-example --output=yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows the minimum and maximum memory 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.
|
||||
-->
|
||||
输出显示预期的最小和最大内存约束。 但请注意,即使你没有在 LimitRange 的配置文件中指定默认值,也会自动创建它们。
|
||||
|
||||
```
|
||||
limits:
|
||||
- default:
|
||||
memory: 1Gi
|
||||
defaultRequest:
|
||||
memory: 1Gi
|
||||
max:
|
||||
memory: 1Gi
|
||||
min:
|
||||
memory: 500Mi
|
||||
type: Container
|
||||
```
|
||||
|
||||
<!--
|
||||
Now whenever you define a Pod within the constraints-mem-example namespace, Kubernetes
|
||||
performs these steps:
|
||||
|
||||
* If any container in that Pod does not specify its own memory request and limit,
|
||||
the control plane assig nthe default memory request and limit to that container.
|
||||
|
||||
* Verify that every container in that Pod requests at least 500 MiB of memory.
|
||||
|
||||
* Verify that every container in that Pod requests no more than 1024 MiB (1 GiB)
|
||||
of memory.
|
||||
|
||||
Here's a manifest for a Pod that has one container. Within the Pod spec, the sole
|
||||
container specifies a memory request of 600 MiB and a memory limit of 800 MiB. These satisfy the
|
||||
minimum and maximum memory constraints imposed by the LimitRange.
|
||||
-->
|
||||
现在,每当在 constraints-mem-example 命名空间中创建 Pod 时,Kubernetes 就会执行下面的步骤:
|
||||
|
||||
* 如果 Pod 中的任何容器未声明自己的内存请求和限制,控制面将为该容器设置默认的内存请求和限制。
|
||||
|
||||
* 确保该 Pod 中的每个容器的内存请求至少 500 MiB。
|
||||
|
||||
* 确保该 Pod 中每个容器内存请求不大于 1 GiB。
|
||||
|
||||
以下为包含一个容器的 Pod 清单。该容器声明了 600 MiB 的内存请求和 800 MiB 的内存限制,
|
||||
这些满足了 LimitRange 施加的最小和最大内存约束。
|
||||
|
||||
{{< codenew file="admin/resource/memory-constraints-pod.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the Pod:
|
||||
-->
|
||||
创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/memory-constraints-pod.yaml --namespace=constraints-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
Verify that the Pod is running and that its container is healthy:
|
||||
-->
|
||||
确认 Pod 正在运行,并且其容器处于健康状态:
|
||||
|
||||
```shell
|
||||
kubectl get pod constraints-mem-demo --namespace=constraints-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View detailed information about the Pod:
|
||||
-->
|
||||
查看 Pod 详情:
|
||||
|
||||
```shell
|
||||
kubectl get pod constraints-mem-demo --output=yaml --namespace=constraints-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the container within that Pod has a memory request of 600 MiB and
|
||||
a memory limit of 800 MiB. These satisfy the constraints imposed by the LimitRange for
|
||||
this namespace:
|
||||
-->
|
||||
输出结果显示该 Pod 的容器的内存请求为 600 MiB,内存限制为 800 MiB。
|
||||
这些满足这个命名空间中 LimitRange 设定的限制范围。
|
||||
|
||||
```yaml
|
||||
resources:
|
||||
limits:
|
||||
memory: 800Mi
|
||||
requests:
|
||||
memory: 600Mi
|
||||
```
|
||||
|
||||
<!--
|
||||
Delete your Pod:
|
||||
-->
|
||||
删除你创建的 Pod:
|
||||
|
||||
```shell
|
||||
kubectl delete pod constraints-mem-demo --namespace=constraints-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
## Attempt to create a Pod that exceeds the maximum memory constraint
|
||||
|
||||
Here's a manifest for a Pod that has one container. The container specifies a
|
||||
memory request of 800 MiB and a memory limit of 1.5 GiB.
|
||||
-->
|
||||
## 尝试创建一个超过最大内存限制的 Pod
|
||||
|
||||
以下为包含一个容器的 Pod 的清单。这个容器声明了 800 MiB 的内存请求和 1.5 GiB 的内存限制。
|
||||
|
||||
{{< codenew file="admin/resource/memory-constraints-pod-2.yaml" >}}
|
||||
|
||||
<!--
|
||||
Attempt to create the Pod:
|
||||
-->
|
||||
尝试创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/memory-constraints-pod-2.yaml --namespace=constraints-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the Pod does not get created, because it defines a container that
|
||||
requests more memory than is allowed:
|
||||
-->
|
||||
输出结果显示 Pod 没有创建成功,因为它定义了一个容器的内存请求超过了允许的值。
|
||||
|
||||
```
|
||||
Error from server (Forbidden): error when creating "examples/admin/resource/memory-constraints-pod-2.yaml":
|
||||
pods "constraints-mem-demo-2" is forbidden: maximum memory usage per Container is 1Gi, but limit is 1536Mi.
|
||||
```
|
||||
|
||||
<!--
|
||||
## Attempt to create a Pod that does not meet the minimum memory request
|
||||
|
||||
Here's a manifest for a Pod that has one container. That container specifies a
|
||||
memory request of 100 MiB and a memory limit of 800 MiB.
|
||||
-->
|
||||
## 尝试创建一个不满足最小内存请求的 Pod
|
||||
|
||||
以下为只有一个容器的 Pod 的清单。这个容器声明了 100 MiB 的内存请求和 800 MiB 的内存限制。
|
||||
|
||||
{{< codenew file="admin/resource/memory-constraints-pod-3.yaml" >}}
|
||||
|
||||
<!--
|
||||
Attempt to create the Pod:
|
||||
-->
|
||||
尝试创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/memory-constraints-pod-3.yaml --namespace=constraints-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the Pod does not get created, because it defines a container
|
||||
that requests less memory than the enforced minimum:
|
||||
-->
|
||||
输出结果显示 Pod 没有创建成功,因为它定义了一个容器的内存请求小于强制要求的最小值:
|
||||
|
||||
```
|
||||
Error from server (Forbidden): error when creating "examples/admin/resource/memory-constraints-pod-3.yaml":
|
||||
pods "constraints-mem-demo-3" is forbidden: minimum memory usage per Container is 500Mi, but request is 100Mi.
|
||||
```
|
||||
|
||||
<!--
|
||||
## Create a Pod that does not specify any memory request or limit
|
||||
|
||||
Here's a manifest for a Pod that has one container. The container does not
|
||||
specify a memory request, and it does not specify a memory limit.
|
||||
-->
|
||||
## 创建一个没有声明内存请求和限制的 Pod
|
||||
|
||||
以下为只有一个容器的 Pod 清单。该容器没有声明内存请求,也没有声明内存限制。
|
||||
|
||||
{{< codenew file="admin/resource/memory-constraints-pod-4.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the Pod:
|
||||
-->
|
||||
创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/memory-constraints-pod-4.yaml --namespace=constraints-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View detailed information about the Pod:
|
||||
-->
|
||||
查看 Pod 详情:
|
||||
|
||||
```shell
|
||||
kubectl get pod constraints-mem-demo-4 --namespace=constraints-mem-example --output=yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the Pod's only container has a memory request of 1 GiB and a memory limit of 1 GiB.
|
||||
How did that container get those values?
|
||||
-->
|
||||
输出结果显示 Pod 的唯一容器内存请求为 1 GiB,内存限制为 1 GiB。容器怎样获得那些数值呢?
|
||||
|
||||
```
|
||||
resources:
|
||||
limits:
|
||||
memory: 1Gi
|
||||
requests:
|
||||
memory: 1Gi
|
||||
```
|
||||
|
||||
<!--
|
||||
Because your Pod did not define any memory request and limit for that container, the cluster
|
||||
applied a
|
||||
[default memory request and limit](/docs/tasks/administer-cluster/memory-default-namespace/)
|
||||
from the LimitRange.
|
||||
-->
|
||||
因为你的 Pod 没有为容器声明任何内存请求和限制,集群会从 LimitRange
|
||||
获取[默认的内存请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/memory-default-namespace/)。
|
||||
应用于容器。
|
||||
|
||||
<!--
|
||||
This means that the definition of that Pod shows those values. You can check it using
|
||||
`kubectl describe`:
|
||||
|
||||
```shell
|
||||
# Look for the "Requests:" section of the output
|
||||
kubectl describe pod constraints-mem-demo-4 --namespace=constraints-mem-example
|
||||
```
|
||||
-->
|
||||
这意味着 Pod 的定义会显示这些值。你可以通过 `kubectl describe` 查看:
|
||||
|
||||
```shell
|
||||
# 查看输出结果中的 "Requests:" 的值
|
||||
kubectl describe pod constraints-mem-demo-4 --namespace=constraints-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
At this point, your Pod might be running or it might not be running. Recall that a prerequisite
|
||||
for this task is that your Nodes have at least 1 GiB of memory. If each of your Nodes has only
|
||||
1 GiB of memory, then there is not enough allocatable memory on any Node to accommodate a memory
|
||||
request of 1 GiB. If you happen to be using Nodes with 2 GiB of memory, then you probably have
|
||||
enough space to accommodate the 1 GiB request.
|
||||
|
||||
Delete your Pod:
|
||||
-->
|
||||
此时,你的 Pod 可能已经运行起来也可能没有运行起来。
|
||||
回想一下我们本次任务的先决条件是你的每个节点都至少有 1 GiB 的内存。
|
||||
如果你的每个节点都只有 1 GiB 的内存,那将没有一个节点拥有足够的可分配内存来满足 1 GiB 的内存请求。
|
||||
|
||||
删除你的 Pod:
|
||||
|
||||
```shell
|
||||
kubectl delete pod constraints-mem-demo-4 --namespace=constraints-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
## Enforcement of minimum and maximum memory constraints
|
||||
|
||||
The maximum and minimum memory 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.
|
||||
-->
|
||||
## 强制执行内存最小和最大限制
|
||||
|
||||
LimitRange 为命名空间设定的最小和最大内存限制只有在 Pod 创建和更新时才会强制执行。
|
||||
如果你更新 LimitRange,它不会影响此前创建的 Pod。
|
||||
|
||||
<!--
|
||||
## Motivation for minimum and maximum memory constraints
|
||||
-->
|
||||
## 设置内存最小和最大限制的动因
|
||||
|
||||
<!--
|
||||
As a cluster administrator, you might want to impose restrictions on the amount of memory that Pods can use.
|
||||
For example:
|
||||
|
||||
* Each Node in a cluster has 2 GiB of memory. You do not want to accept any Pod that requests
|
||||
more than 2 GiB of memory, because no Node in the cluster can support the request.
|
||||
|
||||
* A cluster is shared by your production and development departments.
|
||||
You want to allow production workloads to consume up to 8 GiB of memory, but
|
||||
you want development workloads to be limited to 512 MiB. You create separate namespaces
|
||||
for production and development, and you apply memory constraints to each namespace.
|
||||
-->
|
||||
作为集群管理员,你可能想规定 Pod 可以使用的内存总量限制。例如:
|
||||
|
||||
* 集群的每个节点有 2 GiB 内存。你不想接受任何请求超过 2 GiB 的 Pod,因为集群中没有节点可以满足。
|
||||
* 集群由生产部门和开发部门共享。你希望允许产品部门的负载最多耗用 8 GiB 内存,
|
||||
但是开发部门的负载最多可使用 512 MiB。
|
||||
这时,你可以为产品部门和开发部门分别创建名字空间,并为各个名字空间设置内存约束。
|
||||
|
||||
<!--
|
||||
## Clean up
|
||||
|
||||
Delete your namespace:
|
||||
-->
|
||||
## 清理
|
||||
|
||||
删除你的命名空间:
|
||||
|
||||
```shell
|
||||
kubectl delete namespace constraints-mem-example
|
||||
```
|
||||
|
||||
## {{% heading "whatsnext" %}}
|
||||
|
||||
<!--
|
||||
### For cluster administrators
|
||||
|
||||
* [Configure Default Memory Requests and Limits for a Namespace](/docs/tasks/administer-cluster/memory-default-namespace/)
|
||||
* [Configure Default CPU Requests and Limits for a Namespace](/docs/tasks/administer-cluster/cpu-default-namespace/)
|
||||
* [Configure Minimum and Maximum CPU Constraints for a Namespace](/docs/tasks/administer-cluster/cpu-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/)
|
||||
-->
|
||||
|
||||
### 集群管理员参考
|
||||
|
||||
* [为命名空间配置默认内存请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/memory-default-namespace/)
|
||||
* [为命名空间配置内存限制的最小值和最大值](/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace/)
|
||||
* [为命名空间配置 CPU 限制的最小值和最大值](/zh/docs/tasks/administer-cluster/manage-resources/cpu-constraint-namespace/)
|
||||
* [为命名空间配置内存和 CPU 配额](/zh/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace/)
|
||||
* [为命名空间配置 Pod 配额](/zh/docs/tasks/administer-cluster/manage-resources/quota-pod-namespace/)
|
||||
* [为 API 对象配置配额](/zh/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/)
|
||||
* [Assign CPU Resources to Containers and Pods](/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
||||
* [Configure Quality of Service for Pods](/docs/tasks/configure-pod-container/quality-service-pod/)
|
||||
-->
|
||||
|
||||
### 应用开发者参考
|
||||
|
||||
* [为容器和 Pod 分配内存资源](/zh/docs/tasks/configure-pod-container/assign-memory-resource/)
|
||||
* [为容器和 Pod 分配 CPU 资源](/zh/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
||||
* [为 Pod 配置服务质量](/zh/docs/tasks/configure-pod-container/quality-service-pod/)
|
||||
|
||||
+346
@@ -0,0 +1,346 @@
|
||||
---
|
||||
title: 为命名空间配置默认的内存请求和限制
|
||||
content_type: task
|
||||
weight: 10
|
||||
description: >-
|
||||
为命名空间定义默认的内存资源限制,在该命名空间中每个新建的 Pod 都会被配置上内存资源限制。
|
||||
---
|
||||
|
||||
<!--
|
||||
title: Configure Default Memory Requests and Limits for a Namespace
|
||||
content_type: task
|
||||
weight: 10
|
||||
description: >-
|
||||
Define a default memory resource limit for a namespace, so that every new Pod
|
||||
in that namespace has a memory resource limit configured.
|
||||
-->
|
||||
|
||||
<!-- overview -->
|
||||
|
||||
<!--
|
||||
This page shows how to configure default memory requests and limits for a
|
||||
{{< glossary_tooltip text="namespace" term_id="namespace" >}}.
|
||||
|
||||
A Kubernetes cluster can be divided into namespaces. Once you have a namespace that
|
||||
has a default memory
|
||||
[limit](/docs/concepts/configuration/manage-resources-containers/#requests-and-limits),
|
||||
and you then try to create a Pod with a container that does not specify its own memory
|
||||
limit, then the
|
||||
{{< glossary_tooltip text="control plane" term_id="control-plane" >}} assigns the default
|
||||
memory limit to that container.
|
||||
|
||||
Kubernetes assigns a default memory request under certain conditions that are explained later in this topic.
|
||||
-->
|
||||
本章介绍如何为{{< glossary_tooltip text="命名空间" term_id="namespace" >}}配置默认的内存请求和限制。
|
||||
|
||||
一个 Kubernetes 集群可被划分为多个命名空间。
|
||||
如果你在具有默认内存[限制](/zh/docs/concepts/configuration/manage-resources-containers/#requests-and-limits)
|
||||
的命名空间内尝试创建一个 Pod,并且这个 Pod 中的容器没有声明自己的内存资源限制,
|
||||
那么{{< glossary_tooltip text="控制面" term_id="control-plane" >}}会为该容器设定默认的内存限制。
|
||||
|
||||
Kubernetes 还为某些情况指定了默认的内存请求,本章后面会进行介绍。
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}}
|
||||
|
||||
<!--
|
||||
You must have access to create namespaces in your cluster.
|
||||
|
||||
Each node in your cluster must have at least 2 GiB of memory.
|
||||
-->
|
||||
在你的集群里你必须要有创建命名空间的权限。
|
||||
|
||||
你的集群中的每个节点必须至少有 2 GiB 的内存。
|
||||
|
||||
<!-- steps -->
|
||||
|
||||
<!--
|
||||
## 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 default-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
## Create a LimitRange and a Pod
|
||||
|
||||
Here's a manifest for an example {{< glossary_tooltip text="LimitRange" term_id="limitrange" >}}.
|
||||
The manifest specifies a default memory
|
||||
request and a default memory limit.
|
||||
-->
|
||||
## 创建 LimitRange 和 Pod
|
||||
|
||||
以下为 {{< glossary_tooltip text="LimitRange" term_id="limitrange" >}} 的示例清单。
|
||||
清单中声明了默认的内存请求和默认的内存限制。
|
||||
|
||||
{{< codenew file="admin/resource/memory-defaults.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the LimitRange in the default-mem-example namespace:
|
||||
-->
|
||||
在 default-mem-example 命名空间创建限制范围:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/memory-defaults.yaml --namespace=default-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
Now if you create a Pod in the default-mem-example namespace, and any container
|
||||
within that Pod does not specify its own values for memory request and memory limit,
|
||||
then the {{< glossary_tooltip text="control plane" term_id="control-plane" >}}
|
||||
applies default values: a memory request of 256MiB and a memory limit of 512MiB.
|
||||
|
||||
Here's an example manifest for a Pod that has one container. The container
|
||||
does not specify a memory request and limit.
|
||||
-->
|
||||
现在如果你在 default-mem-example 命名空间中创建一个 Pod,
|
||||
并且该 Pod 中所有容器都没有声明自己的内存请求和内存限制,
|
||||
{{< glossary_tooltip text="控制面" term_id="control-plane" >}}
|
||||
会将内存的默认请求值 256MiB 和默认限制值 512MiB 应用到 Pod 上。
|
||||
|
||||
以下为只包含一个容器的 Pod 的清单。该容器没有声明内存请求和限制。
|
||||
|
||||
{{< codenew file="admin/resource/memory-defaults-pod.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the Pod.
|
||||
-->
|
||||
创建 Pod
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/memory-defaults-pod.yaml --namespace=default-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View detailed information about the Pod:
|
||||
-->
|
||||
查看 Pod 的详情:
|
||||
|
||||
```shell
|
||||
kubectl get pod default-mem-demo --output=yaml --namespace=default-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the Pod's container has a memory request of 256 MiB and
|
||||
a memory limit of 512 MiB. These are the default values specified by the LimitRange.
|
||||
-->
|
||||
输出内容显示该 Pod 的容器有 256 MiB 的内存请求和 512 MiB 的内存限制。
|
||||
这些都是 LimitRange 设置的默认值。
|
||||
|
||||
```shell
|
||||
containers:
|
||||
- image: nginx
|
||||
imagePullPolicy: Always
|
||||
name: default-mem-demo-ctr
|
||||
resources:
|
||||
limits:
|
||||
memory: 512Mi
|
||||
requests:
|
||||
memory: 256Mi
|
||||
```
|
||||
|
||||
<!--
|
||||
Delete your Pod:
|
||||
-->
|
||||
删除你的 Pod:
|
||||
|
||||
```shell
|
||||
kubectl delete pod default-mem-demo --namespace=default-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
## What if you specify a container's limit, but not its request?
|
||||
|
||||
Here's a manifest for a Pod that has one container. The container
|
||||
specifies a memory limit, but not a request:
|
||||
-->
|
||||
## 声明容器的限制而不声明它的请求会怎么样?
|
||||
|
||||
以下为只包含一个容器的 Pod 的清单。该容器声明了内存限制,而没有声明内存请求。
|
||||
|
||||
{{< codenew file="admin/resource/memory-defaults-pod-2.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the Pod:
|
||||
-->
|
||||
创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/memory-defaults-pod-2.yaml --namespace=default-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View detailed information about the Pod:
|
||||
-->
|
||||
查看 Pod 的详情:
|
||||
|
||||
```shell
|
||||
kubectl get pod default-mem-demo-2 --output=yaml --namespace=default-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the container's memory request is set to match its memory limit.
|
||||
Notice that the container was not assigned the default memory request value of 256Mi.
|
||||
-->
|
||||
输出结果显示容器的内存请求被设置为它的内存限制相同的值。注意该容器没有被指定默认的内存请求值 256MiB。
|
||||
|
||||
```
|
||||
resources:
|
||||
limits:
|
||||
memory: 1Gi
|
||||
requests:
|
||||
memory: 1Gi
|
||||
```
|
||||
|
||||
<!--
|
||||
## What if you specify a container's request, but not its limit?
|
||||
-->
|
||||
## 声明容器的内存请求而不声明内存限制会怎么样?
|
||||
|
||||
<!--
|
||||
Here's a manifest for a Pod that has one container. The container
|
||||
specifies a memory request, but not a limit:
|
||||
-->
|
||||
以下为只包含一个容器的 Pod 的清单。该容器声明了内存请求,但没有内存限制:
|
||||
|
||||
{{< codenew file="admin/resource/memory-defaults-pod-3.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the Pod:
|
||||
-->
|
||||
创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/memory-defaults-pod-3.yaml --namespace=default-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View the Pod's specification:
|
||||
-->
|
||||
查看 Pod 声明:
|
||||
|
||||
```shell
|
||||
kubectl get pod default-mem-demo-3 --output=yaml --namespace=default-mem-example
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the container's memory request is set to the value specified in the
|
||||
container's manifest. The container is limited to use no more than 512MiB of
|
||||
memory, which matches the default memory limit for the namespace.
|
||||
-->
|
||||
输出结果显示所创建的 Pod 中,容器的内存请求为 Pod 清单中声明的值。
|
||||
然而同一容器的内存限制被设置为 512MiB,此值是该命名空间的默认内存限制值。
|
||||
|
||||
```
|
||||
resources:
|
||||
limits:
|
||||
memory: 512Mi
|
||||
requests:
|
||||
memory: 128Mi
|
||||
```
|
||||
|
||||
<!--
|
||||
## Motivation for default memory limits and requests
|
||||
|
||||
If your namespace has a memory {{< glossary_tooltip text="resource quota" term_id="resource-quota" >}}
|
||||
configured,
|
||||
it is helpful to have a default value in place for memory limit.
|
||||
Here are three of the restrictions that a resource quota imposes on a namespace:
|
||||
|
||||
* For every Pod that runs in the namespace, the Pod and each of its containers must have a memory limit.
|
||||
(If you specify a memory limit for every container in a Pod, Kubernetes can infer the Pod-level memory
|
||||
limit by adding up the limits for its containers).
|
||||
* Memory limits apply a resource reservation on the node where the Pod in question is scheduled.
|
||||
The total amount of memory reserved for all Pods in the namespace must not exceed a specified limit.
|
||||
* The total amount of memory actually used by all Pods in the namespace must also not exceed a specified limit.
|
||||
-->
|
||||
## 设置默认内存限制和请求的动机
|
||||
|
||||
如果你的命名空间设置了内存 {{< glossary_tooltip text="资源配额" term_id="resource-quota" >}},
|
||||
那么为内存限制设置一个默认值会很有帮助。
|
||||
以下是内存资源配额对命名空间的施加的三条限制:
|
||||
|
||||
* 命名空间中运行的每个 Pod 中的容器都必须有内存限制。
|
||||
(如果为 Pod 中的每个容器声明了内存限制,
|
||||
Kubernetes 可以通过将其容器的内存限制相加推断出 Pod 级别的内存限制)。
|
||||
|
||||
* 内存限制用来在 Pod 被调度到的节点上执行资源预留。
|
||||
预留给命名空间中所有 Pod 使用的内存总量不能超过规定的限制。
|
||||
|
||||
* 命名空间中所有 Pod 实际使用的内存总量也不能超过规定的限制。
|
||||
|
||||
<!--
|
||||
When you add a LimitRange:
|
||||
|
||||
If any Pod in that namespace that includes a container does not specify its own memory limit,
|
||||
the control plane applies the default memory limit to that container, and the Pod can be
|
||||
allowed to run in a namespace that is restricted by a memory ResourceQuota.
|
||||
-->
|
||||
当你添加 LimitRange 时:
|
||||
|
||||
如果该命名空间中的任何 Pod 的容器未指定内存限制,
|
||||
控制面将默认内存限制应用于该容器,
|
||||
这样 Pod 可以在受到内存 ResourceQuota 限制的命名空间中运行。
|
||||
|
||||
<!--
|
||||
## Clean up
|
||||
|
||||
Delete your namespace:
|
||||
-->
|
||||
## 清理
|
||||
|
||||
删除你的命名空间:
|
||||
|
||||
```shell
|
||||
kubectl delete namespace default-mem-example
|
||||
```
|
||||
|
||||
## {{% heading "whatsnext" %}}
|
||||
|
||||
<!--
|
||||
### For cluster administrators
|
||||
|
||||
* [Configure Default CPU Requests and Limits for a Namespace](/docs/tasks/administer-cluster/cpu-default-namespace/)
|
||||
|
||||
* [Configure Minimum and Maximum Memory Constraints for a Namespace](/docs/tasks/administer-cluster/memory-constraint-namespace/)
|
||||
|
||||
* [Configure Minimum and Maximum CPU Constraints for a Namespace](/docs/tasks/administer-cluster/cpu-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/)
|
||||
-->
|
||||
### 集群管理员参考
|
||||
|
||||
* [为命名空间配置默认的 CPU 请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/)
|
||||
* [为命名空间配置最小和最大内存限制](/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace/)
|
||||
* [为命名空间配置最小和最大 CPU 限制](/zh/docs/tasks/administer-cluster/manage-resources/cpu-constraint-namespace/)
|
||||
* [为命名空间配置内存和 CPU 配额](/zh/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace/)
|
||||
* [为命名空间配置 Pod 配额](/zh/docs/tasks/administer-cluster/manage-resources/quota-pod-namespace/)
|
||||
* [为 API 对象配置配额](/zh/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/)
|
||||
|
||||
* [Assign CPU Resources to Containers and Pods](/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
||||
|
||||
* [Configure Quality of Service for Pods](/docs/tasks/configure-pod-container/quality-service-pod/)
|
||||
-->
|
||||
### 应用开发者参考
|
||||
|
||||
* [为容器和 Pod 分配内存资源](/zh/docs/tasks/configure-pod-container/assign-memory-resource/)
|
||||
* [为容器和 Pod 分配 CPU 资源](/zh/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
||||
* [为 Pod 配置服务质量](/zh/docs/tasks/configure-pod-container/quality-service-pod/)
|
||||
|
||||
+289
@@ -0,0 +1,289 @@
|
||||
---
|
||||
title: 为命名空间配置内存和 CPU 配额
|
||||
content_type: task
|
||||
weight: 50
|
||||
description: >-
|
||||
为命名空间定义总的 CPU 和内存资源限制。
|
||||
---
|
||||
|
||||
<!--
|
||||
title: Configure Memory and CPU Quotas for a Namespace
|
||||
content_type: task
|
||||
weight: 50
|
||||
description: >-
|
||||
Define overall memory and CPU resource limits for a namespace.
|
||||
-->
|
||||
|
||||
<!-- overview -->
|
||||
|
||||
<!--
|
||||
This page shows how to set quotas for the total amount memory and CPU that
|
||||
can be used by all Pods running in a {{< glossary_tooltip text="namespace" term_id="namespace" >}}.
|
||||
You specify quotas in a
|
||||
[ResourceQuota](/docs/reference/kubernetes-api/policy-resources/resource-quota-v1/)
|
||||
object.
|
||||
-->
|
||||
本文介绍如何为{{< glossary_tooltip text="命名空间" term_id="namespace" >}}下运行的所有 Pod 设置总的内存和 CPU 配额。
|
||||
你可以通过使用
|
||||
[ResourceQuota](/docs/reference/kubernetes-api/policy-resources/resource-quota-v1/)
|
||||
对象设置配额.
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}}
|
||||
|
||||
<!--
|
||||
You must have access to create namespaces in your cluster.
|
||||
|
||||
Each node in your cluster must have at least 1 GiB of memory.
|
||||
-->
|
||||
在你的集群里你必须要有创建命名空间的权限。
|
||||
|
||||
集群中每个节点至少有 1 GiB 的内存。
|
||||
|
||||
<!-- steps -->
|
||||
|
||||
<!--
|
||||
## 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 quota-mem-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
## Create a ResourceQuota
|
||||
|
||||
Here is a manifest for an example ResourceQuota:
|
||||
-->
|
||||
## 创建 ResourceQuota
|
||||
|
||||
下面是 ResourceQuota 的示例清单:
|
||||
|
||||
{{< codenew file="admin/resource/quota-mem-cpu.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the ResourceQuota:
|
||||
-->
|
||||
创建 ResourceQuota
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/quota-mem-cpu.yaml --namespace=quota-mem-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View detailed information about the ResourceQuota:
|
||||
-->
|
||||
查看 ResourceQuota 详情:
|
||||
|
||||
```shell
|
||||
kubectl get resourcequota mem-cpu-demo --namespace=quota-mem-cpu-example --output=yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
The ResourceQuota places these requirements on the quota-mem-cpu-example namespace:
|
||||
|
||||
* For every Pod in the namespace, each container must have a memory request, memory limit, cpu request, and cpu limit.
|
||||
* The memory request total for all Pods in that namespace must not exceed 1 GiB.
|
||||
* The memory limit total for all Pods in that namespace must not exceed 2 GiB.
|
||||
* The CPU request total for all Pods in that namespace must not exceed 1 cpu.
|
||||
* The CPU limit total for all Pods in that namespace must not exceed 2 cpu.
|
||||
|
||||
See [meaning of CPU](/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu)
|
||||
to learn what Kubernetes means by “1 CPU”.
|
||||
-->
|
||||
ResourceQuota 在 quota-mem-cpu-example 命名空间中设置了如下要求:
|
||||
|
||||
* 在该命名空间中的每个 Pod 的所有容器都必须要有内存请求和限制,以及 CPU 请求和限制。
|
||||
* 在该命名空间中所有 Pod 的内存请求总和不能超过 1 GiB。
|
||||
* 在该命名空间中所有 Pod 的内存限制总和不能超过 2 GiB。
|
||||
* 在该命名空间中所有 Pod 的 CPU 请求总和不能超过 1 cpu。
|
||||
* 在该命名空间中所有 Pod 的 CPU 限制总和不能超过 2 cpu。
|
||||
|
||||
请阅读 [CPU 的含义](/zh/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu)
|
||||
理解 "1 CPU" 在 Kubernetes 中的含义。
|
||||
<!--
|
||||
## Create a Pod
|
||||
|
||||
Here is a manifest for an example Pod:
|
||||
-->
|
||||
## 创建 Pod
|
||||
|
||||
以下是 Pod 的示例清单:
|
||||
|
||||
{{< codenew file="admin/resource/quota-mem-cpu-pod.yaml" >}}
|
||||
|
||||
<!--
|
||||
Create the Pod:
|
||||
-->
|
||||
创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/quota-mem-cpu-pod.yaml --namespace=quota-mem-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
Verify that the Pod is running and that its (only) container is healthy:
|
||||
-->
|
||||
确认 Pod 正在运行,并且其容器处于健康状态:
|
||||
|
||||
```shell
|
||||
kubectl get pod quota-mem-cpu-demo --namespace=quota-mem-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
Once again, view detailed information about the ResourceQuota:
|
||||
-->
|
||||
再查看 ResourceQuota 的详情:
|
||||
|
||||
```shell
|
||||
kubectl get resourcequota mem-cpu-demo --namespace=quota-mem-cpu-example --output=yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows the quota along with how much of the quota has been used.
|
||||
You can see that the memory and CPU requests and limits for your Pod do not
|
||||
exceed the quota.
|
||||
-->
|
||||
输出结果显示了配额以及有多少配额已经被使用。你可以看到 Pod 的内存和 CPU 请求值及限制值没有超过配额。
|
||||
|
||||
```
|
||||
status:
|
||||
hard:
|
||||
limits.cpu: "2"
|
||||
limits.memory: 2Gi
|
||||
requests.cpu: "1"
|
||||
requests.memory: 1Gi
|
||||
used:
|
||||
limits.cpu: 800m
|
||||
limits.memory: 800Mi
|
||||
requests.cpu: 400m
|
||||
requests.memory: 600Mi
|
||||
```
|
||||
|
||||
<!--
|
||||
If you have the `jq` tool, you can also query (using [JSONPath](/docs/reference/kubectl/jsonpath/))
|
||||
for just the `used` values, **and** pretty-print that that of the output. For example:
|
||||
-->
|
||||
如果有 `jq` 工具的话,你可以通过(使用 [JSONPath](/zh/docs/reference/kubectl/jsonpath/))
|
||||
直接查询 `used` 字段的值,并且输出整齐的 JSON 格式。
|
||||
|
||||
```shell
|
||||
kubectl get resourcequota mem-cpu-demo --namespace=quota-mem-cpu-example -o jsonpath='{ .status.used }' | jq .
|
||||
```
|
||||
|
||||
<!--
|
||||
## Attempt to create a second Pod
|
||||
|
||||
Here is a manifest for a second Pod:
|
||||
-->
|
||||
## 尝试创建第二个 Pod
|
||||
|
||||
以下为第二个 Pod 的清单:
|
||||
|
||||
{{< codenew file="admin/resource/quota-mem-cpu-pod-2.yaml" >}}
|
||||
|
||||
<!--
|
||||
In the manifest, you can see that the Pod has a memory request of 700 MiB.
|
||||
Notice that the sum of the used memory request and this new memory
|
||||
request exceeds the memory request quota: 600 MiB + 700 MiB > 1 GiB.
|
||||
|
||||
Attempt to create the Pod:
|
||||
-->
|
||||
|
||||
在清单中,你可以看到 Pod 的内存请求为 700 MiB。
|
||||
请注意新的内存请求与已经使用的内存请求之和超过了内存请求的配额:
|
||||
600 MiB + 700 MiB > 1 GiB。
|
||||
|
||||
尝试创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/quota-mem-cpu-pod-2.yaml --namespace=quota-mem-cpu-example
|
||||
```
|
||||
|
||||
<!--
|
||||
The second Pod does not get created. The output shows that creating the second Pod
|
||||
would cause the memory request total to exceed the memory request quota.
|
||||
-->
|
||||
第二个 Pod 不能被创建成功。输出结果显示创建第二个 Pod 会导致内存请求总量超过内存请求配额。
|
||||
|
||||
```
|
||||
Error from server (Forbidden): error when creating "examples/admin/resource/quota-mem-cpu-pod-2.yaml":
|
||||
pods "quota-mem-cpu-demo-2" is forbidden: exceeded quota: mem-cpu-demo,
|
||||
requested: requests.memory=700Mi,used: requests.memory=600Mi, limited: requests.memory=1Gi
|
||||
```
|
||||
|
||||
<!--
|
||||
## Discussion
|
||||
|
||||
As you have seen in this exercise, you can use a ResourceQuota to restrict
|
||||
the memory request total for all Pods running in a namespace.
|
||||
You can also restrict the totals for memory limit, cpu request, and cpu limit.
|
||||
|
||||
Instead of managing total resource use within a namespace, you might want to restrict
|
||||
individual Pods, or the containers in those Pods. To achieve that kind of limiting, use a
|
||||
[LimitRange](/docs/concepts/policy/limit-range/).
|
||||
-->
|
||||
## 讨论
|
||||
|
||||
如你在本练习中所见,你可以用 ResourceQuota 限制命名空间中所有 Pod 的内存请求总量。
|
||||
同样你也可以限制内存限制总量、CPU 请求总量、CPU 限制总量。
|
||||
|
||||
除了可以管理命名空间资源使用的总和,如果你想限制单个 Pod,或者限制这些 Pod 中的容器资源,
|
||||
可以使用 [LimitRange](/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace/) 实现这类的功能。
|
||||
|
||||
<!--
|
||||
## Clean up
|
||||
|
||||
Delete your namespace:
|
||||
-->
|
||||
## 清理
|
||||
|
||||
删除你的命名空间:
|
||||
|
||||
```shell
|
||||
kubectl delete namespace quota-mem-cpu-example
|
||||
```
|
||||
|
||||
## {{% heading "whatsnext" %}}
|
||||
|
||||
<!--
|
||||
### For cluster administrators
|
||||
|
||||
* [Configure Default Memory Requests and Limits for a Namespace](/docs/tasks/administer-cluster/memory-default-namespace/)
|
||||
* [Configure Default CPU Requests and Limits for a Namespace](/docs/tasks/administer-cluster/cpu-default-namespace/)
|
||||
* [Configure Minimum and Maximum Memory Constraints for a Namespace](/docs/tasks/administer-cluster/memory-constraint-namespace/)
|
||||
* [Configure Minimum and Maximum CPU Constraints for a Namespace](/docs/tasks/administer-cluster/cpu-constraint-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/)
|
||||
-->
|
||||
|
||||
### 集群管理员参考
|
||||
|
||||
* [为命名空间配置默认内存请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/memory-default-namespace/)
|
||||
* [为命名空间配置内存限制的最小值和最大值](/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace/)
|
||||
* [为命名空间配置 CPU 限制的最小值和最大值](/zh/docs/tasks/administer-cluster/manage-resources/cpu-constraint-namespace/)
|
||||
* [为命名空间配置内存和 CPU 配额](/zh/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace/)
|
||||
* [为命名空间配置 Pod 配额](/zh/docs/tasks/administer-cluster/manage-resources/quota-pod-namespace/)
|
||||
* [为 API 对象配置配额](/zh/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/)
|
||||
* [Assign CPU Resources to Containers and Pods](/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
||||
* [Configure Quality of Service for Pods](/docs/tasks/configure-pod-container/quality-service-pod/)
|
||||
-->
|
||||
### 应用开发者参考
|
||||
|
||||
* [为容器和 Pod 分配内存资源](/zh/docs/tasks/configure-pod-container/assign-memory-resource/)
|
||||
* [为容器和 Pod 分配 CPU 资源](/zh/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
||||
* [为 Pod 配置服务质量](/zh/docs/tasks/configure-pod-container/quality-service-pod/)
|
||||
|
||||
@@ -0,0 +1,209 @@
|
||||
---
|
||||
title: 配置命名空间下 Pod 配额
|
||||
content_type: task
|
||||
weight: 60
|
||||
description: >-
|
||||
限制在命名空间中创建的 Pod 数量。
|
||||
---
|
||||
|
||||
<!--
|
||||
title: Configure a Pod Quota for a Namespace
|
||||
content_type: task
|
||||
weight: 60
|
||||
description: >-
|
||||
Restrict how many Pods you can create within a namespace.
|
||||
-->
|
||||
|
||||
<!-- overview -->
|
||||
|
||||
<!--
|
||||
This page shows how to set a quota for the total number of Pods that can run
|
||||
in a {{< glossary_tooltip text="Namespace" term_id="namespace" >}}. You specify quotas in a
|
||||
[ResourceQuota](/docs/reference/kubernetes-api/policy-resources/resource-quota-v1/)
|
||||
object.
|
||||
-->
|
||||
本文主要介绍如何在{{< glossary_tooltip text="命名空间" term_id="namespace" >}}中设置可运行 Pod 总数的配额。
|
||||
你可以通过使用
|
||||
[ResourceQuota](/zh/docs/reference/kubernetes-api/policy-resources/resource-quota-v1/)
|
||||
对象来配置配额。
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}}
|
||||
|
||||
<!--
|
||||
You must have access to create namespaces in your cluster.
|
||||
-->
|
||||
在你的集群里你必须要有创建命名空间的权限。
|
||||
|
||||
<!-- steps -->
|
||||
|
||||
<!--
|
||||
## 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 quota-pod-example
|
||||
```
|
||||
|
||||
<!--
|
||||
## Create a ResourceQuota
|
||||
|
||||
Here is an example manifest for a ResourceQuota:
|
||||
-->
|
||||
## 创建 ResourceQuota
|
||||
|
||||
下面是 ResourceQuota 的示例清单:
|
||||
|
||||
{{< codenew file="admin/resource/quota-pod.yaml" >}}
|
||||
|
||||
<!-- 创建 ResourceQuota: -->
|
||||
创建这个 ResourceQuota:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/quota-pod.yaml --namespace=quota-pod-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View detailed information about the ResourceQuota:
|
||||
-->
|
||||
查看资源配额的详细信息:
|
||||
|
||||
```shell
|
||||
kubectl get resourcequota pod-demo --namespace=quota-pod-example --output=yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the namespace has a quota of two Pods, and that currently there are
|
||||
no Pods; that is, none of the quota is used.
|
||||
-->
|
||||
从输出的信息我们可以看到,该命名空间下 Pod 的配额是 2 个,目前创建的 Pod 数为 0,
|
||||
配额使用率为 0。
|
||||
|
||||
```yaml
|
||||
spec:
|
||||
hard:
|
||||
pods: "2"
|
||||
status:
|
||||
hard:
|
||||
pods: "2"
|
||||
used:
|
||||
pods: "0"
|
||||
```
|
||||
|
||||
<!--
|
||||
Here is an example manifest for a {{< glossary_tooltip term_id="deployment" >}}:
|
||||
-->
|
||||
下面是一个 {{< glossary_tooltip term_id="deployment" >}} 的示例清单:
|
||||
|
||||
{{< codenew file="admin/resource/quota-pod-deployment.yaml" >}}
|
||||
|
||||
<!--
|
||||
In that manifest, `replicas: 3` tells Kubernetes to attempt to create three new Pods, all
|
||||
running the same application.
|
||||
|
||||
Create the Deployment:
|
||||
-->
|
||||
在清单中,`replicas: 3` 告诉 Kubernetes 尝试创建三个 Pods,
|
||||
且运行相同的应用。
|
||||
|
||||
创建这个 Deployment:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/admin/resource/quota-pod-deployment.yaml --namespace=quota-pod-example
|
||||
```
|
||||
|
||||
<!--
|
||||
View detailed information about the Deployment:
|
||||
-->
|
||||
查看 Deployment 的详细信息:
|
||||
|
||||
```shell
|
||||
kubectl get deployment pod-quota-demo --namespace=quota-pod-example --output=yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that even though the Deployment specifies three replicas, only two
|
||||
Pods were created because of the quota you defined earlier:
|
||||
-->
|
||||
从输出的信息我们可以看到,尽管尝试创建三个 Pod,但是由于配额的限制,只有两个 Pod 能被成功创建。
|
||||
|
||||
```yaml
|
||||
spec:
|
||||
...
|
||||
replicas: 3
|
||||
...
|
||||
status:
|
||||
availableReplicas: 2
|
||||
...
|
||||
lastUpdateTime: 2021-04-02T20:57:05Z
|
||||
message: 'unable to create pods: pods "pod-quota-demo-1650323038-" is forbidden:
|
||||
exceeded quota: pod-demo, requested: pods=1, used: pods=2, limited: pods=2'
|
||||
```
|
||||
|
||||
<!--
|
||||
### Choice of resource
|
||||
|
||||
In this task you have defined a ResourceQuota that limited the total number of Pods, but
|
||||
you could also limit the total number of other kinds of object. For example, you
|
||||
might decide to limit how many {{< glossary_tooltip text="CronJobs" term_id="cronjob" >}}
|
||||
that can live in a single namespace.
|
||||
-->
|
||||
### 资源的选择
|
||||
在此任务中,你定义了一个限制 Pod 总数的 ResourceQuota,
|
||||
你也可以限制其他类型对象的总数。例如,
|
||||
你可以限制在一个命名空间中可以创建的 {{< glossary_tooltip text="CronJobs" term_id="cronjob" >}} 的数量。
|
||||
|
||||
<!--
|
||||
## Clean up
|
||||
|
||||
Delete your namespace:
|
||||
-->
|
||||
## 清理
|
||||
|
||||
删除命名空间:
|
||||
|
||||
```shell
|
||||
kubectl delete namespace quota-pod-example
|
||||
```
|
||||
|
||||
## {{% heading "whatsnext" %}}
|
||||
|
||||
<!--
|
||||
### For cluster administrators
|
||||
|
||||
* [Configure Default Memory Requests and Limits for a Namespace](/docs/tasks/administer-cluster/manage-resources/memory-default-namespace/)
|
||||
* [Configure Default CPU Requests and Limits for a Namespace](/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/)
|
||||
* [Configure Minimum and Maximum Memory Constraints for a Namespace](/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace/)
|
||||
* [Configure Minimum and Maximum CPU Constraints for a Namespace](/docs/tasks/administer-cluster/manage-resources/cpu-constraint-namespace/)
|
||||
* [Configure Memory and CPU Quotas for a Namespace](/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace/)
|
||||
* [Configure Quotas for API Objects](/docs/tasks/administer-cluster/quota-api-object/)
|
||||
-->
|
||||
### 集群管理人员参考
|
||||
|
||||
* [为命名空间配置默认的内存请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/memory-default-namespace/)
|
||||
* [为命名空间配置默认的的 CPU 请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/)
|
||||
* [为命名空间配置内存的最小值和最大值约束](/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace/)
|
||||
* [为命名空间配置 CPU 的最小值和最大值约束](/zh/docs/tasks/administer-cluster/manage-resources/cpu-constraint-namespace/)
|
||||
* [为命名空间配置内存和 CPU 配额](/zh/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace/)
|
||||
* [为 API 对象的设置配额](/zh/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/)
|
||||
* [Assign CPU Resources to Containers and Pods](/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
||||
* [Configure Quality of Service for Pods](/docs/tasks/configure-pod-container/quality-service-pod/)
|
||||
-->
|
||||
### 应用开发人员参考
|
||||
|
||||
* [为容器和 Pod 分配内存资源](/zh/docs/tasks/configure-pod-container/assign-memory-resource/)
|
||||
* [给容器和 Pod 分配 CPU 资源](/zh/docs/tasks/configure-pod-container/assign-cpu-resource/)
|
||||
* [配置 Pod 的服务质量](/zh/docs/tasks/configure-pod-container/quality-service-pod/)
|
||||
|
||||
Reference in New Issue
Block a user