diff --git a/content/zh-cn/docs/concepts/overview/working-with-objects/labels.md b/content/zh-cn/docs/concepts/overview/working-with-objects/labels.md index 650eb02a4e..df23d8cf18 100644 --- a/content/zh-cn/docs/concepts/overview/working-with-objects/labels.md +++ b/content/zh-cn/docs/concepts/overview/working-with-objects/labels.md @@ -16,11 +16,10 @@ weight: 40 -_标签(Labels)_ 是附加到 Kubernetes 对象(比如 Pods)上的键值对。 +**标签(Labels)**是附加到 Kubernetes 对象(比如 Pods)上的键值对。 标签旨在用于指定对用户有意义且相关的对象的标识属性,但不直接对核心系统有语义含义。 标签可以用于组织和选择对象的子集。标签可以在创建时附加到对象,随后可以随时添加和修改。 每个对象都可以定义一组键/值标签。每个键对于给定对象必须是唯一的。 @@ -49,7 +48,7 @@ and CLIs. Non-identifying information should be recorded using Labels enable users to map their own organizational structures onto system objects in a loosely coupled fashion, without requiring clients to store these mappings. --> -## 动机 +## 动机 {#motivation} 标签使用户能够以松散耦合的方式将他们自己的组织结构映射到系统对象,而无需客户端存储这些映射。 @@ -78,15 +77,15 @@ These are examples of [commonly used labels](/docs/concepts/overview/working-wit -## 语法和字符集 +## 语法和字符集 {#syntax-and-character-set} -_标签_ 是键值对。有效的标签键有两个段:可选的前缀和名称,用斜杠(`/`)分隔。 +**标签**是键值对。有效的标签键有两个段:可选的前缀和名称,用斜杠(`/`)分隔。 名称段是必需的,必须小于等于 63 个字符,以字母数字字符(`[a-z0-9A-Z]`)开头和结尾, 带有破折号(`-`),下划线(`_`),点( `.`)和之间的字母数字。 前缀是可选的。如果指定,前缀必须是 DNS 子域:由点(`.`)分隔的一系列 DNS 标签,总共不超过 253 个字符, @@ -111,10 +110,33 @@ Valid label value: * 除非标签值为空,必须以字母数字字符(`[a-z0-9A-Z]`)开头和结尾 * 包含破折号(`-`)、下划线(`_`)、点(`.`)和字母或数字 + +例如,这是一个有 `environment: production` 和 `app: nginx` 标签的 Pod 配置文件: + +```yaml + +apiVersion: v1 +kind: Pod +metadata: + name: label-demo + labels: + environment: production + app: nginx +spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 + +``` + ## 标签选择算符 {#label-selectors} @@ -124,15 +146,15 @@ Unlike [names and UIDs](/docs/user-guide/identifiers), labels do not provide uni -通过 _标签选择算符_,客户端/用户可以识别一组对象。标签选择算符是 Kubernetes 中的核心分组原语。 +通过**标签选择算符**,客户端/用户可以识别一组对象。标签选择算符是 Kubernetes 中的核心分组原语。 -API 目前支持两种类型的选择算符:_基于等值的_ 和 _基于集合的_。 -标签选择算符可以由逗号分隔的多个 _需求_ 组成。 -在多个需求的情况下,必须满足所有要求,因此逗号分隔符充当逻辑 _与_(`&&`)运算符。 +API 目前支持两种类型的选择算符:**基于等值的**和**基于集合的**。 +标签选择算符可以由逗号分隔的多个**需求**组成。 +在多个需求的情况下,必须满足所有要求,因此逗号分隔符充当逻辑**与**(`&&`)运算符。 -{{< note >}} 对于某些 API 类别(例如 ReplicaSet)而言,两个实例的标签选择算符不得在命名空间内重叠, 否则它们的控制器将互相冲突,无法确定应该存在的副本个数。 {{< /note >}} +{{< caution >}} -{{< caution >}} 对于基于等值的和基于集合的条件而言,不存在逻辑或(`||`)操作符。 你要确保你的过滤语句按合适的方式组织。 {{< /caution >}} @@ -162,14 +184,14 @@ For both equality-based and set-based conditions there is no logical _OR_ (`||`) ### _Equality-based_ requirement _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. -Three kinds of operators are admitted `=`,`==`,`!=`. The first two represent _equality_ (and are simply synonyms), while the latter represents _inequality_. For example: +Three kinds of operators are admitted `=`,`==`,`!=`. The first two represent _equality_ (and are synonyms), while the latter represents _inequality_. For example: --> -### _基于等值的_ 需求 +### **基于等值的**需求 -_基于等值_ 或 _基于不等值_ 的需求允许按标签键和值进行过滤。 +**基于等值**或**基于不等值**的需求允许按标签键和值进行过滤。 匹配对象必须满足所有指定的标签约束,尽管它们也可能具有其他标签。 -可接受的运算符有 `=`、`==` 和 `!=` 三种。 -前两个表示 _相等_(并且只是同义词),而后者表示 _不相等_。例如: +可接受的运算符有 `=`、`==` 和 `!=` 三种。 +前两个表示**相等**(并且是同义词),而后者表示**不相等**。例如: ``` environment = production @@ -214,9 +236,9 @@ spec: _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: --> -### _基于集合_ 的需求 +### **基于集合**的需求 -_基于集合_ 的标签需求允许你通过一组值来过滤键。 +**基于集合**的标签需求允许你通过一组值来过滤键。 支持三种操作符:`in`、`notin` 和 `exists`(只可以用在键标识符上)。例如: ``` @@ -240,19 +262,19 @@ Similarly the comma separator acts as an _AND_ operator. So filtering resources * 第三个示例选择了所有包含了有 `partition` 标签的资源;没有校验它的值。 * 第四个示例选择了所有没有 `partition` 标签的资源;没有校验它的值。 -类似地,逗号分隔符充当 _与_ 运算符。因此,使用 `partition` 键(无论为何值)和 +类似地,逗号分隔符充当 **与**运算符。因此,使用 `partition` 键(无论为何值)和 `environment` 不同于 `qa` 来过滤资源可以使用 `partition, environment notin (qa)` 来实现。 -_基于集合_ 的标签选择算符是相等标签选择算符的一般形式,因为 `environment=production` +**基于集合**的标签选择算符是相等标签选择算符的一般形式,因为 `environment=production` 等同于 `environment in (production)`;`!=` 和 `notin` 也是类似的。 -_基于集合_ 的要求可以与基于 _相等_ 的要求混合使用。例如:`partition in (customerA, customerB),environment!=qa`。 +**基于集合**的要求可以与基于**相等**的要求混合使用。例如:`partition in (customerA, customerB),environment!=qa`。 ## API @@ -270,22 +292,24 @@ LIST 和 WATCH 操作可以使用查询参数指定标签选择算符过滤一 * _equality-based_ requirements: `?labelSelector=environment%3Dproduction,tier%3Dfrontend` * _set-based_ requirements: `?labelSelector=environment+in+%28production%2Cqa%29%2Ctier+in+%28frontend%29` --> -* _基于等值_ 的需求:`?labelSelector=environment%3Dproduction,tier%3Dfrontend` -* _基于集合_ 的需求:`?labelSelector=environment+in+%28production%2Cqa%29%2Ctier+in+%28frontend%29` +* **基于等值**的需求:`?labelSelector=environment%3Dproduction,tier%3Dfrontend` +* **基于集合**的需求:`?labelSelector=environment+in+%28production%2Cqa%29%2Ctier+in+%28frontend%29` 两种标签选择算符都可以通过 REST 客户端用于 list 或者 watch 资源。 -例如,使用 `kubectl` 定位 `apiserver`,可以使用 _基于等值_ 的标签选择算符可以这么写: +例如,使用 `kubectl` 定位 `apiserver`,可以使用**基于等值**的标签选择算符可以这么写: ```shell kubectl get pods -l environment=production,tier=frontend ``` - -或者使用 _基于集合的_ 需求: + +或者使用**基于集合的**需求: ```shell kubectl get pods -l 'environment in (production),tier in (frontend)' @@ -294,14 +318,16 @@ kubectl get pods -l 'environment in (production),tier in (frontend)' -正如刚才提到的,_基于集合_ 的需求更具有表达力。例如,它们可以实现值的 _或_ 操作: +正如刚才提到的,**基于集合**的需求更具有表达力。例如,它们可以实现值的**或**操作: ```shell kubectl get pods -l 'environment in (production, qa)' ``` - -或者通过 _exists_ 运算符限制不匹配: + +或者通过**exists**运算符限制不匹配: ```shell kubectl get pods -l 'environment,environment notin (frontend)' @@ -318,7 +344,7 @@ also use label selectors to specify sets of other resources, such as ### 在 API 对象中设置引用 一些 Kubernetes 对象,例如 [`services`](/zh-cn/docs/concepts/services-networking/service/) -和 [`replicationcontrollers`](/zh-cn/docs/concepts/workloads/controllers/replicationcontroller/) , +和 [`replicationcontrollers`](/zh-cn/docs/concepts/workloads/controllers/replicationcontroller/), 也使用了标签选择算符去指定了其他资源的集合,例如 [pods](/zh-cn/docs/concepts/workloads/pods/)。 @@ -335,7 +361,7 @@ Labels selectors for both objects are defined in `json` or `yaml` files using ma 应该管理的 pods 的数量也是由标签选择算符定义的。 两个对象的标签选择算符都是在 `json` 或者 `yaml` 文件中使用映射定义的,并且只支持 -_基于等值_ 需求的选择算符: +**基于等值**需求的选择算符: ```json "selector": { @@ -343,7 +369,9 @@ _基于等值_ 需求的选择算符: } ``` - + 或者 ```yaml @@ -359,15 +387,19 @@ this selector (respectively in `json` or `yaml` format) is equivalent to `compon #### 支持基于集合需求的资源 比较新的资源,例如 [`Job`](/zh-cn/docs/concepts/workloads/controllers/job/)、 [`Deployment`](/zh-cn/docs/concepts/workloads/controllers/deployment/)、 -[`Replica Set`](/zh-cn/docs/concepts/workloads/controllers/replicaset/) 和 +[`ReplicaSet`](/zh-cn/docs/concepts/workloads/controllers/replicaset/) 和 [`DaemonSet`](/zh-cn/docs/concepts/workloads/controllers/daemonset/), -也支持 _基于集合的_ 需求。 +也支持**基于集合的**需求。 ```yaml selector: @@ -379,7 +411,7 @@ selector: ``` `matchLabels` 是由 `{key,value}` 对组成的映射。 @@ -395,10 +427,9 @@ selector: #### Selecting sets of nodes One use case for selecting over labels is to constrain the set of nodes onto which a pod can schedule. -See the documentation on [node selection](/docs/concepts/configuration/assign-pod-node/) for more information. +See the documentation on [node selection](/docs/concepts/scheduling-eviction/assign-pod-node/) for more information. --> #### 选择节点集 通过标签进行选择的一个用例是确定节点集,方便 Pod 调度。 有关更多信息,请参阅[选择节点](/zh-cn/docs/concepts/scheduling-eviction/assign-pod-node/)文档。 -