Kubernetes 简明教程
Kubernetes - Labels & Selectors
Labels
标签是附加到 Pod、副本控制器和服务上的键值对。它们用作 Pod 和副本控制器等对象的标识属性。它们可以在创建时添加到一个对象中,也可以在运行时添加到一个对象中或对其进行修改。
Labels are key-value pairs which are attached to pods, replication controller and services. They are used as identifying attributes for objects such as pods and replication controller. They can be added to an object at creation time and can be added or modified at the run time.
Selectors
标签并不提供唯一性。一般来说,我们可以说许多对象可以带有相同的标签。标签选择器是 Kubernetes 中的核心分组基元。它们是由用户用来选择一组对象的。
Labels do not provide uniqueness. In general, we can say many objects can carry the same labels. Labels selector are core grouping primitive in Kubernetes. They are used by the users to select a set of objects.
Kubernetes API 当前支持两种类型的选择器 -
Kubernetes API currently supports two type of selectors −
-
Equality-based selectors
-
Set-based selectors
Equality-based Selectors
它们允许按键和值进行过滤。匹配的对象应满足所有指定的标签。
They allow filtering by key and value. Matching objects should satisfy all the specified labels.
Set-based Selectors
基于集合的选择器允许根据一组值过滤键。
Set-based selectors allow filtering of keys according to a set of values.
apiVersion: v1
kind: Service
metadata:
name: sp-neo4j-standalone
spec:
ports:
- port: 7474
name: neo4j
type: NodePort
selector:
app: salesplatform ---------> 1
component: neo4j -----------> 2
在上述代码中,我们将标签选择器用作 app: salesplatform ,将组件用作 component: neo4j 。
In the above code, we are using the label selector as app: salesplatform and component as component: neo4j.
一旦使用 kubectl 命令运行该文件,它将创建一个名为 sp-neo4j-standalone 的服务,将在端口 7474 上进行通信。类型是 NodePort ,带有新的标签选择器 app: salesplatform 和 component: neo4j 。
Once we run the file using the kubectl command, it will create a service with the name sp-neo4j-standalone which will communicate on port 7474. The ype is NodePort with the new label selector as app: salesplatform and component: neo4j.