2
2
title : 为命名空间配置默认的 CPU 请求和限制
3
3
content_type : task
4
4
weight : 20
5
+ description : >-
6
+ 为命名空间定义默认的 CPU 资源限制,在该命名空间中每个新建的 Pod 都会被配置上 CPU 资源限制。
5
7
---
6
8
7
9
<!--
@@ -12,20 +14,44 @@ weight: 20
12
14
13
15
<!-- overview -->
14
16
<!--
15
- This page shows how to configure default CPU requests and limits for a namespace.
16
- A Kubernetes cluster can be divided into namespaces. If a Container is created in a namespace
17
- that has a default CPU limit, and the Container does not specify its own CPU limit, then
18
- the Container is assigned the default CPU limit. Kubernetes assigns a default CPU request
19
- under certain conditions that are explained later in this topic.
17
+ This page shows how to configure default CPU requests and limits for a
18
+ {{< glossary_tooltip text="namespace" term_id="namespace" >}}.
19
+
20
+ A Kubernetes cluster can be divided into namespaces. If you create a Pod within a
21
+ namespace that has a default CPU
22
+ [limit](/docs/concepts/configuration/manage-resources-containers/#requests-and-limits), and any container in that Pod does not specify
23
+ its own CPU limit, then the
24
+ {{< glossary_tooltip text="control plane" term_id="control-plane" >}} assigns the default
25
+ CPU limit to that container.
26
+
27
+ Kubernetes assigns a default CPU
28
+ [request](/docs/concepts/configuration/manage-resources-containers/#requests-and-limits),
29
+ but only under certain conditions that are explained later in this page.
30
+
20
31
-->
21
- 本章介绍怎样为命名空间配置默认的 CPU 请求和限制。
22
- 一个 Kubernetes 集群可被划分为多个命名空间。如果在配置了 CPU 限制的命名空间创建容器,
23
- 并且该容器没有声明自己的 CPU 限制,那么这个容器会被指定默认的 CPU 限制。
24
- Kubernetes 在一些特定情况还会指定 CPU 请求,本文后续章节将会对其进行解释。
32
+ 本章介绍如何为{{< glossary_tooltip text="命名空间" term_id="namespace" >}}配置默认的 CPU 请求和限制。
33
+
34
+ 一个 Kubernetes 集群可被划分为多个命名空间。
35
+ 如果你在具有默认 CPU[ 限制] ( /zh/docs/concepts/configuration/manage-resources-containers/#requests-and-limits )
36
+ 的命名空间内创建一个 Pod,并且这个 Pod 中任何容器都没有声明自己的 CPU 限制,
37
+ 那么{{< glossary_tooltip text="控制面" term_id="control-plane" >}}会为容器设定默认的 CPU 限制。
38
+
39
+ Kubernetes 在一些特定情况还可以设置默认的 CPU 请求,本文后续章节将会对其进行解释。
25
40
26
41
## {{% heading "prerequisites" %}}
27
42
28
- {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
43
+ {{< include "task-tutorial-prereqs.md" >}}
44
+
45
+ <!--
46
+ You must have access to create namespaces in your cluster.
47
+
48
+ If you're not already familiar with what Kubernetes means by 1.0 CPU,
49
+ read [meaning of CPU](/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu).
50
+ -->
51
+ 在你的集群里你必须要有创建命名空间的权限。
52
+
53
+ 如果你还不熟悉 Kubernetes 中 1.0 CPU 的含义,
54
+ 请阅读 [ CPU 的含义] ( /zh/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu ) 。
29
55
30
56
<!-- steps -->
31
57
@@ -46,12 +72,13 @@ kubectl create namespace default-cpu-example
46
72
<!--
47
73
## Create a LimitRange and a Pod
48
74
49
- Here's the configuration file for a LimitRange object. The configuration specifies
50
- a default CPU request and a default CPU limit.
75
+ Here's a manifest for an example {{< glossary_tooltip text="LimitRange" term_id="limitrange" >}}.
76
+ The manifest specifies a default CPU request and a default CPU limit.
51
77
-->
52
78
## 创建 LimitRange 和 Pod
53
79
54
- 这里给出了 LimitRange 对象的配置文件。该配置声明了一个默认的 CPU 请求和一个默认的 CPU 限制。
80
+ 以下为 {{< glossary_tooltip text="LimitRange" term_id="limitrange" >}} 的示例清单。
81
+ 清单中声明了默认 CPU 请求和默认 CPU 限制。
55
82
56
83
{{< codenew file="admin/resource/cpu-defaults.yaml" >}}
57
84
@@ -65,18 +92,19 @@ kubectl apply -f https://k8s.io/examples/admin/resource/cpu-defaults.yaml --name
65
92
```
66
93
67
94
<!--
68
- Now if a Container is created in the default-cpu-example namespace, and the
69
- Container does not specify its own values for CPU request and CPU limit,
70
- the Container is given a default CPU request of 0.5 and a default
95
+ Now if you create a Pod in the default-cpu-example namespace, and any container
96
+ in that Pod does not specify its own values for CPU request and CPU limit,
97
+ then the control plane applies default values: a CPU request of 0.5 and a default
71
98
CPU limit of 1.
72
99
73
- Here's the configuration file for a Pod that has one Container . The Container
100
+ Here's a manifest for a Pod that has one container . The container
74
101
does not specify a CPU request and limit.
75
102
-->
76
- 现在如果在 default-cpu-example 命名空间创建一个容器,该容器没有声明自己的 CPU 请求和限制时,
77
- 将会给它指定默认的 CPU 请求0.5和默认的 CPU 限制值1.
103
+ 现在如果你在 default-cpu-example 命名空间中创建一个 Pod,
104
+ 并且该 Pod 中所有容器都没有声明自己的 CPU 请求和 CPU 限制,
105
+ 控制面会将 CPU 的默认请求值 0.5 和默认限制值 1 应用到 Pod 上。
78
106
79
- 这里给出了包含一个容器的 Pod 的配置文件 。该容器没有声明 CPU 请求和限制。
107
+ 以下为只包含一个容器的 Pod 的清单 。该容器没有声明 CPU 请求和限制。
80
108
81
109
{{< codenew file="admin/resource/cpu-defaults-pod.yaml" >}}
82
110
@@ -99,10 +127,12 @@ kubectl get pod default-cpu-demo --output=yaml --namespace=default-cpu-example
99
127
```
100
128
101
129
<!--
102
- The output shows that the Pod's Container has a CPU request of 500 millicpus and
103
- a CPU limit of 1 cpu. These are the default values specified by the LimitRange.
130
+ The output shows that the Pod's only container has a CPU request of 500m `cpu`
131
+ (which you can read as “500 millicpu”), and a CPU limit of 1 `cpu`.
132
+ These are the default values specified by the LimitRange.
104
133
-->
105
- 输出显示该 Pod 的容器有一个500 millicpus的 CPU 请求和一个1 cpu的 CPU 限制。这些是 LimitRange 声明的默认值。
134
+ 输出显示该 Pod 的唯一的容器有 500m ` cpu ` 的 CPU 请求和 1 ` cpu ` 的 CPU 限制。
135
+ 这些是 LimitRange 声明的默认值。
106
136
107
137
``` shell
108
138
containers:
@@ -117,14 +147,14 @@ containers:
117
147
```
118
148
119
149
<!--
120
- ## What if you specify a Container 's limit, but not its request?
150
+ ## What if you specify a container 's limit, but not its request?
121
151
122
- Here's the configuration file for a Pod that has one Container . The Container
152
+ Here's a manifest for a Pod that has one container . The container
123
153
specifies a CPU limit, but not a request:
124
154
-->
125
155
## 你只声明容器的限制,而不声明请求会怎么样?
126
156
127
- 这是包含一个容器的 Pod 的配置文件 。该容器声明了 CPU 限制,而没有声明 CPU 请求。
157
+ 以下为只包含一个容器的 Pod 的清单 。该容器声明了 CPU 限制,而没有声明 CPU 请求。
128
158
129
159
{{< codenew file="admin/resource/cpu-defaults-pod-2.yaml" >}}
130
160
@@ -138,19 +168,20 @@ kubectl apply -f https://k8s.io/examples/admin/resource/cpu-defaults-pod-2.yaml
138
168
```
139
169
140
170
<!--
141
- View the Pod specification:
171
+ View the [specification](/docs/concepts/overview/working-with-objects/kubernetes-objects/#object-spec-and-status)
172
+ of the Pod that you created:
142
173
-->
143
- 查看 Pod 的声明 :
174
+ 查看你所创建的 Pod 的 [ 规约 ] ( /zh/docs/concepts/overview/working-with-objects/kubernetes-objects/#object-spec-and-status ) :
144
175
145
176
```
146
177
kubectl get pod default-cpu-demo-2 --output=yaml --namespace=default-cpu-example
147
178
```
148
179
149
180
<!--
150
181
The output shows that the Container's CPU request is set to match its CPU limit.
151
- Notice that the Container was not assigned the default CPU request value of 0.5 cpu.
182
+ Notice that the container was not assigned the default CPU request value of 0.5 ` cpu`:
152
183
-->
153
- 输出显示该容器的 CPU 请求和 CPU 限制设置相同。注意该容器没有被指定默认的 CPU 请求值0 .5 cpu。
184
+ 输出显示该容器的 CPU 请求和 CPU 限制设置相同。注意该容器没有被指定默认的 CPU 请求值 0 .5 ` cpu ` :
154
185
155
186
```
156
187
resources:
@@ -161,14 +192,14 @@ resources:
161
192
```
162
193
163
194
<!--
164
- ## What if you specify a Container 's request, but not its limit?
195
+ ## What if you specify a container 's request, but not its limit?
165
196
166
- Here's the configuration file for a Pod that has one Container . The Container
197
+ Here's an example manifest for a Pod that has one container . The container
167
198
specifies a CPU request, but not a limit:
168
199
-->
169
200
## 你只声明容器的请求,而不声明它的限制会怎么样?
170
201
171
- 这里给出了包含一个容器的 Pod 的配置文件 。该容器声明了 CPU 请求,而没有声明 CPU 限制。
202
+ 这里给出了包含一个容器的 Pod 的示例清单 。该容器声明了 CPU 请求,而没有声明 CPU 限制。
172
203
173
204
{{< codenew file="admin/resource/cpu-defaults-pod-3.yaml" >}}
174
205
@@ -182,21 +213,22 @@ kubectl apply -f https://k8s.io/examples/admin/resource/cpu-defaults-pod-3.yaml
182
213
```
183
214
184
215
<!--
185
- View the Pod specification :
216
+ View the specification of the Pod that you created :
186
217
-->
187
- 查看 Pod 的规约:
218
+ 查看所你创建的 Pod 的规约:
188
219
189
220
```
190
221
kubectl get pod default-cpu-demo-3 --output=yaml --namespace=default-cpu-example
191
222
```
192
223
193
224
<!--
194
- The output shows that the Container's CPU request is set to the value specified in the
195
- Container's configuration file. The Container's CPU limit is set to 1 cpu, which is the
196
- default CPU limit for the namespace.
225
+ The output shows that the container's CPU request is set to the value you specified at
226
+ the time you created the Pod (in other words: it matches the manifest).
227
+ However, the same container's CPU limit is set to 1 `cpu`, which is the default CPU limit
228
+ for that namespace.
197
229
-->
198
- 结果显示该容器的 CPU 请求被设置为容器配置文件中声明的数值 。
199
- 容器的CPU限制被设置为 1 CPU,即该命名空间的默认 CPU 限制值。
230
+ 输出显示你所创建的 Pod 中,容器的 CPU 请求为 Pod 清单中声明的值 。
231
+ 然而同一容器的 CPU 限制被设置为 1 ` cpu ` ,此值是该命名空间的默认 CPU 限制值。
200
232
201
233
```
202
234
resources:
@@ -209,27 +241,41 @@ resources:
209
241
<!--
210
242
## Motivation for default CPU limits and requests
211
243
212
- If your namespace has a
213
- [resource quota](/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace/) ,
244
+ If your namespace has a CPU {{< glossary_tooltip text="resource quota" term_id="resource-quota" >}}
245
+ configured ,
214
246
it is helpful to have a default value in place for CPU limit.
215
- Here are two of the restrictions that a resource quota imposes on a namespace:
247
+ Here are two of the restrictions that a CPU resource quota imposes on a namespace:
216
248
217
- * Every Container that runs in the namespace must have its own CPU limit.
218
- * The total amount of CPU used by all Containers in the namespace must not exceed a specified limit.
249
+ * For every Pod that runs in the namespace, each of its containers must have a CPU limit.
250
+ * CPU request apply a resource reservation on the node where the Pod in question is scheduled.
251
+ The total amount of CPU that is reserved for use by all Pods in the namespace must not
252
+ exceed a specified limit.
219
253
220
- If a Container does not specify its own CPU limit, it is given the default limit, and then
221
- it can be allowed to run in a namespace that is restricted by a quota.
222
254
-->
223
255
## 默认 CPU 限制和请求的动机
224
256
225
- 如果你的命名空间有一个
226
- [ 资源配额] ( /zh/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace/ ) ,
227
- 那么有一个默认的 CPU 限制是有帮助的。这里有资源配额强加给命名空间的两条限制:
257
+ 如果你的命名空间设置了 CPU {{< glossary_tooltip text="资源配额" term_id="resource-quota" >}},
258
+ 为 CPU 限制设置一个默认值会很有帮助。
259
+ 以下是 CPU 资源配额对命名空间的施加的两条限制:
260
+
261
+ * 命名空间中运行的每个 Pod 中的容器都必须有 CPU 限制。
262
+
263
+ * CPU 限制用来在 Pod 被调度到的节点上执行资源预留。
228
264
229
- * 命名空间中运行的每个容器必须有自己的 CPU 限制。
230
- * 命名空间中所有容器使用的 CPU 总和不能超过一个声明值。
265
+ 预留给命名空间中所有 Pod 使用的 CPU 总量不能超过规定的限制。
266
+
267
+ <!--
268
+ When you add a LimitRange:
269
+
270
+ If any Pod in that namespace that includes a container does not specify its own CPU limit,
271
+ the control plane applies the default CPU limit to that container, and the Pod can be
272
+ allowed to run in a namespace that is restricted by a CPU ResourceQuota.
273
+ -->
274
+ 当你添加 LimitRange 时:
231
275
232
- 如果容器没有声明自己的 CPU 限制,将会给它一个默认限制,这样它就能被允许运行在一个有配额限制的命名空间中。
276
+ 如果该命名空间中的任何 Pod 的容器未指定 CPU 限制,
277
+ 控制面将默认 CPU 限制应用于该容器,
278
+ 这样 Pod 可以在受到 CPU ResourceQuota 限制的命名空间中运行。
233
279
234
280
<!--
235
281
## Clean up
0 commit comments