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
_Labels_ are key/value pairs that are attached to objects, such as pods.
12
-
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.
13
-
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.
14
-
Each object can have a set of key/value labels defined. Each Key must be unique for a given object.
12
+
Labels are intended to be used to specify identifying attributes of objects
13
+
that are meaningful and relevant to users, but do not directly imply semantics
14
+
to the core system. Labels can be used to organize and to select subsets of
15
+
objects. Labels can be attached to objects at creation time and subsequently
16
+
added and modified at any time. Each object can have a set of key/value labels
17
+
defined. Each Key must be unique for a given object.
15
18
16
19
```json
17
20
"metadata": {
@@ -30,37 +33,56 @@ and CLIs. Non-identifying information should be recorded using
30
33
31
34
## Motivation
32
35
33
-
Labels enable users to map their own organizational structures onto system objects in a loosely coupled fashion, without requiring clients to store these mappings.
36
+
Labels enable users to map their own organizational structures onto system objects
37
+
in a loosely coupled fashion, without requiring clients to store these mappings.
34
38
35
-
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.
39
+
Service deployments and batch processing pipelines are often multi-dimensional entities
40
+
(e.g., multiple partitions or deployments, multiple release tracks, multiple tiers,
41
+
multiple micro-services per tier). Management often requires cross-cutting operations,
42
+
which breaks encapsulation of strictly hierarchical representations, especially rigid
43
+
hierarchies determined by the infrastructure rather than by users.
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.
53
+
These are examples of
54
+
[commonly used labels](/docs/concepts/overview/working-with-objects/common-labels/);
55
+
you are free to develop your own conventions.
56
+
Keep in mind that label Key must be unique for a given object.
46
57
47
58
## Syntax and character set
48
59
49
-
_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 (`/`).
60
+
_Labels_ are key/value pairs. Valid label keys have two segments: an optional
61
+
prefix and name, separated by a slash (`/`). The name segment is required and
62
+
must be 63 characters or less, beginning and ending with an alphanumeric
63
+
character (`[a-z0-9A-Z]`) with dashes (`-`), underscores (`_`), dots (`.`),
64
+
and alphanumerics between. The prefix is optional. If specified, the prefix
65
+
must be a DNS subdomain: a series of DNS labels separated by dots (`.`),
66
+
not longer than 253 characters in total, followed by a slash (`/`).
50
67
51
-
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.
68
+
If the prefix is omitted, the label Key is presumed to be private to the user.
69
+
Automated system components (e.g. `kube-scheduler`, `kube-controller-manager`,
70
+
`kube-apiserver`, `kubectl`, or other third-party automation) which add labels
71
+
to end-user objects must specify a prefix.
52
72
53
-
The `kubernetes.io/` and `k8s.io/` prefixes are [reserved](/docs/reference/labels-annotations-taints/) for Kubernetes core components.
73
+
The `kubernetes.io/` and `k8s.io/` prefixes are
74
+
[reserved](/docs/reference/labels-annotations-taints/) for Kubernetes core components.
54
75
55
76
Valid label value:
77
+
56
78
* must be 63 characters or less (can be empty),
57
79
* unless empty, must begin and end with an alphanumeric character (`[a-z0-9A-Z]`),
58
80
* could contain dashes (`-`), underscores (`_`), dots (`.`), and alphanumerics between.
59
81
60
-
For example, here's the configuration file for a Pod that has two labels `environment: production` and `app: nginx` :
82
+
For example, here's the configuration file for a Pod that has two labels
83
+
`environment: production` and `app: nginx`:
61
84
62
85
```yaml
63
-
64
86
apiVersion: v1
65
87
kind: Pod
66
88
metadata:
@@ -74,43 +96,53 @@ spec:
74
96
image: nginx:1.14.2
75
97
ports:
76
98
- containerPort: 80
77
-
78
99
```
79
100
80
101
## Label selectors
81
102
82
-
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).
103
+
Unlike [names and UIDs](/docs/concepts/overview/working-with-objects/names/), labels
104
+
do not provide uniqueness. In general, we expect many objects to carry the same label(s).
83
105
84
-
Via a _label selector_, the client/user can identify a set of objects. The label selector is the core grouping primitive in Kubernetes.
106
+
Via a _label selector_, the client/user can identify a set of objects.
107
+
The label selector is the core grouping primitive in Kubernetes.
85
108
86
109
The API currently supports two types of selectors: _equality-based_ and _set-based_.
87
-
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.
110
+
A label selector can be made of multiple _requirements_ which are comma-separated.
111
+
In the case of multiple requirements, all must be satisfied so the comma separator
112
+
acts as a logical _AND_ (`&&`) operator.
88
113
89
114
The semantics of empty or non-specified selectors are dependent on the context,
90
115
and API types that use selectors should document the validity and meaning of
91
116
them.
92
117
93
118
{{< note >}}
94
-
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.
119
+
For some API types, such as ReplicaSets, the label selectors of two instances must
120
+
not overlap within a namespace, or the controller can see that as conflicting
121
+
instructions and fail to determine how many replicas should be present.
95
122
{{< /note >}}
96
123
97
124
{{< caution >}}
98
-
For both equality-based and set-based conditions there is no logical _OR_ (`||`) operator. Ensure your filter statements are structured accordingly.
125
+
For both equality-based and set-based conditions there is no logical _OR_ (`||`) operator.
126
+
Ensure your filter statements are structured accordingly.
99
127
{{< /caution >}}
100
128
101
129
### _Equality-based_ requirement
102
130
103
-
_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.
104
-
Three kinds of operators are admitted `=`,`==`,`!=`. The first two represent _equality_ (and are synonyms), while the latter represents _inequality_. For example:
131
+
_Equality-_ or _inequality-based_ requirements allow filtering by label keys and values.
132
+
Matching objects must satisfy all of the specified label constraints, though they may
133
+
have additional labels as well. Three kinds of operators are admitted `=`,`==`,`!=`.
134
+
The first two represent _equality_ (and are synonyms), while the latter represents _inequality_.
135
+
For example:
105
136
106
137
```
107
138
environment = production
108
139
tier != frontend
109
140
```
110
141
111
142
The former selects all resources with key equal to `environment` and value equal to `production`.
112
-
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.
113
-
One could filter for resources in `production` excluding `frontend` using the comma operator: `environment=production,tier!=frontend`
143
+
The latter selects all resources with key equal to `tier` and value distinct from `frontend`,
144
+
and all resources with no labels with the `tier` key. One could filter for resources in `production`
145
+
excluding `frontend` using the comma operator: `environment=production,tier!=frontend`
114
146
115
147
One usage scenario for equality-based label requirement is for Pods to specify
116
148
node selection criteria. For example, the sample Pod below selects nodes with
@@ -134,7 +166,9 @@ spec:
134
166
135
167
### _Set-based_ requirement
136
168
137
-
_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:
169
+
_Set-based_ label requirements allow filtering keys according to a set of values.
170
+
Three kinds of operators are supported: `in`,`notin` and `exists` (only the key identifier).
171
+
For example:
138
172
139
173
```
140
174
environment in (production, qa)
@@ -143,27 +177,38 @@ partition
143
177
!partition
144
178
```
145
179
146
-
* The first example selects all resources with key equal to `environment` and value equal to `production` or `qa`.
147
-
* 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.
148
-
* The third example selects all resources including a label with key `partition`; no values are checked.
149
-
* The fourth example selects all resources without a label with key `partition`; no values are checked.
150
-
151
-
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)`.
152
-
The _set-based_ label selector is a general form of equality since `environment=production` is equivalent to `environment in (production)`; similarly for `!=` and `notin`.
153
-
154
-
_Set-based_ requirements can be mixed with _equality-based_ requirements. For example: `partition in (customerA, customerB),environment!=qa`.
155
-
180
+
- The first example selects all resources with key equal to `environment` and value
181
+
equal to `production` or `qa`.
182
+
- The second example selects all resources with key equal to `tier` and values other
183
+
than `frontend` and `backend`, and all resources with no labels with the `tier` key.
184
+
- The third example selects all resources including a label with key `partition`;
185
+
no values are checked.
186
+
- The fourth example selects all resources without a label with key `partition`;
187
+
no values are checked.
188
+
189
+
Similarly the comma separator acts as an _AND_ operator. So filtering resources
190
+
with a `partition` key (no matter the value) and with `environment` different
191
+
than `qa` can be achieved using `partition,environment notin (qa)`.
192
+
The _set-based_ label selector is a general form of equality since
193
+
`environment=production`is equivalent to `environment in (production)`;
194
+
similarly for `!=` and `notin`.
195
+
196
+
_Set-based_ requirements can be mixed with _equality-based_ requirements.
197
+
For example: `partition in (customerA, customerB),environment!=qa`.
156
198
157
199
## API
158
200
159
201
### LIST and WATCH filtering
160
202
161
-
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):
203
+
LIST and WATCH operations may specify label selectors to filter the sets of objects
204
+
returned using a query parameter. Both requirements are permitted
205
+
(presented here as they would appear in a URL query string):
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:
210
+
Both label selector styles can be used to list or watch resources via a REST client.
211
+
For example, targeting `apiserver` with `kubectl` and using _equality-based_ one may write:
167
212
168
213
```shell
169
214
kubectl get pods -l environment=production,tier=frontend
@@ -175,7 +220,8 @@ or using _set-based_ requirements:
175
220
kubectl get pods -l 'environment in (production),tier in (frontend)'
176
221
```
177
222
178
-
As already mentioned _set-based_ requirements are more expressive. For instance, they can implement the _OR_ operator on values:
223
+
As already mentioned _set-based_ requirements are more expressive.
224
+
For instance, they can implement the _OR_ operator on values:
179
225
180
226
```shell
181
227
kubectl get pods -l 'environment in (production, qa)'
@@ -196,23 +242,28 @@ also use label selectors to specify sets of other resources, such as
196
242
197
243
#### Service and ReplicationController
198
244
199
-
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.
245
+
The set of pods that a `service` targets is defined with a label selector.
246
+
Similarly, the population of pods that a `replicationcontroller` should
247
+
manage is also defined with a label selector.
200
248
201
-
Labels selectors for both objects are defined in `json` or `yaml` files using maps, and only _equality-based_ requirement selectors are supported:
249
+
Labels selectors for both objects are defined in `json` or `yaml` files using maps,
250
+
and only _equality-based_ requirement selectors are supported:
202
251
203
252
```json
204
253
"selector": {
205
254
"component" : "redis",
206
255
}
207
256
```
257
+
208
258
or
209
259
210
260
```yaml
211
261
selector:
212
262
component: redis
213
263
```
214
264
215
-
this selector (respectively in `json` or `yaml` format) is equivalent to `component=redis` or `component in (redis)`.
265
+
This selector (respectively in `json` or `yaml` format) is equivalent to
266
+
`component=redis`or `component in (redis)`.
216
267
217
268
#### Resources that support set-based requirements
`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.
285
+
`matchLabels`is a map of `{key,value}` pairs. A single `{key,value}` in the
286
+
`matchLabels`map is equivalent to an element of `matchExpressions`, whose `key`
287
+
field is "key", the `operator` is "In", and the `values` array contains only "value".
288
+
`matchExpressions`is a list of pod selector requirements. Valid operators include
289
+
In, NotIn, Exists, and DoesNotExist. The values set must be non-empty in the case of
290
+
In and NotIn. All of the requirements, from both `matchLabels` and `matchExpressions`
291
+
are ANDed together -- they must all be satisfied in order to match.
235
292
236
293
#### Selecting sets of nodes
237
294
238
-
One use case for selecting over labels is to constrain the set of nodes onto which a pod can schedule.
239
-
See the documentation on [node selection](/docs/concepts/scheduling-eviction/assign-pod-node/) for more information.
295
+
One use case for selecting over labels is to constrain the set of nodes onto which
296
+
a pod can schedule. See the documentation on
297
+
[node selection](/docs/concepts/scheduling-eviction/assign-pod-node/) for more information.
0 commit comments