Labels

environment = production
environment == production  # 等同于前者
tier != frontend

environment in (production, qa)  # 在集合里
tier notin (frontend, backend)  # 不再集合里
partition  # 含partition key
!partition  # 不含partition key

连接各条件使用 , 分割.

在 Api url 请求中, 可以使用 query 参数指定 Label 选择器, 如下所示 ( 需要注意转义)

?labelSelector=environment%3Dproduction,tier%3Dfrontend

?labelSelector=environment+in+%28production%2Cqa%29%2Ctier+in+%28frontend%29

在命令行中, 直接使用 -l 参数, 如下所示

kubectl get pods -l environment=production,tier=frontend

kubectl get pods -l 'environment in (production),tier in (frontend)'

同样在 yaml 中也可以指定

selector:
    component: redis  # component=redis 或者 component in (redis). 这里是直接指定
		...
		matchLabels:
	    component: redis  # 等同于上者, 这里使用了 matchLabels 来标记
		matchExpressions:
	    - {key: tier, operator: In, values: [cache]}  # 更复杂的标记, operator: In, NotIn, Exists, 和DoesNotExist
	    - {key: environment, operator: NotIn, values: [dev]}

Annotations

Annotations 和 Labels 比较像, 同样由 key/value 组成, 可以有多个, 不同的是, Annotations不用于标识和选择对象。Annotations中的元数据可以是small 或large,structured 或unstructured,并且可以包括标签不允许使用的字符.

以下是在Annotations中记录信息的一些例子:

<aside> 💡 Annotations不会被 k8s 直接使用,其主要目的是方便用户阅读查找。

</aside>