$ kubectl create -f kubia-manual-with-labels.yaml
pod/kubia-manual-v2 created
运行如下指令体会下标签功效
$ k get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
kubia-manual 1/1 Running 0 52m <none>
kubia-manual-v2 1/1 Running 0 66s creation_method=manual,env=prod
$ k get pod -L creation_method,env
NAME READY STATUS RESTARTS AGE CREATION_METHOD ENV
kubia-manual 1/1 Running 0 52m
kubia-manual-v2 1/1 Running 0 108s manual prod
$ k get pod -l creation_method
NAME READY STATUS RESTARTS AGE
kubia-manual-v2 1/1 Running 0 2m4s
$ k get pod -l creation_method=auto
No resources found.
$ kubectl label pod kubia-manual-v2 env= --overwrite 1 ↵
pod/kubia-manual-v2 labeled
$ k get pod -L creation_method,env
NAME READY STATUS RESTARTS AGE CREATION_METHOD ENV
kubia-manual 1/1 Running 0 59m
kubia-manual-v2 1/1 Running 0 8m51s manual
使用标签做选择
前面已经有指令练习过了,常用的是含有指定标签的资源,也可以列出不含标签的资源,如
$ k get pod -l '!creation_method'
NAME READY STATUS RESTARTS AGE
kubia-manual 1/1 Running 0 63m
标签过滤是重要功能,等大家在实际工作中要多搜索一下使用方式,过多细节这里不展开了
将pod部署到指定节点
这个操作非常重要,我们经常会用到。因为我们每个团队有各自的节点,可以避免互相争抢。
首先我们先看看节点上的标签
$ k get node --show-labels
NAME STATUS ROLES AGE VERSION LABELS
cn-hongkong.10.32.100.46 Ready <none> 6d4h v1.14.8-aliyun.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/instance-type=ecs.n2.medium,beta.kubernetes.io/os=linux,failure-domain.beta.kubernetes.io/region=cn-hongkong,failure-domain.beta.kubernetes.io/zone=cn-hongkong-c,k8s.aliyun.com=true,k8s.io/cluster-autoscaler=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=cn-hongkong.10.32.100.46,kubernetes.io/os=linux,node.spotmax/spec=2c4g,node.spotmax/team=dsp,node.spotmax/type=spot,policy=release,workload_type=spot
cn-hongkong.10.32.100.47 Ready <none> 3d3h v1.14.8-aliyun.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/instance-type=ecs.n2.medium,beta.kubernetes.io/os=linux,failure-domain.beta.kubernetes.io/region=cn-hongkong,failure-domain.beta.kubernetes.io/zone=cn-hongkong-c,k8s.aliyun.com=true,k8s.io/cluster-autoscaler=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=cn-hongkong.10.32.100.47,kubernetes.io/os=linux,node.spotmax/spec=2c4g,node.spotmax/team=adn,node.spotmax/type=spot,policy=release,workload_type=spot
cn-hongkong.10.32.100.48 Ready <none> 2d v1.14.8-aliyun.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/instance-type=ecs.n2.medium,beta.kubernetes.io/os=linux,failure-domain.beta.kubernetes.io/region=cn-hongkong,failure-domain.beta.kubernetes.io/zone=cn-hongkong-c,k8s.aliyun.com=true,k8s.io/cluster-autoscaler=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=cn-hongkong.10.32.100.48,kubernetes.io/os=linux,node.spotmax/spec=2c4g,node.spotmax/team=3s,node.spotmax/type=spot,policy=release,workload_type=spot
还是比较多的,我们结合内部需要,定制了我们关心标签
$ k get node -L node.spotmax/spec,node.spotmax/team,node.spotmax/type 130 ↵
NAME STATUS ROLES AGE VERSION SPEC TEAM TYPE
cn-hongkong.10.32.100.46 Ready <none> 6d8h v1.14.8-aliyun.1 2c4g dsp spot
cn-hongkong.10.32.100.47 Ready <none> 3d7h v1.14.8-aliyun.1 2c4g adn spot
cn-hongkong.10.32.100.48 Ready <none> 2d3h v1.14.8-aliyun.1 2c4g 3s spot