????????label 特性:
????????Label selector是Kubernetes核心的分组机制,通过label selector客户端/用户能够识别一组有共同特征或属性的资源对象。
查询条件:
selector:
matchLabels:
app: myweb
matchExpressions:- {key: tier, operator: In, values: [frontend]}- {key: environment, operator: NorIn, values: [dev]}
????????matchLabels用于定义一组Label,与直接写在Selector中作用相同:matchExpression用于定义一组基于集合的筛选条件,可用的条件运算符包括:In、NotIn、Exists和DoesNotExist。
????????如果同时设置了matchLabels和matchExpression,则两组条件为“AND”关系,即所有条件需要满足才能完成Selector的筛选。
#可以多创建几个deploy 方便演示
[root@k8s-master-1 test]# vim deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: label-test
spec:
selector:
matchLabels:
app: nginx
env: prod
replicas: 1
template:
metadata:
labels:
app: nginx
env: prod
spec:
containers:
- image: nginx:1.17.1
name: nginx
[root@k8s-master-1 test]# kubectl apply -f deployment.yaml
deployment.apps/label-test created
#创建完成就可以看到他的标签
[root@k8s-master-1 test]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
label-test-7787b4694b-jvrz5 1/1 Running 0 16s app=nginx,env=prod,pod-template-hash=7787b4694b
[root@k8s-master-1 test]# kubectl get pod --show-labels -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
label-test-7787b4694b-jvrz5 1/1 Running 0 46s 10.244.0.164 k8s-node-1 <none> <none> app=nginx,env=prod,pod-template-hash=7787b4694b
#查看所有标签 -A
[root@k8s-master-1 ~]# kubectl get pod -A --show-labels
NAMESPACE NAME READY STATUS RESTARTS AGE LABELS
default busybox 1/1 Running 11 2d17h <none>
default label-test-7787b4694b-jvrz5 1/1 Running 2 41h app=nginx,env=prod,pod-template-hash=7787b4694b
default web-96d5df5c8-vmxgr 1/1 Running 2 2d23h app=web,pod-template-hash=96d5df5c8
kube-system coredns-6b774d79bb-4ssxb 1/1 Running 48 18d k8s-app=kube-dns,pod-template-hash=6b774d79bb
kube-system kube-flannel-ds-amd64-7bpfs 1/1 Running 16 18d app=flannel,controller-revision-hash=76ccd4ff4f,pod-template-generation=1,tier=node
kube-system kube-flannel-ds-amd64-8d8fj 1/1 Running 17 18d app=flannel,controller-revision-hash=76ccd4ff4f,pod-template-generation=1,tier=node
kube-system kubernetes-dashboard-567f98444d-d9zb4 1/1 Running 2 2d23h k8s-app=kubernetes-dashboard,pod-template-hash=567f98444d
kube-system metrics-server-6f6c655778-66q5g 1/1 Running 2 2d23h k8s-app=metrics-server,pod-template-hash=6f6c655778
kube-system metrics-server-6f6c655778-m6r7g 1/1 Running 3 3d17h k8s-app=metrics-server,pod-template-hash=6f6c655778
#命令行方式给容器、node节点、deploy、等等添加标签
[root@k8s-master-1 ~]# kubectl label node k8s-node01 k8s-node02 env=test
[root@k8s-master-1 ~]# kubectl label pod busybox app=v1
pod/busybox labeled
[root@k8s-master-1 ~]#
[root@k8s-master-1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
busybox 1/1 Running 11 2d18h app=v1
[root@k8s-master-1 ~]#
##修改标签使用 --overwrite
[root@k8s-master-1 ~]# kubectl label pod busybox app=v2 --overwrite
pod/busybox labeled
[root@k8s-master-1 ~]#
[root@k8s-master-1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
busybox 1/1 Running 11 2d18h app=v2
#删除标签
[root@k8s-master-1 ~]# kubectl label pod busybox app-
pod/busybox labeled
?????????命令行方式过滤
1、过滤出所有包含env=prod的pod
[root@k8s-master-1 test]# kubectl get pod -l env=prod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
label-test-7787b4694b-jvrz5 1/1 Running 0 34m app=nginx,env=prod,pod-template-hash=7787b4694b
2、过滤出包含env=prod,app=nginx的pod
[root@k8s-master-1 test]# kubectl get pod -l 'env=prod,app=nginx' --show-labels
NAME READY STATUS RESTARTS AGE LABELS
label-test-7787b4694b-jvrz5 1/1 Running 0 38m app=nginx,env=prod,pod-template-hash=7787b4694b
'env=prod,app in (nginx,nginx02,nginx03)',逗号分隔2个条件,表示要同时的满足
3、过滤出来不包含app=nginx1和app=nginx3的pod。语法:'app notin (nginx,nginx03)' ,用notin来实现
[root@k8s-master-1 test]# kubectl get pod -l 'app notin (nginx1,nginx2)' --show-labels
4、过滤出来不包含app=nginx的pod。 语法:'env != dev'
[root@k8s-master-1 test]# kubectl get pod -l 'app !=nginx' --show-labels
NAME READY STATUS RESTARTS AGE LABELS
web-96d5df5c8-vmxgr 1/1 Running 1 31h app=web,pod-template-hash=96d5df5c8
创建一个service
[root@k8s-master-1 test]# vim deployment-service.yaml
apiVersion: v1
kind: Service
metadata:
name: service-label
spec:
ports:
- name: 80-80
port: 80
targetPort: 80
selector: #标签选择器,选择标签为env: prod的pod
env: prod
[root@k8s-master-1 test]# kubectl apply -f deployment-service.yaml
service/service-label created
[root@k8s-master-1 test]# kubectl get svc service-label -owide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service-label ClusterIP 10.0.0.254 <none> 80/TCP 6m8s env=prod
# 如下图,可以看到,service恰好就是关联到了这2个pod上了。
[root@k8s-master-1 ~]# kubectl get endpoints service-label
NAME ENDPOINTS AGE
service-label 10.244.0.166:80,10.244.0.167:80 5h18m
[root@k8s-master-1 ~]#
[root@k8s-master-1 ~]#
[root@k8s-master-1 ~]# kubectl get pod -l env=prod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox 1/1 Running 18 3d1h 10.244.0.166 k8s-node-1 <none> <none>
label-test-7787b4694b-jvrz5 1/1 Running 2 2d1h 10.244.0.167 k8s-node-1 <none> <none>
注意:job、deployment、replicaset、daemonset不仅仅支持基于等式的匹配,也支持基于集合的匹配
如第三个步骤,创建的deployment,matchlabels用于匹配一组的标签,与直接在selector中的作用相同
在yaml文件中基于集合的方式匹配
语法:
spec: #
selector:#
matchLabels: #matchlabels用于匹配一组的标签,与直接在selector中的作用相同
app: nginx06#
env: online#
[root@k8s-master-1 test]#
[root@k8s-master-1 test]# kubectl get deployment label-test -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
label-test 1/1 1 1 2d1h nginx nginx:1.17.1 app=nginx,env=prod
[root@k8s-master-1 test]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox 1/1 Running 18 3d1h 10.244.0.166 k8s-node-1 <none> <none>
label-test-7787b4694b-jvrz5 1/1 Running 2 2d1h 10.244.0.167 k8s-node-1 <none> <none>
[root@k8s-master-1 test]#
[root@k8s-master-1 test]# kubectl get pod label-test-7787b4694b-jvrz5 -owide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
label-test-7787b4694b-jvrz5 1/1 Running 2 2d1h 10.244.0.167 k8s-node-1 <none> <none> app=nginx,env=prod,pod-template-hash=7787b4694b
#这样,deployment就和要控制、管理的pod进行了关联。
另外,在集合的方式里,matchExpression也可以作为匹配的操作
spec:
selector:
matchLabels:
app: nginx06
env: online
matchExpressions:
- {key: release, operator: In, values: [stable] }
- {key: app,operator: NotIn, values: [nginx02,nginx03] }
语法:
matchExpressions:
- {key: release, operator: In, values: [stable] }
- {key: app,operator: NotIn, values: [nginx02,nginx03] }