From 177423bbd4d405b59d45bffae552c84929a9c18e Mon Sep 17 00:00:00 2001 From: song Date: Tue, 17 May 2022 23:11:52 +0800 Subject: [PATCH] [zh] sync tasks/administer-cluster/manage-resources Signed-off-by: song --- .../cpu-constraint-namespace.md | 141 +++++++++++------- .../memory-constraint-namespace.md | 6 +- .../quota-memory-cpu-namespace.md | 99 +++++++----- .../manage-resources/quota-pod-namespace.md | 58 +++++-- 4 files changed, 193 insertions(+), 111 deletions(-) diff --git a/content/zh/docs/tasks/administer-cluster/manage-resources/cpu-constraint-namespace.md b/content/zh/docs/tasks/administer-cluster/manage-resources/cpu-constraint-namespace.md index d05a5f6b339..ce990f8863b 100644 --- a/content/zh/docs/tasks/administer-cluster/manage-resources/cpu-constraint-namespace.md +++ b/content/zh/docs/tasks/administer-cluster/manage-resources/cpu-constraint-namespace.md @@ -2,36 +2,53 @@ title: 为命名空间配置 CPU 最小和最大约束 content_type: task weight: 40 +description: >- + 为命名空间定义一个有效的 CPU 资源限制范围,使得在该命名空间中所有新建 Pod 的 CPU 资源是在你所设置的范围内。 --- -本页介绍如何为命名空间中容器和 Pod 使用的 CPU 资源设置最小和最大值。 +本页介绍如何为{{< glossary_tooltip text="命名空间" term_id="namespace" >}}中的容器和 Pod +设置其所使用的 CPU 资源的最小和最大值。 你可以通过 -[LimitRange](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#limitrange-v1-core) -对象声明 CPU 的最小和最大值. 如果 Pod 不能满足 LimitRange 的限制,它就不能在命名空间中创建。 +[LimitRange](/docs/reference/kubernetes-api/policy-resources/limit-range-v1/) +对象声明 CPU 的最小和最大值. +如果 Pod 不能满足 LimitRange 的限制,就无法在该命名空间中被创建。 ## {{% heading "prerequisites" %}} -{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} +{{< include "task-tutorial-prereqs.md" >}} -你的集群中每个节点至少要有 1 个 CPU 可用才能运行本任务示例。 +在你的集群里你必须要有创建命名空间的权限。 + +集群中的每个节点都必须至少有 1.0 个 CPU 可供 Pod 使用。 + +请阅读 [CPU 的含义](/zh/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu) +理解 "1 CPU" 在 Kubernetes 中的含义。 @@ -52,11 +69,11 @@ kubectl create namespace constraints-cpu-example ## 创建 LimitRange 和 Pod -这里给出了 LimitRange 的配置文件: +以下为 {{< glossary_tooltip text="LimitRange" term_id="limitrange" >}} 的示例清单: {{< codenew file="admin/resource/cpu-constraints.yaml" >}} @@ -100,24 +117,25 @@ limits: ``` -现在不管什么时候在 constraints-cpu-example 命名空间中创建容器,Kubernetes 都会执行下面这些步骤: +现在,每当你在 constraints-mem-example 命名空间中创建 Pod 时,或者某些其他的 +Kubernetes API 客户端创建了等价的 Pod 时,Kubernetes 就会执行下面的步骤: -* 如果容器没有声明自己的 CPU 请求和限制,将为容器指定默认 CPU 请求和限制。 +* 如果 Pod 中的任何容器未声明自己的 CPU 请求和限制,控制面将为该容器设置默认的 CPU 请求和限制。 -* 核查容器声明的 CPU 请求确保其大于或者等于 200 millicpu。 +* 确保该 Pod 中的每个容器的 CPU 请求至少 200 millicpu。 -* 核查容器声明的 CPU 限制确保其小于或者等于 800 millicpu。 +* 确保该 Pod 中每个容器 CPU 请求不大于 800 millicpu。 -这里给出了包含一个容器的 Pod 的配置文件。 -该容器声明了 500 millicpu 的 CPU 请求和 800 millicpu 的 CPU 限制。 +以下为某个仅包含一个容器的 Pod 的清单。 +该容器声明了 CPU 请求 500 millicpu 和 CPU 限制 800 millicpu 。 这些参数满足了 LimitRange 对象规定的 CPU 最小和最大限制。 {{< codenew file="admin/resource/cpu-constraints-pod.yaml" >}} @@ -143,16 +161,16 @@ minimum and maximum CPU constraints imposed by the LimitRange. -创建Pod: +创建 Pod: ```shell kubectl apply -f https://k8s.io/examples/admin/resource/cpu-constraints-pod.yaml --namespace=constraints-cpu-example ``` -确认一下 Pod 中的容器在运行: +确认 Pod 正在运行,并且其容器处于健康状态: ```shell kubectl get pod constraints-cpu-demo --namespace=constraints-cpu-example @@ -168,10 +186,10 @@ kubectl get pod constraints-cpu-demo --output=yaml --namespace=constraints-cpu-e ``` -输出结果表明容器的 CPU 请求为 500 millicpu,CPU 限制为 800 millicpu。 +输出结果显示该 Pod 的容器的 CPU 请求为 500 millicpu,CPU 限制为 800 millicpu。 这些参数满足 LimitRange 规定的限制范围。 ```yaml @@ -214,10 +232,11 @@ kubectl apply -f https://k8s.io/examples/admin/resource/cpu-constraints-pod-2.ya ``` -输出结果表明 Pod 没有创建成功,因为容器声明的 CPU 限制太大了: +输出结果表明 Pod 没有创建成功,因为其中定义了一个无法被接受的容器。 +该容器之所以无法被接受是因为其中设定了过高的 CPU 限制值: ``` Error from server (Forbidden): error when creating "examples/admin/resource/cpu-constraints-pod-2.yaml": @@ -227,12 +246,12 @@ pods "constraints-cpu-demo-2" is forbidden: maximum cpu usage per Container is 8 ## 尝试创建一个不满足最小 CPU 请求的 Pod -这里给出了包含一个容器的 Pod 的配置文件。该容器声明了100 millicpu的 CPU 请求和800 millicpu的 CPU 限制。 +以下为某个只有一个容器的 Pod 的清单。该容器声明了 CPU 请求 100 millicpu 和 CPU 限制 800 millicpu。 {{< codenew file="admin/resource/cpu-constraints-pod-3.yaml" >}} @@ -246,10 +265,12 @@ kubectl apply -f https://k8s.io/examples/admin/resource/cpu-constraints-pod-3.ya ``` -输出结果显示 Pod 没有创建成功,因为容器声明的 CPU 请求太小了: +输出结果显示 Pod 没有创建成功,因为其中定义了一个无法被接受的容器。 +该容器无法被接受的原因是其中所设置的 CPU 请求小于最小值的限制: ``` Error from server (Forbidden): error when creating "examples/admin/resource/cpu-constraints-pod-3.yaml": @@ -259,12 +280,12 @@ pods "constraints-cpu-demo-4" is forbidden: minimum cpu usage per Container is 2 ## 创建一个没有声明 CPU 请求和 CPU 限制的 Pod -这里给出了包含一个容器的 Pod 的配置文件。该容器没有设定 CPU 请求和 CPU 限制。 +以下为一个只有一个容器的 Pod 的清单。该容器没有声明 CPU 请求,也没有声明 CPU 限制。 {{< codenew file="admin/resource/cpu-constraints-pod-4.yaml" >}} @@ -287,11 +308,14 @@ kubectl get pod constraints-cpu-demo-4 --namespace=constraints-cpu-example --out ``` -输出结果显示 Pod 的容器有个 800 millicpu 的 CPU 请求和 800 millicpu 的 CPU 限制。 -容器是怎样得到那些值的呢? +输出结果显示 Pod 的唯一容器的 CPU 请求为 800 millicpu,CPU 限制为 800 millicpu。 + +容器是怎样获得这些数值的呢? + ```yaml resources: @@ -302,26 +326,27 @@ resources: ``` -因为你的 Container 没有声明自己的 CPU 请求和限制,LimitRange 给它指定了 -[默认的 CPU 请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/) +因为这一容器没有声明自己的 CPU 请求和限制, +控制面会根据命名空间中配置 LimitRange +设置[默认的 CPU 请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/)。 -此时,你的容器可能运行也可能没有运行。 -回想一下,本任务的先决条件是你的节点要有 1 个 CPU。 -如果你的每个节点仅有 1 个 CPU,那么可能没有任何一个节点可以满足 800 millicpu 的 CPU 请求。 -如果你在用的节点恰好有两个 CPU,那么你才可能有足够的 CPU 来满足 800 millicpu 的请求。 +此时,你的 Pod 可能已经运行起来也可能没有运行起来。 +回想一下我们本次任务的先决条件是你的每个节点都至少有 1 CPU。 +如果你的每个节点都只有 1 CPU,那将没有一个节点拥有足够的可分配 CPU 来满足 800 millicpu 的请求。 +如果你在用的节点恰好有 2 CPU,那么有可能有足够的 CPU 来满足 800 millicpu 的请求。 ``` kubectl delete pod constraints-cpu-demo-4 --namespace=constraints-cpu-example diff --git a/content/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace.md b/content/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace.md index e0da942183e..203fcc95eb6 100644 --- a/content/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace.md +++ b/content/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace.md @@ -107,8 +107,8 @@ file for the LimitRange, they were created automatically. 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, assign -the default memory request and limit to that container. +* 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. @@ -121,7 +121,7 @@ minimum and maximum memory constraints imposed by the LimitRange. --> 现在,每当在 constraints-mem-example 命名空间中创建 Pod 时,Kubernetes 就会执行下面的步骤: -* 如果 Pod 中的任何容器未声明自己的内存请求和限制,将为该容器设置默认的内存请求和限制。 +* 如果 Pod 中的任何容器未声明自己的内存请求和限制,控制面将为该容器设置默认的内存请求和限制。 * 确保该 Pod 中的每个容器的内存请求至少 500 MiB。 diff --git a/content/zh/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace.md b/content/zh/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace.md index 416b0760942..e00f52fbd63 100644 --- a/content/zh/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace.md +++ b/content/zh/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace.md @@ -2,33 +2,43 @@ title: 为命名空间配置内存和 CPU 配额 content_type: task weight: 50 +description: >- + 为命名空间定义总的 CPU 和内存资源限制。 --- -本文介绍怎样为命名空间设置容器可用的内存和 CPU 总量。你可以通过 -[ResourceQuota](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcequota-v1-core) +本文介绍如何为{{< 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" >}} {{< version-check >}} +{{< include "task-tutorial-prereqs.md" >}} +在你的集群里你必须要有创建命名空间的权限。 + 集群中每个节点至少有 1 GiB 的内存。 @@ -51,11 +61,11 @@ kubectl create namespace quota-mem-cpu-example ## 创建 ResourceQuota -这里给出一个 ResourceQuota 对象的配置文件: +下面是 ResourceQuota 的示例清单: {{< codenew file="admin/resource/quota-mem-cpu.yaml" >}} @@ -80,28 +90,33 @@ kubectl get resourcequota mem-cpu-demo --namespace=quota-mem-cpu-example --outpu ResourceQuota 在 quota-mem-cpu-example 命名空间中设置了如下要求: -* 每个容器必须有内存请求和限制,以及 CPU 请求和限制。 -* 所有容器的内存请求总和不能超过1 GiB。 -* 所有容器的内存限制总和不能超过2 GiB。 -* 所有容器的 CPU 请求总和不能超过1 cpu。 -* 所有容器的 CPU 限制总和不能超过2 cpu。 +* 在该命名空间中的每个 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 中的含义。 ## 创建 Pod -这里给出 Pod 的配置文件: +以下是 Pod 的示例清单: {{< codenew file="admin/resource/quota-mem-cpu-pod.yaml" >}} @@ -115,11 +130,11 @@ kubectl apply -f https://k8s.io/examples/admin/resource/quota-mem-cpu-pod.yaml - ``` -检查下 Pod 中的容器在运行: +确认 Pod 正在运行,并且其容器处于健康状态: -``` +```shell kubectl get pod quota-mem-cpu-demo --namespace=quota-mem-cpu-example ``` @@ -128,7 +143,7 @@ Once again, view detailed information about the ResourceQuota: --> 再查看 ResourceQuota 的详情: -``` +```shell kubectl get resourcequota mem-cpu-demo --namespace=quota-mem-cpu-example --output=yaml ``` @@ -153,27 +168,38 @@ status: requests.memory: 600Mi ``` + +如果有 `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 . +``` + ## 尝试创建第二个 Pod -这里给出了第二个 Pod 的配置文件: +以下为第二个 Pod 的清单: {{< codenew file="admin/resource/quota-mem-cpu-pod-2.yaml" >}} -配置文件中,你可以看到 Pod 的内存请求为 700 MiB。 -请注意新的内存请求与已经使用的内存请求只和超过了内存请求的配额。 +在清单中,你可以看到 Pod 的内存请求为 700 MiB。 +请注意新的内存请求与已经使用的内存请求之和超过了内存请求的配额: 600 MiB + 700 MiB > 1 GiB。 尝试创建 Pod: @@ -198,19 +224,20 @@ requested: requests.memory=700Mi,used: requests.memory=600Mi, limited: requests. ## Discussion As you have seen in this exercise, you can use a ResourceQuota to restrict -the memory request total for all Containers running in a namespace. +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. -If you want to restrict individual Containers, instead of totals for all Containers, use a -[LimitRange](/docs/tasks/administer-cluster/memory-constraint-namespace/). +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 限制命名空间中所有容器的内存请求总量。 +如你在本练习中所见,你可以用 ResourceQuota 限制命名空间中所有 Pod 的内存请求总量。 同样你也可以限制内存限制总量、CPU 请求总量、CPU 限制总量。 -如果你想对单个容器而不是所有容器进行限制,就请使用 -[LimitRange](/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace/)。 +除了可以管理命名空间资源使用的总和,如果你想限制单个 Pod,或者限制这些 Pod 中的容器资源, +可以使用 [LimitRange](/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace/) 实现这类的功能。 + -本文主要描述如何配置一个命名空间下可运行的 Pod 个数配额。 -你可以使用 -[ResourceQuota](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcequota-v1-core) +本文主要介绍如何在{{< 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" >}} {{< version-check >}} +{{< include "task-tutorial-prereqs.md" >}} + + +在你的集群里你必须要有创建命名空间的权限。 @@ -40,11 +55,11 @@ kubectl create namespace quota-pod-example ## 创建 ResourceQuota -下面是一个 ResourceQuota 的配置文件: +下面是 ResourceQuota 的示例清单: {{< codenew file="admin/resource/quota-pod.yaml" >}} @@ -83,18 +98,20 @@ status: ``` -下面是一个 Deployment 的配置文件: +下面是一个 {{< glossary_tooltip term_id="deployment" >}} 的示例清单: {{< codenew file="admin/resource/quota-pod-deployment.yaml" >}} -在配置文件中,`replicas: 3` 告诉 Kubernetes 尝试创建三个 Pods,且运行相同的应用。 +在清单中,`replicas: 3` 告诉 Kubernetes 尝试创建三个 Pods, +且运行相同的应用。 创建这个 Deployment: @@ -113,7 +130,7 @@ kubectl get deployment pod-quota-demo --namespace=quota-pod-example --output=yam 从输出的信息我们可以看到,尽管尝试创建三个 Pod,但是由于配额的限制,只有两个 Pod 能被成功创建。 @@ -125,11 +142,24 @@ spec: status: availableReplicas: 2 ... -lastUpdateTime: 2017-07-07T20:57:05Z +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' ``` + +### 资源的选择 +在此任务中,你定义了一个限制 Pod 总数的 ResourceQuota, +你也可以限制其他类型对象的总数。例如, +你可以限制在一个命名空间中可以创建的 {{< glossary_tooltip text="CronJobs" term_id="cronjob" >}} 的数量。 +