2
2
title : 为命名空间配置默认的内存请求和限制
3
3
content_type : task
4
4
weight : 10
5
+ description : >-
6
+ 为命名空间定义默认的内存资源限制,在该命名空间中每个新建的 Pod 都会被配置上内存资源限制。
5
7
---
6
8
7
9
<!--
8
10
title: Configure Default Memory Requests and Limits for a Namespace
9
11
content_type: task
10
12
weight: 10
13
+ description: >-
14
+ Define a default memory resource limit for a namespace, so that every new Pod
15
+ in that namespace has a memory resource limit configured.
11
16
-->
12
17
13
18
<!-- overview -->
14
19
15
20
<!--
16
- This page shows how to configure default memory requests and limits for a namespace.
17
- If a Container is created in a namespace that has a default memory limit, and the Container
18
- does not specify its own memory limit, then the Container is assigned the default memory limit.
21
+ This page shows how to configure default memory requests and limits for a
22
+ {{< glossary_tooltip text="namespace" term_id="namespace" >}}.
23
+
24
+ A Kubernetes cluster can be divided into namespaces. Once you have a namespace that
25
+ has a default memory
26
+ [limit](/docs/concepts/configuration/manage-resources-containers/#requests-and-limits),
27
+ and you then try to create a Pod with a container that does not specify its own memory
28
+ limit, then the
29
+ {{< glossary_tooltip text="control plane" term_id="control-plane" >}} assigns the default
30
+ memory limit to that container.
31
+
19
32
Kubernetes assigns a default memory request under certain conditions that are explained later in this topic.
20
33
-->
34
+ 本章介绍如何为{{< glossary_tooltip text="命名空间" term_id="namespace" >}}配置默认的内存请求和限制。
35
+
36
+ 一个 Kubernetes 集群可被划分为多个命名空间。
37
+ 如果你在具有默认内存[ 限制] ( /zh/docs/concepts/configuration/manage-resources-containers/#requests-and-limits )
38
+ 的命名空间内尝试创建一个 Pod,并且这个 Pod 中的容器没有声明自己的内存资源限制,
39
+ 那么{{< glossary_tooltip text="控制面" term_id="control-plane" >}}会为该容器设定默认的内存限制。
21
40
22
- 本文介绍怎样给命名空间配置默认的内存请求和限制。
23
- 如果在一个有默认内存限制的命名空间创建容器,该容器没有声明自己的内存限制时,
24
- 将会被指定默认内存限制。
25
41
Kubernetes 还为某些情况指定了默认的内存请求,本章后面会进行介绍。
26
42
27
43
## {{% heading "prerequisites" %}}
28
44
29
- {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
45
+ {{< include "task-tutorial-prereqs.md" >}}
30
46
31
47
<!--
48
+ You must have access to create namespaces in your cluster.
49
+
32
50
Each node in your cluster must have at least 2 GiB of memory.
33
51
-->
52
+ 在你的集群里你必须要有创建命名空间的权限。
53
+
34
54
你的集群中的每个节点必须至少有 2 GiB 的内存。
35
55
36
56
<!-- steps -->
@@ -52,12 +72,14 @@ kubectl create namespace default-mem-example
52
72
<!--
53
73
## Create a LimitRange and a Pod
54
74
55
- Here's the configuration file for a LimitRange object. The configuration specifies
56
- a default memory request and a default memory limit.
75
+ Here's a manifest for an example {{< glossary_tooltip text="LimitRange" term_id="limitrange" >}}.
76
+ The manifest specifies a default memory
77
+ request and a default memory limit.
57
78
-->
58
79
## 创建 LimitRange 和 Pod
59
80
60
- 这里给出了一个限制范围对象的配置文件。该配置声明了一个默认的内存请求和一个默认的内存限制。
81
+ 以下为 {{< glossary_tooltip text="LimitRange" term_id="limitrange" >}} 的示例清单。
82
+ 清单中声明了默认的内存请求和默认的内存限制。
61
83
62
84
{{< codenew file="admin/resource/memory-defaults.yaml" >}}
63
85
@@ -71,19 +93,20 @@ kubectl apply -f https://k8s.io/examples/admin/resource/memory-defaults.yaml --n
71
93
```
72
94
73
95
<!--
74
- Now if a Container is created in the default-mem-example namespace, and the
75
- Container does not specify its own values for memory request and memory limit,
76
- the Container is given a default memory request of 256 MiB and a default
77
- memory limit of 512 MiB .
96
+ Now if you create a Pod in the default-mem-example namespace, and any container
97
+ within that Pod does not specify its own values for memory request and memory limit,
98
+ then the {{< glossary_tooltip text="control plane" term_id="control-plane" >}}
99
+ applies default values: a memory request of 256MiB and a memory limit of 512MiB .
78
100
79
- Here's the configuration file for a Pod that has one Container . The Container
101
+ Here's an example manifest for a Pod that has one container . The container
80
102
does not specify a memory request and limit.
81
103
-->
82
- 现在,如果在 default-mem-example 命名空间创建容器,并且该容器没有声明自己的内存请求和限制值,
83
- 它将被指定默认的内存请求 256 MiB 和默认的内存限制 512 MiB。
104
+ 现在如果你在 default-mem-example 命名空间中创建一个 Pod,
105
+ 并且该 Pod 中所有容器都没有声明自己的内存请求和内存限制,
106
+ {{< glossary_tooltip text="控制面" term_id="control-plane" >}}
107
+ 会将内存的默认请求值 256MiB 和默认限制值 512MiB 应用到 Pod 上。
84
108
85
- 下面是具有一个容器的 Pod 的配置文件。
86
- 容器未指定内存请求和限制。
109
+ 以下为只包含一个容器的 Pod 的清单。该容器没有声明内存请求和限制。
87
110
88
111
{{< codenew file="admin/resource/memory-defaults-pod.yaml" >}}
89
112
@@ -106,7 +129,7 @@ kubectl get pod default-mem-demo --output=yaml --namespace=default-mem-example
106
129
```
107
130
108
131
<!--
109
- The output shows that the Pod's Container has a memory request of 256 MiB and
132
+ The output shows that the Pod's container has a memory request of 256 MiB and
110
133
a memory limit of 512 MiB. These are the default values specified by the LimitRange.
111
134
-->
112
135
输出内容显示该 Pod 的容器有 256 MiB 的内存请求和 512 MiB 的内存限制。
@@ -134,14 +157,14 @@ kubectl delete pod default-mem-demo --namespace=default-mem-example
134
157
```
135
158
136
159
<!--
137
- ## What if you specify a Container 's limit, but not its request?
160
+ ## What if you specify a container 's limit, but not its request?
138
161
139
- Here's the configuration file for a Pod that has one Container . The Container
162
+ Here's a manifest for a Pod that has one container . The container
140
163
specifies a memory limit, but not a request:
141
164
-->
142
165
## 声明容器的限制而不声明它的请求会怎么样?
143
166
144
- 这里给出了包含一个容器的 Pod 的配置文件 。该容器声明了内存限制,而没有声明内存请求:
167
+ 以下为只包含一个容器的 Pod 的清单 。该容器声明了内存限制,而没有声明内存请求。
145
168
146
169
{{< codenew file="admin/resource/memory-defaults-pod-2.yaml" >}}
147
170
@@ -164,8 +187,8 @@ kubectl get pod default-mem-demo-2 --output=yaml --namespace=default-mem-example
164
187
```
165
188
166
189
<!--
167
- The output shows that the Container 's memory request is set to match its memory limit.
168
- Notice that the Container was not assigned the default memory request value of 256Mi.
190
+ The output shows that the container 's memory request is set to match its memory limit.
191
+ Notice that the container was not assigned the default memory request value of 256Mi.
169
192
-->
170
193
输出结果显示容器的内存请求被设置为它的内存限制相同的值。注意该容器没有被指定默认的内存请求值 256MiB。
171
194
@@ -178,15 +201,15 @@ resources:
178
201
```
179
202
180
203
<!--
181
- ## What if you specify a Container 's request, but not its limit?
204
+ ## What if you specify a container 's request, but not its limit?
182
205
-->
183
206
## 声明容器的内存请求而不声明内存限制会怎么样?
184
207
185
208
<!--
186
- Here's the configuration file for a Pod that has one Container . The Container
209
+ Here's a manifest for a Pod that has one container . The container
187
210
specifies a memory request, but not a limit:
188
211
-->
189
- 这里给出了一个包含一个容器的 Pod 的配置文件 。该容器声明了内存请求,但没有内存限制:
212
+ 以下为只包含一个容器的 Pod 的清单 。该容器声明了内存请求,但没有内存限制:
190
213
191
214
{{< codenew file="admin/resource/memory-defaults-pod-3.yaml" >}}
192
215
@@ -209,12 +232,12 @@ kubectl get pod default-mem-demo-3 --output=yaml --namespace=default-mem-example
209
232
```
210
233
211
234
<!--
212
- The output shows that the Container 's memory request is set to the value specified in the
213
- Container 's configuration file . The Container's memory limit is set to 512Mi, which is the
214
- default memory limit for the namespace.
235
+ The output shows that the container 's memory request is set to the value specified in the
236
+ container 's manifest . The container is limited to use no more than 512MiB of
237
+ memory, which matches the default memory limit for the namespace.
215
238
-->
216
- 输出结果显示该容器的内存请求被设置为了容器配置文件中声明的数值 。
217
- 容器的内存限制被设置为 512MiB,即命名空间的默认内存限制 。
239
+ 输出结果显示所创建的 Pod 中,容器的内存请求为 Pod 清单中声明的值 。
240
+ 然而同一容器的内存限制被设置为 512MiB,此值是该命名空间的默认内存限制值 。
218
241
219
242
```
220
243
resources:
@@ -227,27 +250,45 @@ resources:
227
250
<!--
228
251
## Motivation for default memory limits and requests
229
252
230
- If your namespace has a resource quota,
253
+ If your namespace has a memory {{< glossary_tooltip text="resource quota" term_id="resource-quota" >}}
254
+ configured,
231
255
it is helpful to have a default value in place for memory limit.
232
- Here are two of the restrictions that a resource quota imposes on a namespace:
256
+ Here are three of the restrictions that a resource quota imposes on a namespace:
257
+
258
+ * For every Pod that runs in the namespace, the Pod and each of its containers must have a memory limit.
259
+ (If you specify a memory limit for every container in a Pod, Kubernetes can infer the Pod-level memory
260
+ limit by adding up the limits for its containers).
261
+ * Memory limits apply a resource reservation on the node where the Pod in question is scheduled.
262
+ The total amount of memory reserved for all Pods in the namespace must not exceed a specified limit.
263
+ * The total amount of memory actually used by all Pods in the namespace must also not exceed a specified limit.
233
264
-->
234
265
## 设置默认内存限制和请求的动机
235
266
236
- 如果你的命名空间有资源配额,那么默认内存限制是很有帮助的。
237
- 下面是一个例子,通过资源配额为命名空间设置两项约束:
267
+ 如果你的命名空间设置了内存 {{< glossary_tooltip text="资源配额" term_id="resource-quota" >}},
268
+ 那么为内存限制设置一个默认值会很有帮助。
269
+ 以下是内存资源配额对命名空间的施加的三条限制:
238
270
239
- <!--
240
- * Every Container that runs in the namespace must have its own memory limit.
241
- * The total amount of memory used by all Containers in the namespace must not exceed a specified limit.
242
- -->
243
- * 运行在命名空间中的每个容器必须有自己的内存限制。
244
- * 命名空间中所有容器的内存使用量之和不能超过声明的限制值。
271
+ * 命名空间中运行的每个 Pod 中的容器都必须有内存限制。
272
+ (如果为 Pod 中的每个容器声明了内存限制,
273
+ Kubernetes 可以通过将其容器的内存限制相加推断出 Pod 级别的内存限制)。
274
+
275
+ * 内存限制用来在 Pod 被调度到的节点上执行资源预留。
276
+ 预留给命名空间中所有 Pod 使用的内存总量不能超过规定的限制。
277
+
278
+ * 命名空间中所有 Pod 实际使用的内存总量也不能超过规定的限制。
245
279
246
280
<!--
247
- If a Container does not specify its own memory limit, it is given the default limit, and then
248
- it can be allowed to run in a namespace that is restricted by a quota.
281
+ When you add a LimitRange:
282
+
283
+ If any Pod in that namespace that includes a container does not specify its own memory limit,
284
+ the control plane applies the default memory limit to that container, and the Pod can be
285
+ allowed to run in a namespace that is restricted by a memory ResourceQuota.
249
286
-->
250
- 如果一个容器没有声明自己的内存限制,会被指定默认限制,然后它才会被允许在限定了配额的命名空间中运行。
287
+ 当你添加 LimitRange 时:
288
+
289
+ 如果该命名空间中的任何 Pod 的容器未指定内存限制,
290
+ 控制面将默认内存限制应用于该容器,
291
+ 这样 Pod 可以在受到内存 ResourceQuota 限制的命名空间中运行。
251
292
252
293
<!--
253
294
## Clean up
0 commit comments