2
2
title : 为命名空间配置 CPU 最小和最大约束
3
3
content_type : task
4
4
weight : 40
5
+ description : >-
6
+ 为命名空间定义一个有效的 CPU 资源限制范围,使得在该命名空间中所有新建 Pod 的 CPU 资源是在你所设置的范围内。
5
7
---
6
8
7
9
<!--
8
10
title: Configure Minimum and Maximum CPU Constraints for a Namespace
9
11
content_type: task
10
12
weight: 40
13
+ description: >-
14
+ Define a range of valid CPU resource limits for a namespace, so that every new Pod
15
+ in that namespace falls within the range you configure.
11
16
-->
12
17
13
18
<!-- overview -->
14
19
15
20
<!--
16
- This page shows how to set minimum and maximum values for the CPU resources used by Containers
17
- and Pods in a namespace. You specify minimum and maximum CPU values in a
18
- [LimitRange](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#limitrange-v1-core)
21
+ This page shows how to set minimum and maximum values for the CPU resources used by containers
22
+ and Pods in a {{< glossary_tooltip text="namespace" term_id="namespace" >}}. You specify minimum
23
+ and maximum CPU values in a
24
+ [LimitRange](/docs/reference/kubernetes-api/policy-resources/limit-range-v1/)
19
25
object. If a Pod does not meet the constraints imposed by the LimitRange, it cannot be created
20
26
in the namespace.
21
27
-->
22
- 本页介绍如何为命名空间中容器和 Pod 使用的 CPU 资源设置最小和最大值。
28
+ 本页介绍如何为{{< glossary_tooltip text="命名空间" term_id="namespace" >}}中的容器和 Pod
29
+ 设置其所使用的 CPU 资源的最小和最大值。
23
30
你可以通过
24
- [ LimitRange] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#limitrange-v1-core)
25
- 对象声明 CPU 的最小和最大值. 如果 Pod 不能满足 LimitRange 的限制,它就不能在命名空间中创建。
31
+ [ LimitRange] ( /docs/reference/kubernetes-api/policy-resources/limit-range-v1/ )
32
+ 对象声明 CPU 的最小和最大值.
33
+ 如果 Pod 不能满足 LimitRange 的限制,就无法在该命名空间中被创建。
26
34
27
35
## {{% heading "prerequisites" %}}
28
36
29
- {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
37
+ {{< include "task-tutorial-prereqs.md" >}}
30
38
31
39
<!--
32
- Your cluster must have at least 1 CPU available for use to run the task examples.
40
+ You must have access to create namespaces in your cluster.
41
+
42
+ Each node in your cluster must have at least 1.0 CPU available for Pods.
43
+ See [meaning of CPU](/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu)
44
+ to learn what Kubernetes means by “1 CPU”.
33
45
-->
34
- 你的集群中每个节点至少要有 1 个 CPU 可用才能运行本任务示例。
46
+ 在你的集群里你必须要有创建命名空间的权限。
47
+
48
+ 集群中的每个节点都必须至少有 1.0 个 CPU 可供 Pod 使用。
49
+
50
+ 请阅读 [ CPU 的含义] ( /zh/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu )
51
+ 理解 "1 CPU" 在 Kubernetes 中的含义。
35
52
36
53
<!-- steps -->
37
54
@@ -52,11 +69,11 @@ kubectl create namespace constraints-cpu-example
52
69
<!--
53
70
## Create a LimitRange and a Pod
54
71
55
- Here's the configuration file for a LimitRange:
72
+ Here's a manifest for an example {{< glossary_tooltip text=" LimitRange" term_id="limitrange" >}} :
56
73
-->
57
74
## 创建 LimitRange 和 Pod
58
75
59
- 这里给出了 LimitRange 的配置文件 :
76
+ 以下为 {{< glossary_tooltip text=" LimitRange" term_id="limitrange" >}} 的示例清单 :
60
77
61
78
{{< codenew file="admin/resource/cpu-constraints.yaml" >}}
62
79
@@ -100,24 +117,25 @@ limits:
100
117
` ` `
101
118
102
119
<!--
103
- Now whenever a Container is created in the constraints-cpu-example namespace, Kubernetes
104
- performs these steps:
120
+ Now whenever you create a Pod in the constraints-cpu-example namespace (or some other client
121
+ of the Kubernetes API creates an equivalent Pod), Kubernetes performs these steps:
105
122
106
- * If the Container does not specify its own CPU request and limit, assign the default
107
- CPU request and limit to the Container .
123
+ * If any container in that Pod does not specify its own CPU request and limit, the control plane
124
+ assigns the default CPU request and limit to that container .
108
125
109
- * Verify that the Container specifies a CPU request that is greater than or equal to 200 millicpu.
126
+ * Verify that every container in that Pod specifies a CPU request that is greater than or equal to 200 millicpu.
110
127
111
- * Verify that the Container specifies a CPU limit that is less than or equal to 800 millicpu.
128
+ * Verify that every container in that Pod specifies a CPU limit that is less than or equal to 800 millicpu.
112
129
-->
113
130
114
- 现在不管什么时候在 constraints-cpu-example 命名空间中创建容器,Kubernetes 都会执行下面这些步骤:
131
+ 现在,每当你在 constraints-mem-example 命名空间中创建 Pod 时,或者某些其他的
132
+ Kubernetes API 客户端创建了等价的 Pod 时,Kubernetes 就会执行下面的步骤:
115
133
116
- * 如果容器没有声明自己的 CPU 请求和限制,将为容器指定默认 CPU 请求和限制。
134
+ * 如果 Pod 中的任何容器未声明自己的 CPU 请求和限制,控制面将为该容器设置默认的 CPU 请求和限制。
117
135
118
- * 核查容器声明的 CPU 请求确保其大于或者等于 200 millicpu。
136
+ * 确保该 Pod 中的每个容器的 CPU 请求至少 200 millicpu。
119
137
120
- * 核查容器声明的 CPU 限制确保其小于或者等于 800 millicpu。
138
+ * 确保该 Pod 中每个容器 CPU 请求不大于 800 millicpu。
121
139
122
140
<!--
123
141
When creating a ` LimitRange` object, you can specify limits on huge-pages
@@ -130,29 +148,29 @@ on these resources, the two values must be the same.
130
148
{{< /note >}}
131
149
132
150
<!--
133
- Here's the configuration file for a Pod that has one Container . The Container manifest
151
+ Here's a manifest for a Pod that has one container . The container manifest
134
152
specifies a CPU request of 500 millicpu and a CPU limit of 800 millicpu. These satisfy the
135
153
minimum and maximum CPU constraints imposed by the LimitRange.
136
154
-->
137
- 这里给出了包含一个容器的 Pod 的配置文件 。
138
- 该容器声明了 500 millicpu 的 CPU 请求和 800 millicpu 的 CPU 限制 。
155
+ 以下为某个仅包含一个容器的 Pod 的清单 。
156
+ 该容器声明了 CPU 请求 500 millicpu 和 CPU 限制 800 millicpu 。
139
157
这些参数满足了 LimitRange 对象规定的 CPU 最小和最大限制。
140
158
141
159
{{< codenew file="admin/resource/cpu-constraints-pod.yaml" >}}
142
160
143
161
<!--
144
162
Create the Pod :
145
163
-->
146
- 创建Pod :
164
+ 创建 Pod :
147
165
148
166
` ` ` shell
149
167
kubectl apply -f https://k8s.io/examples/admin/resource/cpu-constraints-pod.yaml --namespace=constraints-cpu-example
150
168
` ` `
151
169
152
170
<!--
153
- Verify that the Pod's Container is running :
171
+ Verify that the Pod is running and that its container is healthy :
154
172
-->
155
- 确认一下 Pod 中的容器在运行 :
173
+ 确认 Pod 正在运行,并且其容器处于健康状态 :
156
174
157
175
` ` ` shell
158
176
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
168
186
` ` `
169
187
170
188
<!--
171
- The output shows that the Container has a CPU request of 500 millicpu and CPU limit
189
+ The output shows that the Pod's only container has a CPU request of 500 millicpu and CPU limit
172
190
of 800 millicpu. These satisfy the constraints imposed by the LimitRange.
173
191
-->
174
- 输出结果表明容器的 CPU 请求为 500 millicpu,CPU 限制为 800 millicpu。
192
+ 输出结果显示该 Pod 的容器的 CPU 请求为 500 millicpu,CPU 限制为 800 millicpu。
175
193
这些参数满足 LimitRange 规定的限制范围。
176
194
177
195
` ` ` yaml
@@ -214,10 +232,11 @@ kubectl apply -f https://k8s.io/examples/admin/resource/cpu-constraints-pod-2.ya
214
232
` ` `
215
233
216
234
<!--
217
- The output shows that the Pod does not get created, because the Container specifies a CPU limit that is
218
- too large :
235
+ The output shows that the Pod does not get created, because it defines an unacceptable container.
236
+ That container is not acceptable because it specifies a CPU limit that is too large :
219
237
-->
220
- 输出结果表明 Pod 没有创建成功,因为容器声明的 CPU 限制太大了:
238
+ 输出结果表明 Pod 没有创建成功,因为其中定义了一个无法被接受的容器。
239
+ 该容器之所以无法被接受是因为其中设定了过高的 CPU 限制值:
221
240
222
241
```
223
242
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
227
246
<!--
228
247
## Attempt to create a Pod that does not meet the minimum CPU request
229
248
230
- Here's the configuration file for a Pod that has one Container . The Container specifies a
249
+ Here's a manifest for a Pod that has one container . The container specifies a
231
250
CPU request of 100 millicpu and a CPU limit of 800 millicpu.
232
251
-->
233
252
## 尝试创建一个不满足最小 CPU 请求的 Pod
234
253
235
- 这里给出了包含一个容器的 Pod 的配置文件。该容器声明了100 millicpu的 CPU 请求和800 millicpu的 CPU 限制。
254
+ 以下为某个只有一个容器的 Pod 的清单。该容器声明了 CPU 请求 100 millicpu 和 CPU 限制 800 millicpu 。
236
255
237
256
{{< codenew file="admin/resource/cpu-constraints-pod-3.yaml" >}}
238
257
@@ -246,10 +265,12 @@ kubectl apply -f https://k8s.io/examples/admin/resource/cpu-constraints-pod-3.ya
246
265
```
247
266
248
267
<!--
249
- The output shows that the Pod does not get created, because the Container specifies a CPU
250
- request that is too small:
268
+ The output shows that the Pod does not get created, because it defines an unacceptable container.
269
+ That container is not acceptable because it specifies a CPU request that is lower than the
270
+ enforced minimum:
251
271
-->
252
- 输出结果显示 Pod 没有创建成功,因为容器声明的 CPU 请求太小了:
272
+ 输出结果显示 Pod 没有创建成功,因为其中定义了一个无法被接受的容器。
273
+ 该容器无法被接受的原因是其中所设置的 CPU 请求小于最小值的限制:
253
274
254
275
```
255
276
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
259
280
<!--
260
281
## Create a Pod that does not specify any CPU request or limit
261
282
262
- Here's the configuration file for a Pod that has one Container . The Container does not
263
- specify a CPU request, and it does not specify a CPU limit.
283
+ Here's a manifest for a Pod that has one container . The container does not
284
+ specify a CPU request, nor does it specify a CPU limit.
264
285
-->
265
286
## 创建一个没有声明 CPU 请求和 CPU 限制的 Pod
266
287
267
- 这里给出了包含一个容器的 Pod 的配置文件。该容器没有设定 CPU 请求和 CPU 限制。
288
+ 以下为一个只有一个容器的 Pod 的清单。该容器没有声明 CPU 请求,也没有声明 CPU 限制。
268
289
269
290
{{< codenew file="admin/resource/cpu-constraints-pod-4.yaml" >}}
270
291
@@ -287,11 +308,14 @@ kubectl get pod constraints-cpu-demo-4 --namespace=constraints-cpu-example --out
287
308
```
288
309
289
310
<!--
290
- The output shows that the Pod's Container has a CPU request of 800 millicpu and a CPU limit of 800 millicpu.
291
- How did the Container get those values?
311
+ The output shows that the Pod's single container has a CPU request of 800 millicpu and a
312
+ CPU limit of 800 millicpu.
313
+ How did that container get those values?
292
314
-->
293
- 输出结果显示 Pod 的容器有个 800 millicpu 的 CPU 请求和 800 millicpu 的 CPU 限制。
294
- 容器是怎样得到那些值的呢?
315
+ 输出结果显示 Pod 的唯一容器的 CPU 请求为 800 millicpu,CPU 限制为 800 millicpu。
316
+
317
+ 容器是怎样获得这些数值的呢?
318
+
295
319
296
320
``` yaml
297
321
resources :
@@ -302,26 +326,27 @@ resources:
302
326
` ` `
303
327
304
328
<!--
305
- Because your Container did not specify its own CPU request and limit, it was given the
306
- [default CPU request and limit](/docs/tasks/administer-cluster/cpu-default-namespace/)
307
- from the LimitRange.
329
+ Because that container did not specify its own CPU request and limit, the control plane
330
+ applied the
331
+ [default CPU request and limit](/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/)
332
+ from the LimitRange for this namespace.
308
333
-->
309
- 因为你的 Container 没有声明自己的 CPU 请求和限制,LimitRange 给它指定了
310
- [默认的 CPU 请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/)
334
+ 因为这一容器没有声明自己的 CPU 请求和限制,
335
+ 控制面会根据命名空间中配置 LimitRange
336
+ 设置[默认的 CPU 请求和限制](/zh/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/)。
311
337
312
338
<!--
313
- At this point, your Container might be running or it might not be running. Recall that a prerequisite
314
- for this task is that your Nodes have at least 1 CPU. If each of your Nodes has only
315
- 1 CPU, then there might not be enough allocatable CPU on any Node to accommodate a request
316
- of 800 millicpu. If you happen to be using Nodes with 2 CPU, then you probably have
317
- enough CPU to accommodate the 800 millicpu request.
339
+ At this point, your Pod might be running or it might not be running. Recall that a prerequisite for
340
+ this task is that your Nodes must have at least 1 CPU available for use. If each of your Nodes has only 1 CPU,
341
+ then there might not be enough allocatable CPU on any Node to accommodate a request of 800 millicpu.
342
+ If you happen to be using Nodes with 2 CPU, then you probably have enough CPU to accommodate the 800 millicpu request.
318
343
319
344
Delete your Pod:
320
345
-->
321
- 此时,你的容器可能运行也可能没有运行 。
322
- 回想一下,本任务的先决条件是你的节点要有 1 个 CPU。
323
- 如果你的每个节点仅有 1 个 CPU,那么可能没有任何一个节点可以满足 800 millicpu 的 CPU 请求 。
324
- 如果你在用的节点恰好有两个 CPU,那么你才可能有足够的 CPU 来满足 800 millicpu 的请求。
346
+ 此时,你的 Pod 可能已经运行起来也可能没有运行起来 。
347
+ 回想一下我们本次任务的先决条件是你的每个节点都至少有 1 CPU。
348
+ 如果你的每个节点都只有 1 CPU,那将没有一个节点拥有足够的可分配 CPU 来满足 800 millicpu 的请求 。
349
+ 如果你在用的节点恰好有 2 CPU,那么有可能有足够的 CPU 来满足 800 millicpu 的请求。
325
350
326
351
` ` `
327
352
kubectl delete pod constraints-cpu-demo-4 --namespace=constraints-cpu-example
0 commit comments