为了方便归类和管理,在kubernetes中标签管理也至为重要
labels标签管理
一个资源可以拥有多个标签,同一个标签可以被添加到多个对象,标签可以在创建资源时指定,并且可以在之后进行添加和删除,以及修改
labels:
www: linuxea-com
如上所示,类型key=value,其中键和值的长度最多使用63个字符
- key
其中key只能使用字母,数字,下划线(_),减号(-),点(.),并且只能使用字母开头在使用键时,可以使用键前缀,前缀总长度不能超过253个字符。
- value
其中value长度也不能超过253个字符,但是可以为空。也就是说可以有key,value可以为空。并且只能以字母或数字开头以及结尾,中间可以使用字母,数字,下划线(_),减号(-),点(.)这当中,标签至少满足一点,见名知意
为了充分利用这些标签,每个标签见名知意,以应用程序名称,标识应用程序实例的唯一名称,版本,组件等。如下示例:
标签查看
此前使用kubectl get pods --show-labels
可观察到所有pod的标签
[root@linuxea linuxea]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client-linuxea 1/1 Running 0 2d run=client-linuxea
nginx-linuxea-5786698598-dx2jr 1/1 Running 0 33s pod-template-hash=1342254154,run=nginx-linuxea
nginx-linuxea-5786698598-stttv 1/1 Running 0 33s pod-template-hash=1342254154,run=nginx-linuxea
nginx-linuxea-5786698598-t6lpx 1/1 Running 0 33s pod-template-hash=1342254154,run=nginx-linuxea
pod-demo-linuxea 3/3 Running 0 21m tier=backend,www=linuxea-com
列出www的标签的标签值的pods: -L
选项
[root@linuxea linuxea]# kubectl get pods -L=www
NAME READY STATUS RESTARTS AGE WWW
client-linuxea 1/1 Running 0 2d
nginx-linuxea-5786698598-dx2jr 1/1 Running 0 20s
nginx-linuxea-5786698598-stttv 1/1 Running 0 20s
nginx-linuxea-5786698598-t6lpx 1/1 Running 0 20s
pod-demo-linuxea 3/3 Running 0 21m linuxea-com
并且可以显示多个标签值,如:显示包含www标签和run状态的pods
[root@linuxea linuxea]# kubectl get pods -L=www,run
NAME READY STATUS RESTARTS AGE WWW RUN
client-linuxea 1/1 Running 0 2d client-linuxea
nginx-linuxea-5786698598-dx2jr 1/1 Running 0 3s nginx-linuxea
nginx-linuxea-5786698598-stttv 1/1 Running 0 3s nginx-linuxea
nginx-linuxea-5786698598-t6lpx 1/1 Running 0 3s nginx-linuxea
pod-demo-linuxea 3/3 Running 0 20m linuxea-com
那么如果只想显示www标签的pods,使用小l: -l
,并且可以接上--show-labels
[root@linuxea linuxea]# kubectl get pods -l=www
NAME READY STATUS RESTARTS AGE
pod-demo-linuxea 3/3 Running 0 16m
[root@linuxea linuxea]# kubectl get pods -l=www --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo-linuxea 3/3 Running 0 15m tier=backend,www=linuxea-com
标签打包
事先创建了pod-demo-linuxea
,此刻添加一个标签version=20180808
[root@linuxea linuxea]# kubectl label pods pod-demo-linuxea version=20180808
pod/pod-demo-linuxea labeled
[root@linuxea linuxea]# kubectl get pods -l=www --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo-linuxea 3/3 Running 0 22m tier=backend,version=20180808,www=linuxea-com
此刻标签已经打上,可是现在要进行更改,如果要 更改就要使用--overwrite
覆盖将version=20180808改成version=20180809,方式覆盖
[root@linuxea linuxea]# kubectl label pods pod-demo-linuxea version=20180809 --overwrite
pod/pod-demo-linuxea labeled
[root@linuxea linuxea]# kubectl get pods -l=www --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo-linuxea 3/3 Running 0 13h tier=backend,version=20180809,www=linuxea-com
标签的选择器
等值关系标签选择器:=(等于),==(等于),!=(不等于)集合关系标签选择器: Key in (VALUE1,VALUE2,...)
Key notin (VALUE1,VALUE2,...)
Key 存在次键
!Key 不存在次键
其中,在标签选择器中可以指定多个选择条件,在多个条件中隐含了逻辑域如果使用空的标签,则都符合选择
- 等值关系示例:
等于
[root@linuxea linuxea]# kubectl get pods -l version=20180809 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo-linuxea 3/3 Running 0 14h tier=backend,version=20180809,www=linuxea-com
不等于
[root@linuxea linuxea]# kubectl get pods -l version!=20180809 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client-linuxea 1/1 Running 0 2d run=client-linuxea
nginx-linuxea-5786698598-dx2jr 1/1 Running 0 14h pod-template-hash=1342254154,run=nginx-linuxea
nginx-linuxea-5786698598-stttv 1/1 Running 0 14h pod-template-hash=1342254154,run=nginx-linuxea
nginx-linuxea-5786698598-t6lpx 1/1 Running 0 14h pod-template-hash=1342254154,run=nginx-linuxea
[root@linuxea linuxea]#
- 集合关系示例:
查找version的值是20180809,以及20180810
[root@linuxea linuxea]# kubectl get pods -l "version in (20180809,20180910)" --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo-linuxea 3/3 Running 0 15h tier=backend,version=20180809,www=linuxea-com
[root@linuxea linuxea]#
查找version的值是20180809和20180810以外的
[root@linuxea linuxea]# kubectl get pods -l "version notin (20180809,20180810)" --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client-linuxea 1/1 Running 0 2d run=client-linuxea
nginx-linuxea-5786698598-dx2jr 1/1 Running 0 14h pod-template-hash=1342254154,run=nginx-linuxea
nginx-linuxea-5786698598-stttv 1/1 Running 0 14h pod-template-hash=1342254154,run=nginx-linuxea
nginx-linuxea-5786698598-t6lpx 1/1 Running 0 14h pod-template-hash=1342254154,run=nginx-linuxea
pod-demo-linuxea 3/3 Running 0 15h tier=backend,version=20180809,www=linuxea-com
标签关联
通常,在pod和service中会做一些标签关联,会使用另外两个字段来.许多资源支持内嵌字段,其使用的标签选择器如下:
- matchLabels: 直接给定键值
-
matchExpressions: 基于给定的表达式来定义使用标签选择器,{key:"KEY",operator:"OPERATOR",values:[VAL1,VAL2,.....]} 操作符
In,NotIn: values字段必须为非空列表 Exists,NotExists: values字段必须为空列表
node标签
nodeSelector:节点标签选择器,简单的打标:
wwwtype=www.linuxea.com
[root@linuxea linuxea]# kubectl label nodes linuxea.node-3.com wwwtype=www.linuxea.com node/linuxea.node-3.com labeled
查看
wwwtype=www.linuxea.com
,是对应到linuxea.node-3.com的nodes上[root@linuxea linuxea]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS linuxea.master-1.com Ready master 3d v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=linuxea.master-1.com,node-role.kubernetes.io/master= linuxea.node-1.com Ready <none> 3d v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=linuxea.node-1.com linuxea.node-2.com Ready <none> 3d v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=linuxea.node-2.com linuxea.node-3.com Ready <none> 3d v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=linuxea.node-3.com,men=8G,wwwtype=www.linuxea.com
而后在yaml文件中指定
nodeSelector: wwwtype: www.linuxea.com
与spec下的containers同级,示例如下:
apiVersion: v1 kind: Pod metadata: name: pod-demo-linuxea namespace: default labels: www: linuxea-com tier: backend spec: containers: - name: linuxea-pod1-com image: "marksugar/nginx:1.14.a" ports: - containerPort: 88 - name: linuxea-pod2-com image: "marksugar/nginx:1.14.b" ports: - containerPort: 88 - name: alpine image: "alpine:3.8" command: - "/bin/sh" - "-c" - "tail -f /etc/passwd" nodeSelector: wwwtype: www.linuxea.com
而后
kubectl create -f pod-demo.yaml
[root@linuxea linuxea]# kubectl create -f pod-demo.yaml pod/pod-demo-linuxea created
在次观察,
pod-demo.yaml
已经添加到linuxea.node-3.com
[root@linuxea linuxea]# kubectl get pods -o wide --show-labels NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE LABELS client-linuxea 1/1 Running 0 2d 172.16.2.252 linuxea.node-2.com <none> run=client-linuxea nginx-linuxea-5786698598-dx2jr 1/1 Running 0 15h 172.16.2.11 linuxea.node-2.com <none> pod-template-hash=1342254154,run=nginx-linuxea nginx-linuxea-5786698598-stttv 1/1 Running 0 15h 172.16.1.15 linuxea.node-1.com <none> pod-template-hash=1342254154,run=nginx-linuxea nginx-linuxea-5786698598-t6lpx 1/1 Running 0 15h 172.16.3.25 linuxea.node-3.com <none> pod-template-hash=1342254154,run=nginx-linuxea pod-demo-linuxea 3/3 Running 0 2s 172.16.3.26 linuxea.node-3.com <none> tier=backend,www=linuxea-com
而后
kubectl describe pods pod-demo-linuxea |tail -15
查看倒数15行的信息[root@linuxea linuxea]# kubectl describe pods pod-demo-linuxea |tail -15 Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 50s default-scheduler Successfully assigned default/pod-demo-linuxea to linuxea.node-3.com Normal Pulled 49s kubelet, linuxea.node-3.com Container image "marksugar/nginx:1.14.a" already present on machine Normal Created 49s kubelet, linuxea.node-3.com Created container Normal Started 49s kubelet, linuxea.node-3.com Started container Normal Pulled 49s kubelet, linuxea.node-3.com Container image "marksugar/nginx:1.14.b" already present on machine Normal Created 49s kubelet, linuxea.node-3.com Created container Normal Started 48s kubelet, linuxea.node-3.com Started container Normal Pulled 48s kubelet, linuxea.node-3.com Container image "alpine:3.8" already present on machine Normal Created 48s kubelet, linuxea.node-3.com Created container Normal Started 48s kubelet, linuxea.node-3.com Started container
资源注解
annotations : annotations与label的区别在于 : 它不能用于标签的选择器,仅用于为对象提供"元数据"。这些数据有些时候会被某些程序用到,键值不受长度限制。在声明时会被用上
[root@linuxea linuxea]# kubectl describe pods pod-demo-linuxea |grep Annotations Annotations: <none> [root@linuxea linuxea]#
可见现在没有配置。删除后添加
[root@linuxea linuxea]# kubectl delete -f pod-demo.yaml pod "pod-demo-linuxea" deleted
编辑资源清单添加
annotations: www.linuxea.com/ops-by: "linuxea admin"
完整如下
apiVersion: v1 kind: Pod metadata: name: pod-demo-linuxea namespace: default labels: www: linuxea-com tier: backend annotations: www.linuxea.com/ops-by: "linuxea admin" spec: containers: - name: linuxea-pod1-com image: "marksugar/nginx:1.14.a" ports: - containerPort: 88 - name: linuxea-pod2-com image: "marksugar/nginx:1.14.b" ports: - containerPort: 88 - name: alpine image: "alpine:3.8" command: - "/bin/sh" - "-c" - "tail -f /etc/passwd" nodeSelector: wwwtype: www.linuxea.com
开始create
[root@linuxea linuxea]# kubectl create -f pod-demo.yaml pod/pod-demo-linuxea created
[root@linuxea linuxea]# kubectl describe pods pod-demo-linuxea |grep Annotations Annotations: www.linuxea.com/ops-by=linuxea admin