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
+73-42Lines changed: 73 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,11 +16,10 @@ weight: 40
16
16
<!--
17
17
_Labels_ are key/value pairs that are attached to objects, such as pods.
18
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.
20
-
Labels can be attached to objects at creation time and subsequently added and modified at any time.
21
-
Each object can have a set of key/value labels defined. Each Key must be unique for a given object.
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.
22
21
-->
23
-
_标签(Labels)_是附加到 Kubernetes 对象(比如 Pods)上的键值对。
22
+
**标签(Labels)**是附加到 Kubernetes 对象(比如 Pods)上的键值对。
24
23
标签旨在用于指定对用户有意义且相关的对象的标识属性,但不直接对核心系统有语义含义。
25
24
标签可以用于组织和选择对象的子集。标签可以在创建时附加到对象,随后可以随时添加和修改。
26
25
每个对象都可以定义一组键/值标签。每个键对于给定对象必须是唯一的。
@@ -49,7 +48,7 @@ and CLIs. Non-identifying information should be recorded using
49
48
50
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.
51
50
-->
52
-
## 动机
51
+
## 动机 {#motivation}
53
52
54
53
标签使用户能够以松散耦合的方式将他们自己的组织结构映射到系统对象,而无需客户端存储这些映射。
55
54
@@ -78,15 +77,15 @@ These are examples of [commonly used labels](/docs/concepts/overview/working-wit
78
77
<!--
79
78
## Syntax and character set
80
79
81
-
_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 (`/`).
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 (`/`).
82
81
83
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.
84
83
85
84
The `kubernetes.io/` and `k8s.io/` prefixes are [reserved](/docs/reference/labels-annotations-taints/) for Kubernetes core components.
前缀是可选的。如果指定,前缀必须是 DNS 子域:由点(`.`)分隔的一系列 DNS 标签,总共不超过 253 个字符,
@@ -111,10 +110,33 @@ Valid label value:
111
110
* 除非标签值为空,必须以字母数字字符(`[a-z0-9A-Z]`)开头和结尾
112
111
* 包含破折号(`-`)、下划线(`_`)、点(`.`)和字母或数字
113
112
113
+
<!--
114
+
For example, here's the configuration file for a Pod that has two labels `environment: production` and `app: nginx` :
115
+
-->
116
+
例如,这是一个有 `environment: production` 和 `app: nginx` 标签的 Pod 配置文件:
117
+
118
+
```yaml
119
+
120
+
apiVersion: v1
121
+
kind: Pod
122
+
metadata:
123
+
name: label-demo
124
+
labels:
125
+
environment: production
126
+
app: nginx
127
+
spec:
128
+
containers:
129
+
- name: nginx
130
+
image: nginx:1.14.2
131
+
ports:
132
+
- containerPort: 80
133
+
134
+
```
135
+
114
136
<!--
115
137
## Label selectors
116
138
117
-
Unlike [names and UIDs](/docs/user-guide/identifiers), labels do not provide uniqueness. In general, we expect many objects to carry the same label(s).
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).
118
140
-->
119
141
## 标签选择算符 {#label-selectors}
120
142
@@ -124,15 +146,15 @@ Unlike [names and UIDs](/docs/user-guide/identifiers), labels do not provide uni
124
146
<!--
125
147
Via a _label selector_, the client/user can identify a set of objects. The label selector is the core grouping primitive in Kubernetes.
The API currently supports two types of selectors: _equality-based_ and _set-based_.
131
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.
132
154
-->
133
-
API 目前支持两种类型的选择算符:_基于等值的_ 和 _基于集合的_。
134
-
标签选择算符可以由逗号分隔的多个_需求_组成。
135
-
在多个需求的情况下,必须满足所有要求,因此逗号分隔符充当逻辑_与_(`&&`)运算符。
155
+
API 目前支持两种类型的选择算符:**基于等值的**和**基于集合的**。
156
+
标签选择算符可以由逗号分隔的多个**需求**组成。
157
+
在多个需求的情况下,必须满足所有要求,因此逗号分隔符充当逻辑**与**(`&&`)运算符。
136
158
137
159
<!--
138
160
The semantics of empty or non-specified selectors are dependent on the context,
@@ -142,18 +164,18 @@ them.
142
164
空标签选择算符或者未指定的选择算符的语义取决于上下文,
143
165
支持使用选择算符的 API 类别应该将算符的合法性和含义用文档记录下来。
144
166
167
+
{{< note >}}
145
168
<!--
146
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.
147
170
-->
148
-
{{< note >}}
149
171
对于某些 API 类别(例如 ReplicaSet)而言,两个实例的标签选择算符不得在命名空间内重叠,
150
172
否则它们的控制器将互相冲突,无法确定应该存在的副本个数。
151
173
{{< /note >}}
152
174
175
+
{{< caution >}}
153
176
<!--
154
177
For both equality-based and set-based conditions there is no logical _OR_ (`||`) operator. Ensure your filter statements are structured accordingly.
155
178
-->
156
-
{{< caution >}}
157
179
对于基于等值的和基于集合的条件而言,不存在逻辑或(`||`)操作符。
158
180
你要确保你的过滤语句按合适的方式组织。
159
181
{{< /caution >}}
@@ -162,14 +184,14 @@ For both equality-based and set-based conditions there is no logical _OR_ (`||`)
162
184
### _Equality-based_ requirement
163
185
164
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.
165
-
Three kinds of operators are admitted `=`,`==`,`!=`. The first two represent _equality_ (and are simply synonyms), while the latter represents _inequality_. For example:
187
+
Three kinds of operators are admitted `=`,`==`,`!=`. The first two represent _equality_ (and are synonyms), while the latter represents _inequality_. For example:
166
188
-->
167
-
### _基于等值的_需求
189
+
### **基于等值的**需求
168
190
169
-
_基于等值_ 或 _基于不等值_的需求允许按标签键和值进行过滤。
191
+
**基于等值**或**基于不等值**的需求允许按标签键和值进行过滤。
170
192
匹配对象必须满足所有指定的标签约束,尽管它们也可能具有其他标签。
171
-
可接受的运算符有 `=`、`==` 和 `!=` 三种。
172
-
前两个表示_相等_(并且只是同义词),而后者表示_不相等_。例如:
193
+
可接受的运算符有 `=`、`==` 和 `!=` 三种。
194
+
前两个表示**相等**(并且是同义词),而后者表示**不相等**。例如:
173
195
174
196
```
175
197
environment = production
@@ -214,9 +236,9 @@ spec:
214
236
215
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:
216
238
-->
217
-
### _基于集合_ 的需求
239
+
### **基于集合**的需求
218
240
219
-
_基于集合_ 的标签需求允许你通过一组值来过滤键。
241
+
**基于集合**的标签需求允许你通过一组值来过滤键。
220
242
支持三种操作符:`in`、`notin` 和 `exists`(只可以用在键标识符上)。例如:
221
243
222
244
```
@@ -240,19 +262,19 @@ Similarly the comma separator acts as an _AND_ operator. So filtering resources
The _set-based_ label selector is a general form of equality since `environment=production` is equivalent to `environment in (production)`; similarly for `!=` and `notin`.
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:
@@ -335,15 +361,17 @@ Labels selectors for both objects are defined in `json` or `yaml` files using ma
335
361
应该管理的 pods 的数量也是由标签选择算符定义的。
336
362
337
363
两个对象的标签选择算符都是在 `json` 或者 `yaml` 文件中使用映射定义的,并且只支持
338
-
_基于等值_需求的选择算符:
364
+
**基于等值**需求的选择算符:
339
365
340
366
```json
341
367
"selector": {
342
368
"component" : "redis",
343
369
}
344
370
```
345
371
346
-
<!-- or -->
372
+
<!--
373
+
or
374
+
-->
347
375
或者
348
376
349
377
```yaml
@@ -359,15 +387,19 @@ this selector (respectively in `json` or `yaml` format) is equivalent to `compon
359
387
<!--
360
388
#### Resources that support set-based requirements
361
389
362
-
Newer resources, such as [`Job`](/docs/concepts/jobs/run-to-completion-finite-workloads/), [`Deployment`](/docs/concepts/workloads/controllers/deployment/), [`Replica Set`](/docs/concepts/workloads/controllers/replicaset/), and [`Daemon Set`](/docs/concepts/workloads/controllers/daemonset/), support _set-based_ requirements as well.
390
+
Newer resources, such as [`Job`](/docs/concepts/workloads/controllers/job/),
`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.
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.
383
415
-->
384
416
385
417
`matchLabels`是由 `{key,value}` 对组成的映射。
@@ -395,10 +427,9 @@ selector:
395
427
#### Selecting sets of nodes
396
428
397
429
One use case for selecting over labels is to constrain the set of nodes onto which a pod can schedule.
398
-
See the documentation on [node selection](/docs/concepts/configuration/assign-pod-node/) for more information.
430
+
See the documentation on [node selection](/docs/concepts/scheduling-eviction/assign-pod-node/) for more information.
0 commit comments