You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/zh-cn/docs/concepts/overview/working-with-objects/labels.md
+99-40Lines changed: 99 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,9 +15,12 @@ weight: 40
15
15
16
16
<!--
17
17
_Labels_ are key/value pairs that are attached to objects, such as pods.
18
-
Labels are intended to be used to specify identifying attributes of objects that are meaningful and relevant to users, but do not directly imply semantics to the core system.
19
-
Labels can be used to organize and to select subsets of objects. Labels can be attached to objects at creation time and subsequently added and modified at any time.
20
-
Each object can have a set of key/value labels defined. Each Key must be unique for a given object.
18
+
Labels are intended to be used to specify identifying attributes of objects
19
+
that are meaningful and relevant to users, but do not directly imply semantics
20
+
to the core system. Labels can be used to organize and to select subsets of
21
+
objects. Labels can be attached to objects at creation time and subsequently
22
+
added and modified at any time. Each object can have a set of key/value labels
23
+
defined. Each Key must be unique for a given object.
21
24
-->
22
25
**标签(Labels)** 是附加到 Kubernetes 对象(比如 Pod)上的键值对。
23
26
标签旨在用于指定对用户有意义且相关的对象的标识属性,但不直接对核心系统有语义含义。
@@ -46,14 +49,19 @@ and CLIs. Non-identifying information should be recorded using
46
49
<!--
47
50
## Motivation
48
51
49
-
Labels enable users to map their own organizational structures onto system objects in a loosely coupled fashion, without requiring clients to store these mappings.
52
+
Labels enable users to map their own organizational structures onto system objects
53
+
in a loosely coupled fashion, without requiring clients to store these mappings.
50
54
-->
51
55
## 动机 {#motivation}
52
56
53
57
标签使用户能够以松散耦合的方式将他们自己的组织结构映射到系统对象,而无需客户端存储这些映射。
54
58
55
59
<!--
56
-
Service deployments and batch processing pipelines are often multi-dimensional entities (e.g., multiple partitions or deployments, multiple release tracks, multiple tiers, multiple micro-services per tier). Management often requires cross-cutting operations, which breaks encapsulation of strictly hierarchical representations, especially rigid hierarchies determined by the infrastructure rather than by users.
60
+
Service deployments and batch processing pipelines are often multi-dimensional entities
61
+
(e.g., multiple partitions or deployments, multiple release tracks, multiple tiers,
62
+
multiple micro-services per tier). Management often requires cross-cutting operations,
63
+
which breaks encapsulation of strictly hierarchical representations, especially rigid
64
+
hierarchies determined by the infrastructure rather than by users.
57
65
58
66
Example labels:
59
67
-->
@@ -69,19 +77,32 @@ Example labels:
69
77
*`"track" : "daily"`, `"track" : "weekly"`
70
78
71
79
<!--
72
-
These are examples of [commonly used labels](/docs/concepts/overview/working-with-objects/common-labels/); you are free to develop your own conventions. Keep in mind that label Key must be unique for a given object.
80
+
These are examples of
81
+
[commonly used labels](/docs/concepts/overview/working-with-objects/common-labels/);
82
+
you are free to develop your own conventions.
83
+
Keep in mind that label Key must be unique for a given object.
_Labels_ are key/value pairs. Valid label keys have two segments: an optional prefix and name, separated by a slash (`/`). The name segment is required and must be 63 characters or less, beginning and ending with an alphanumeric character (`[a-z0-9A-Z]`) with dashes (`-`), underscores (`_`), dots (`.`), and alphanumerics between. The prefix is optional. If specified, the prefix must be a DNS subdomain: a series of DNS labels separated by dots (`.`), not longer than 253 characters in total, followed by a slash (`/`).
91
+
_Labels_ are key/value pairs. Valid label keys have two segments: an optional
92
+
prefix and name, separated by a slash (`/`). The name segment is required and
93
+
must be 63 characters or less, beginning and ending with an alphanumeric
94
+
character (`[a-z0-9A-Z]`) with dashes (`-`), underscores (`_`), dots (`.`),
95
+
and alphanumerics between. The prefix is optional. If specified, the prefix
96
+
must be a DNS subdomain: a series of DNS labels separated by dots (`.`),
97
+
not longer than 253 characters in total, followed by a slash (`/`).
81
98
82
-
If the prefix is omitted, the label Key is presumed to be private to the user. Automated system components (e.g. `kube-scheduler`, `kube-controller-manager`, `kube-apiserver`, `kubectl`, or other third-party automation) which add labels to end-user objects must specify a prefix.
99
+
If the prefix is omitted, the label Key is presumed to be private to the user.
100
+
Automated system components (e.g. `kube-scheduler`, `kube-controller-manager`,
101
+
`kube-apiserver`, `kubectl`, or other third-party automation) which add labels
102
+
to end-user objects must specify a prefix.
83
103
84
-
The `kubernetes.io/` and `k8s.io/` prefixes are [reserved](/docs/reference/labels-annotations-taints/) for Kubernetes core components.
104
+
The `kubernetes.io/` and `k8s.io/` prefixes are
105
+
[reserved](/docs/reference/labels-annotations-taints/) for Kubernetes core components.
85
106
-->
86
107
## 语法和字符集 {#syntax-and-character-set}
87
108
@@ -111,7 +132,8 @@ Valid label value:
111
132
* 包含破折号(`-`)、下划线(`_`)、点(`.`)和字母或数字
112
133
113
134
<!--
114
-
For example, here's the configuration file for a Pod that has two labels `environment: production` and `app: nginx` :
135
+
For example, here's the configuration file for a Pod that has two labels
136
+
`environment: production` and `app: nginx`:
115
137
-->
116
138
例如,这是一个有 `environment: production` 和 `app: nginx` 标签的 Pod 配置文件:
117
139
@@ -136,21 +158,25 @@ spec:
136
158
<!--
137
159
## Label selectors
138
160
139
-
Unlike [names and UIDs](/docs/concepts/overview/working-with-objects/names/), labels do not provide uniqueness. In general, we expect many objects to carry the same label(s).
161
+
Unlike [names and UIDs](/docs/concepts/overview/working-with-objects/names/), labels
162
+
do not provide uniqueness. In general, we expect many objects to carry the same label(s).
The API currently supports two types of selectors: _equality-based_ and _set-based_.
153
-
A label selector can be made of multiple _requirements_ which are comma-separated. In the case of multiple requirements, all must be satisfied so the comma separator acts as a logical _AND_ (`&&`) operator.
177
+
A label selector can be made of multiple _requirements_ which are comma-separated.
178
+
In the case of multiple requirements, all must be satisfied so the comma separator
179
+
acts as a logical _AND_ (`&&`) operator.
154
180
-->
155
181
API 目前支持两种类型的选择算符:**基于等值的**和**基于集合的**。
156
182
标签选择算符可以由逗号分隔的多个**需求**组成。
@@ -166,15 +192,18 @@ them.
166
192
167
193
{{< note >}}
168
194
<!--
169
-
For some API types, such as ReplicaSets, the label selectors of two instances must not overlap within a namespace, or the controller can see that as conflicting instructions and fail to determine how many replicas should be present.
195
+
For some API types, such as ReplicaSets, the label selectors of two instances must
196
+
not overlap within a namespace, or the controller can see that as conflicting
197
+
instructions and fail to determine how many replicas should be present.
170
198
-->
171
199
对于某些 API 类别(例如 ReplicaSet)而言,两个实例的标签选择算符不得在命名空间内重叠,
172
200
否则它们的控制器将互相冲突,无法确定应该存在的副本个数。
173
201
{{< /note >}}
174
202
175
203
{{< caution >}}
176
204
<!--
177
-
For both equality-based and set-based conditions there is no logical _OR_ (`||`) operator. Ensure your filter statements are structured accordingly.
205
+
For both equality-based and set-based conditions there is no logical _OR_ (`||`) operator.
206
+
Ensure your filter statements are structured accordingly.
178
207
-->
179
208
对于基于等值的和基于集合的条件而言,不存在逻辑或(`||`)操作符。
180
209
你要确保你的过滤语句按合适的方式组织。
@@ -183,8 +212,11 @@ For both equality-based and set-based conditions there is no logical _OR_ (`||`)
183
212
<!--
184
213
### _Equality-based_ requirement
185
214
186
-
_Equality-_ or _inequality-based_ requirements allow filtering by label keys and values. Matching objects must satisfy all of the specified label constraints, though they may have additional labels as well.
187
-
Three kinds of operators are admitted `=`,`==`,`!=`. The first two represent _equality_ (and are synonyms), while the latter represents _inequality_. For example:
215
+
_Equality-_ or _inequality-based_ requirements allow filtering by label keys and values.
216
+
Matching objects must satisfy all of the specified label constraints, though they may
217
+
have additional labels as well. Three kinds of operators are admitted `=`,`==`,`!=`.
218
+
The first two represent _equality_ (and are synonyms), while the latter represents _inequality_.
219
+
For example:
188
220
-->
189
221
### **基于等值的**需求
190
222
@@ -200,8 +232,9 @@ tier != frontend
200
232
201
233
<!--
202
234
The former selects all resources with key equal to `environment` and value equal to `production`.
203
-
The latter selects all resources with key equal to `tier` and value distinct from `frontend`, and all resources with no labels with the `tier` key.
204
-
One could filter for resources in `production` excluding `frontend` using the comma operator: `environment=production,tier!=frontend`
235
+
The latter selects all resources with key equal to `tier` and value distinct from `frontend`,
236
+
and all resources with no labels with the `tier` key. One could filter for resources in `production`
237
+
excluding `frontend` using the comma operator: `environment=production,tier!=frontend`
_Set-based_ label requirements allow filtering keys according to a set of values. Three kinds of operators are supported: `in`,`notin` and `exists` (only the key identifier). For example:
270
+
_Set-based_ label requirements allow filtering keys according to a set of values.
271
+
Three kinds of operators are supported: `in`,`notin` and `exists` (only the key identifier).
272
+
For example:
238
273
-->
239
274
### **基于集合**的需求
240
275
@@ -249,30 +284,39 @@ partition
249
284
```
250
285
251
286
<!--
252
-
* The first example selects all resources with key equal to `environment` and value equal to `production` or `qa`.
253
-
* The second example selects all resources with key equal to `tier` and values other than `frontend` and `backend`, and all resources with no labels with the `tier` key.
254
-
* The third example selects all resources including a label with key `partition`; no values are checked.
255
-
* The fourth example selects all resources without a label with key `partition`; no values are checked.
287
+
- The first example selects all resources with key equal to `environment` and value
288
+
equal to `production` or `qa`.
289
+
- The second example selects all resources with key equal to `tier` and values other
290
+
than `frontend` and `backend`, and all resources with no labels with the `tier` key.
291
+
- The third example selects all resources including a label with key `partition`;
292
+
no values are checked.
293
+
- The fourth example selects all resources without a label with key `partition`;
294
+
no values are checked.
256
295
257
-
Similarly the comma separator acts as an _AND_ operator. So filtering resources with a `partition` key (no matter the value) and with `environment` different than `qa` can be achieved using `partition,environment notin (qa)`.
296
+
Similarly the comma separator acts as an _AND_ operator. So filtering resources
297
+
with a `partition` key (no matter the value) and with `environment` different
298
+
than `qa` can be achieved using `partition,environment notin (qa)`.
The _set-based_ label selector is a general form of equality since `environment=production` is equivalent to `environment in (production)`; similarly for `!=` and `notin`.
310
+
The _set-based_ label selector is a general form of equality since
311
+
`environment=production` is equivalent to `environment in (production)`;
等同于 `environment in (production)`;`!=` 和 `notin` 也是类似的。
273
316
274
317
<!--
275
-
_Set-based_ requirements can be mixed with _equality-based_ requirements. For example: `partition in (customerA, customerB),environment!=qa`.
318
+
_Set-based_ requirements can be mixed with _equality-based_ requirements.
319
+
For example: `partition in (customerA, customerB),environment!=qa`.
276
320
-->
277
321
**基于集合**的要求可以与基于**相等**的要求混合使用。例如:`partition in (customerA, customerB),environment!=qa`。
278
322
@@ -281,7 +325,9 @@ _Set-based_ requirements can be mixed with _equality-based_ requirements. For ex
281
325
<!--
282
326
### LIST and WATCH filtering
283
327
284
-
LIST and WATCH operations may specify label selectors to filter the sets of objects returned using a query parameter. Both requirements are permitted (presented here as they would appear in a URL query string):
328
+
LIST and WATCH operations may specify label selectors to filter the sets of objects
329
+
returned using a query parameter. Both requirements are permitted
330
+
(presented here as they would appear in a URL query string):
285
331
-->
286
332
### LIST 和 WATCH 过滤
287
333
@@ -296,7 +342,8 @@ LIST 和 WATCH 操作可以使用查询参数指定标签选择算符过滤一
Both label selector styles can be used to list or watch resources via a REST client. For example, targeting `apiserver` with `kubectl` and using _equality-based_ one may write:
345
+
Both label selector styles can be used to list or watch resources via a REST client.
346
+
For example, targeting `apiserver` with `kubectl` and using _equality-based_ one may write:
@@ -316,7 +363,8 @@ kubectl get pods -l 'environment in (production),tier in (frontend)'
316
363
```
317
364
318
365
<!--
319
-
As already mentioned _set-based_ requirements are more expressive. For instance, they can implement the _OR_ operator on values:
366
+
As already mentioned _set-based_ requirements are more expressive.
367
+
For instance, they can implement the _OR_ operator on values:
320
368
-->
321
369
正如刚才提到的,**基于集合**的需求更具有表达力。例如,它们可以实现值的**或**操作:
322
370
@@ -351,9 +399,12 @@ also use label selectors to specify sets of other resources, such as
351
399
<!--
352
400
#### Service and ReplicationController
353
401
354
-
The set of pods that a `service` targets is defined with a label selector. Similarly, the population of pods that a `replicationcontroller` should manage is also defined with a label selector.
402
+
The set of pods that a `service` targets is defined with a label selector.
403
+
Similarly, the population of pods that a `replicationcontroller` should
404
+
manage is also defined with a label selector.
355
405
356
-
Labels selectors for both objects are defined in `json` or `yaml` files using maps, and only _equality-based_ requirement selectors are supported:
406
+
Labels selectors for both objects are defined in `json` or `yaml` files using maps,
407
+
and only _equality-based_ requirement selectors are supported:
357
408
-->
358
409
#### Service 和 ReplicationController
359
410
@@ -380,7 +431,8 @@ selector:
380
431
```
381
432
382
433
<!---
383
-
this selector (respectively in `json` or `yaml` format) is equivalent to `component=redis` or `component in (redis)`.
434
+
This selector (respectively in `json` or `yaml` format) is equivalent to
435
+
`component=redis`or `component in (redis)`.
384
436
-->
385
437
这个选择算符(分别在 `json` 或者 `yaml` 格式中)等价于 `component=redis` 或 `component in (redis)`。
386
438
@@ -411,7 +463,13 @@ selector:
411
463
```
412
464
413
465
<!--
414
-
`matchLabels`is a map of `{key,value}` pairs. A single `{key,value}` in the `matchLabels` map is equivalent to an element of `matchExpressions`, whose `key` field is "key", the `operator` is "In", and the `values` array contains only "value". `matchExpressions` is a list of pod selector requirements. Valid operators include In, NotIn, Exists, and DoesNotExist. The values set must be non-empty in the case of In and NotIn. All of the requirements, from both `matchLabels` and `matchExpressions` are ANDed together -- they must all be satisfied in order to match.
466
+
`matchLabels`is a map of `{key,value}` pairs. A single `{key,value}` in the
467
+
`matchLabels`map is equivalent to an element of `matchExpressions`, whose `key`
468
+
field is "key", the `operator` is "In", and the `values` array contains only "value".
469
+
`matchExpressions`is a list of pod selector requirements. Valid operators include
470
+
In, NotIn, Exists, and DoesNotExist. The values set must be non-empty in the case of
471
+
In and NotIn. All of the requirements, from both `matchLabels` and `matchExpressions`
472
+
are ANDed together -- they must all be satisfied in order to match.
415
473
-->
416
474
417
475
`matchLabels`是由 `{key,value}` 对组成的映射。
@@ -426,8 +484,9 @@ selector:
426
484
<!--
427
485
#### Selecting sets of nodes
428
486
429
-
One use case for selecting over labels is to constrain the set of nodes onto which a pod can schedule.
430
-
See the documentation on [node selection](/docs/concepts/scheduling-eviction/assign-pod-node/) for more information.
487
+
One use case for selecting over labels is to constrain the set of nodes onto which
488
+
a pod can schedule. See the documentation on
489
+
[node selection](/docs/concepts/scheduling-eviction/assign-pod-node/) for more information.
0 commit comments