Skip to content

Commit 1644e90

Browse files
authored
Merge pull request #39198 from Zhuzhenghao/labels
[zh] Resync working-with-objects/labels.md
2 parents a83b2cd + 1a41abb commit 1644e90

File tree

1 file changed

+99
-40
lines changed
  • content/zh-cn/docs/concepts/overview/working-with-objects

1 file changed

+99
-40
lines changed

content/zh-cn/docs/concepts/overview/working-with-objects/labels.md

Lines changed: 99 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ weight: 40
1515

1616
<!--
1717
_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.
2124
-->
2225
**标签(Labels)** 是附加到 Kubernetes 对象(比如 Pod)上的键值对。
2326
标签旨在用于指定对用户有意义且相关的对象的标识属性,但不直接对核心系统有语义含义。
@@ -46,14 +49,19 @@ and CLIs. Non-identifying information should be recorded using
4649
<!--
4750
## Motivation
4851
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.
5054
-->
5155
## 动机 {#motivation}
5256

5357
标签使用户能够以松散耦合的方式将他们自己的组织结构映射到系统对象,而无需客户端存储这些映射。
5458

5559
<!--
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.
5765
5866
Example labels:
5967
-->
@@ -69,19 +77,32 @@ Example labels:
6977
* `"track" : "daily"`, `"track" : "weekly"`
7078

7179
<!--
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.
7384
-->
7485
有一些[常用标签](/zh-cn/docs/concepts/overview/working-with-objects/common-labels/)的例子;你可以任意制定自己的约定。
7586
请记住,标签的 Key 对于给定对象必须是唯一的。
7687

7788
<!--
7889
## Syntax and character set
7990
80-
_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 (`/`).
8198
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.
83103
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.
85106
-->
86107
## 语法和字符集 {#syntax-and-character-set}
87108

@@ -111,7 +132,8 @@ Valid label value:
111132
* 包含破折号(`-`)、下划线(`_`)、点(`.`)和字母或数字
112133

113134
<!--
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`:
115137
-->
116138
例如,这是一个有 `environment: production``app: nginx` 标签的 Pod 配置文件:
117139

@@ -136,21 +158,25 @@ spec:
136158
<!--
137159
## Label selectors
138160
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).
140163
-->
141164
## 标签选择算符 {#label-selectors}
142165

143166
[名称和 UID](/zh-cn/docs/concepts/overview/working-with-objects/names/) 不同,
144167
标签不支持唯一性。通常,我们希望许多对象携带相同的标签。
145168

146169
<!--
147-
Via a _label selector_, the client/user can identify a set of objects. The label selector is the core grouping primitive in Kubernetes.
170+
Via a _label selector_, the client/user can identify a set of objects.
171+
The label selector is the core grouping primitive in Kubernetes.
148172
-->
149173
通过**标签选择算符**,客户端/用户可以识别一组对象。标签选择算符是 Kubernetes 中的核心分组原语。
150174

151175
<!--
152176
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.
154180
-->
155181
API 目前支持两种类型的选择算符:**基于等值的****基于集合的**
156182
标签选择算符可以由逗号分隔的多个**需求**组成。
@@ -166,15 +192,18 @@ them.
166192

167193
{{< note >}}
168194
<!--
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.
170198
-->
171199
对于某些 API 类别(例如 ReplicaSet)而言,两个实例的标签选择算符不得在命名空间内重叠,
172200
否则它们的控制器将互相冲突,无法确定应该存在的副本个数。
173201
{{< /note >}}
174202

175203
{{< caution >}}
176204
<!--
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.
178207
-->
179208
对于基于等值的和基于集合的条件而言,不存在逻辑或(`||`)操作符。
180209
你要确保你的过滤语句按合适的方式组织。
@@ -183,8 +212,11 @@ For both equality-based and set-based conditions there is no logical _OR_ (`||`)
183212
<!--
184213
### _Equality-based_ requirement
185214
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:
188220
-->
189221
### **基于等值的**需求
190222

@@ -200,8 +232,9 @@ tier != frontend
200232

201233
<!--
202234
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`
205238
-->
206239
前者选择所有资源,其键名等于 `environment`,值等于 `production`
207240
后者选择所有资源,其键名等于 `tier`,值不同于 `frontend`,所有资源都没有带有 `tier` 键的标签。
@@ -234,7 +267,9 @@ spec:
234267
<!--
235268
### _Set-based_ requirement
236269
237-
_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:
238273
-->
239274
### **基于集合**的需求
240275

@@ -249,30 +284,39 @@ partition
249284
```
250285
251286
<!--
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.
256295
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)`.
258299
-->
259300
260-
* 第一个示例选择了所有键等于 `environment` 并且值等于 `production` 或者 `qa` 的资源。
261-
* 第二个示例选择了所有键等于 `tier` 并且值不等于 `frontend` 或者 `backend` 的资源,以及所有没有 `tier` 键标签的资源。
262-
* 第三个示例选择了所有包含了有 `partition` 标签的资源;没有校验它的值。
263-
* 第四个示例选择了所有没有 `partition` 标签的资源;没有校验它的值。
301+
- 第一个示例选择了所有键等于 `environment` 并且值等于 `production` 或者 `qa` 的资源。
302+
- 第二个示例选择了所有键等于 `tier` 并且值不等于 `frontend` 或者 `backend` 的资源,以及所有没有 `tier` 键标签的资源。
303+
- 第三个示例选择了所有包含了有 `partition` 标签的资源;没有校验它的值。
304+
- 第四个示例选择了所有没有 `partition` 标签的资源;没有校验它的值。
264305
265306
类似地,逗号分隔符充当**与**运算符。因此,使用 `partition` 键(无论为何值)和
266307
`environment` 不同于 `qa` 来过滤资源可以使用 `partition, environment notin (qa)` 来实现。
267308
268309
<!--
269-
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)`;
312+
similarly for `!=` and `notin`.
270313
-->
271314
**基于集合**的标签选择算符是相等标签选择算符的一般形式,因为 `environment=production`
272315
等同于 `environment in (production)`;`!=` 和 `notin` 也是类似的。
273316
274317
<!--
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`.
276320
-->
277321
**基于集合**的要求可以与基于**相等**的要求混合使用。例如:`partition in (customerA, customerB),environment!=qa`。
278322
@@ -281,7 +325,9 @@ _Set-based_ requirements can be mixed with _equality-based_ requirements. For ex
281325
<!--
282326
### LIST and WATCH filtering
283327
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):
285331
-->
286332
### LIST 和 WATCH 过滤
287333
@@ -296,7 +342,8 @@ LIST 和 WATCH 操作可以使用查询参数指定标签选择算符过滤一
296342
* **基于集合**的需求:`?labelSelector=environment+in+%28production%2Cqa%29%2Ctier+in+%28frontend%29`
297343
298344
<!--
299-
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:
300347
-->
301348
两种标签选择算符都可以通过 REST 客户端用于 list 或者 watch 资源。
302349
例如,使用 `kubectl` 定位 `apiserver`,可以使用**基于等值**的标签选择算符可以这么写:
@@ -316,7 +363,8 @@ kubectl get pods -l 'environment in (production),tier in (frontend)'
316363
```
317364

318365
<!--
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:
320368
-->
321369
正如刚才提到的,**基于集合**的需求更具有表达力。例如,它们可以实现值的****操作:
322370

@@ -351,9 +399,12 @@ also use label selectors to specify sets of other resources, such as
351399
<!--
352400
#### Service and ReplicationController
353401
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.
355405
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:
357408
-->
358409
#### Service 和 ReplicationController
359410

@@ -380,7 +431,8 @@ selector:
380431
```
381432
382433
<!---
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)`.
384436
-->
385437
这个选择算符(分别在 `json` 或者 `yaml` 格式中)等价于 `component=redis` 或 `component in (redis)`。
386438

@@ -411,7 +463,13 @@ selector:
411463
```
412464

413465
<!--
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.
415473
-->
416474

417475
`matchLabels` 是由 `{key,value}` 对组成的映射。
@@ -426,8 +484,9 @@ selector:
426484
<!--
427485
#### Selecting sets of nodes
428486

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.
431490
-->
432491
#### 选择节点集
433492

0 commit comments

Comments
 (0)