0%

K8S|5.标签(label)操作

标签

标签(Labels)是附加到Kubernetes对象(比如 Pod)上的键值对。标签旨在用于指定对用户有意义且相关的对象标识属性。标签可以在创建时附加到对象,随后可以随时添加和修改。每个对象都可以定义一组键(Key)/ 值(Value)标签,但是每个键(Key)对于给定对象必须是唯一的。

标签作用:就是用来给k8s中对象起别名,有了别名可以过滤和筛选

语法

标签由键值对组成,其有效标签值:

  • 最多为63个字符(可以为空)
  • 除非标签值为空,必须以字母数字字符([a-z0-9A-Z])开头和结尾
  • 包含破折号(-)、下划线(_)、点(、)和字母或数字

查看所有node节点标签

1
2
3
4
5
6
7
[root@k8s-master01 ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master01 Ready control-plane 6d17h v1.26.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master01,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-master02 Ready control-plane 6d17h v1.26.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master02,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-master03 Ready control-plane 6d17h v1.26.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master03,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node01 Ready <none> 6d17h v1.26.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01,kubernetes.io/os=linux
k8s-node02 Ready <none> 6d17h v1.26.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02,kubernetes.io/os=linux

标签操作命令格式kubectl label nodes <nodename> label_name=label_value

添加标签

给node节点添加一个磁盘类型为ssd的标签

1
2
3
[root@k8s-master01 ~]# kubectl label nodes k8s-node01 k8s-node02 disktype=ssd
node/k8s-node01 labeled
node/k8s-node02 labeled

查看标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
--查看所有标签
[root@k8s-master01 ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master01 Ready control-plane 6d17h v1.26.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master01,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-master02 Ready control-plane 6d17h v1.26.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master02,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-master03 Ready control-plane 6d17h v1.26.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master03,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node01 Ready <none> 6d17h v1.26.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01,kubernetes.io/os=linux
k8s-node02 Ready <none> 6d17h v1.26.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02,kubernetes.io/os=linux

--查看指定标签节点
[root@k8s-master01 ~]# kubectl get nodes -l disktype=ssd
NAME STATUS ROLES AGE VERSION
k8s-node01 Ready <none> 6d17h v1.26.3
k8s-node02 Ready <none> 6d17h v1.26.3

删除标签

删除一个label,只需在命令行最后指定label的key名后跟一个减号相连((label_name-)即可

1
2
3
4
5
6
[root@k8s-master01 ~]# kubectl label nodes k8s-node02 disktype-
node/k8s-node02 unlabeled
可以看到k8s-node02 disktype=ssd标签已经被删除了
[root@k8s-master01 ~]# kubectl get nodes -l disktype
NAME STATUS ROLES AGE VERSION
k8s-node01 Ready <none> 6d17h v1.26.3

修改标签的值

修改一个label的值,需要加上--overwrite参数

1
2
3
4
5
6
7
[root@k8s-master01 ~]# kubectl label nodes k8s-node01 disktype=sata --overwrite
node/k8s-node01 labeled
[root@k8s-master01 ~]# kubectl get nodes -l disktype=ssd
No resources found
[root@k8s-master01 ~]# kubectl get nodes -l disktype=sata
NAME STATUS ROLES AGE VERSION
k8s-node01 Ready <none> 6d17h v1.26.3

筛选标签

1
2
3
4
5
kubectl get po -l disktype
kubectl get po -l disktype=ssd
kubectl get po -l '!disktype'
kubectl get po -l 'disktype in (ssd,ssta)'
kubectl get po -l 'disktype notin (ssd)'