bafbf668e445be1118be5b1792bf2835.png

Label介绍

Label(标签)是Kubernetes中一个核心个概念,一个Label是一个key=value的键值对,其中key与value都是用户自定义的。Label可以添加到各种资源对象上,如Node、Pod、Service、RC等。Label通常在资源对象定义时确定,也可以在对象创建后动态添加或删除。通常通过给指定的资源对象捆绑一个或多个不同的Label来实现多维度的资源分组管理功能。

一些常用的Label示例如下:

版本标签:    release=stable, release=canary
环境标签:    environment=dev , environment=qa, environment=production
架构标签:    tier=frontend, tier=backend, tier=middleware
分区标签:    partition=customerA, partition=customerB
质量管控标签: track=daily,track=weekly

标签选择器与标签

在kubernetes中,ReplicationController与ReplicaSet分别使用不同的标签选择器(ReplicaSet可以完全替代ReplicationController,是更优的选择)。分别是如下写法:

  • ReplicationController
#nginx1-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx1
spec:
  replicas: 3
  selector:
    app: nginx1
    createtime: nov
  template:
    metadata:
      labels:
        createtime: nov
        app: nginx1
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
  • ReplicaSet
# nginx4-rc.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx3
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx4
      createtime: nov  template:
    metadata:
      labels:
        createtime: nov
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9

可以看到,ReplicaSet的标签选择器的写法变成:spec.selector.matchLabels,如果还是按照ReplicaSet的写法会报错:

# nginx3-rc.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx3
spec:
  replicas: 3
  selector:
    createtime: nov
  template:
    metadata:
      labels:
        createtime: nov
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9

$ kubectl apply -f nginx3-rs.yaml
error: error validating "nginx3-rs.yaml": error validating data: ValidationError(ReplicaSet.spec.selector): unknown field "createtime" in io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector; if you choose to ignore these errors, turn validation off with --validate=false

报错内容说 ReplicaSet.spec.selector中,createtime变量是未知的。

同时,也需要注意,标签选择器中的标签要少于template中的Label,否则会报错:

# nginx5-rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx5
spec:
  replicas: 3
  selector:
    matchLabels:
      createtime: nov
      app: nginx5
  template:
    metadata:
      labels:
        createtime: nov
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9

$ kubectl apply -f nginx5-rs.yaml
The ReplicaSet "nginx5" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"createtime":"nov"}: `selector` does not match template `labels`

除此之外,ReplicaSet还要matchExpressions等标签,用于匹配表达式,功能更加强大,这里不提。

实践

如果Label的作用仅仅在于上文提到匹配自己定义的Pod模板,那我觉得实在没什么意义。在ReplicaSet中,还可以通过Selector、Template和Replica共同作用管理集群中具有Selector关注Label的Pod。

首先创建若干个具有相同Label的Pod:

# pod1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod1
  labels:
    createtime: nov
    app: pod-1
spec:
  containers:
  - name: nginx
    image: nginx:1.7.9
...
kubectl apply -f pod1.yaml -f pod2.yaml -f pod3.yaml

kubectl get po
NAME                                     READY   STATUS    RESTARTS   AGE
nginx-pod1                               1/1     Running   0          96s
nginx-pod2                               1/1     Running   0          96s
nginx-pod3                               1/1     Running   0          96s

此时执行副本集为0的ReplicaSet:

# nginx6-rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx6
spec:
  replicas: 0
  selector:
    matchLabels:
      createtime: nov
  template:
    metadata:
      labels:
        createtime: nov
        app: nginx5
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
kubectl apply -f nginx6-rs.yaml

因为Label匹配的缘故,nginx的Pod被ReplicaSet所管理并进行删除,达到副本集为0的状态,日志如下:

kubectl get events --watch

0s          Normal    SuccessfulDelete         replicaset/nginx6              Deleted pod: nginx-pod2
0s          Normal    Killing                  pod/nginx-pod2                 Stopping container nginx
0s          Normal    Killing                  pod/nginx-pod3                 Stopping container nginx
0s          Normal    SuccessfulDelete         replicaset/nginx6              Deleted pod: nginx-pod1
0s          Normal    Killing                  pod/nginx-pod1                 Stopping container nginx
0s          Normal    SuccessfulDelete         replicaset/nginx6              Deleted pod: nginx-pod3

以上,就是关于Label的一些实践和总结

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐