# 比如说k8s-node-03是那个GPU节点, 给它打一个region=gpu-node的标签
[root@k8s-master-01 ~]# kubectl label node k8s-node-03 region=gpu-node
node/k8s-node-03 labeled
# 查看node标签, 在get node的时候使用--show-labels参数, 此参数也可用于查看其它资源的label
# 结果中可以看到新添加的region=gpu-node标签
[root@k8s-master-01 ~]# kubectl get node k8s-node-03 --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-node-03 Ready node 7d23h v1.23.6 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node-03,kubernetes.io/os=linux,nginx-ds=true,node-role.kubernetes.io/node=,node.kubernetes.io/node=,region=gpu-node
# 命令行 使用 -l 参数进行标签的筛选
[root@k8s-master-01 ~]# kubectl get nodes -l region=gpu-node
NAME STATUS ROLES AGE VERSION
k8s-node-03 Ready node 7d23h v1.23.6
# 也可以在deployment等资源中使用nodeSelector将pod部署到该节点, 配置如下
containers:
......
dnsPolicy: ClusterFirst
nodeSelector:
region: gpu-node
restartPolicy: Always
......
# 查看当前svc
[root@k8s-master-01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.244.0.1 <none> 443/TCP 7d23h
nginx-hpa ClusterIP 10.244.53.55 <none> 80/TCP 21h
# 对nginx-hpa进行label, version=v1.0 hpa=true
[root@k8s-master-01 ~]# kubectl label svc nginx-hpa version=v1.0 hpa=true
service/nginx-hpa labeled
# 查看所有version=v1.0的Service , -A 是匹配所有namespace的资源, 也可以用--all-namespace代替
[root@k8s-master-01 ~]# kubectl get svc -A -l version=v1.0
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default nginx-hpa ClusterIP 10.244.53.55 <none> 80/TCP 21h
为其他资源Label的方式也一样
Selector主要用于资源的匹配,只有符合条件的资源才会被调用或使用,可以使用该方式对集群中的各类资源进行分配
[root@k8s-master-01 ~]# kubectl get svc -n kube-system --show-labels
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE LABELS
calico-typha ClusterIP 10.244.211.165 <none> 5473/TCP 7d23h k8s-app=calico-typha
kube-dns ClusterIP 10.244.0.10 <none> 53/UDP,53/TCP,9153/TCP 7d23h k8s-app=kube-dns,kubernetes.io/cluster-service=true,kubernetes.io/name=CoreDNS
metrics-server ClusterIP 10.244.229.81 <none> 443/TCP 7d23h k8s-app=metrics-server
[root@k8s-master-01 ~]# kubectl get svc -n kube-system -l "k8s-app in (calico-typha,metrics-server)"
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
calico-typha ClusterIP 10.244.211.165 <none> 5473/TCP 7d23h
metrics-server ClusterIP 10.244.229.81 <none> 443/TCP 7d23h
[root@k8s-master-01 ~]# kubectl get svc -n kube-system -l k8s-app!=metrics-server
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
calico-typha ClusterIP 10.244.211.165 <none> 5473/TCP 7d23h
kube-dns ClusterIP 10.244.0.10 <none> 53/UDP,53/TCP,9153/TCP 7d23h
[root@k8s-master-01 ~]# kubectl get svc -A -l k8s-app
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-system calico-typha ClusterIP 10.244.211.165 <none> 5473/TCP 7d23h
kube-system kube-dns ClusterIP 10.244.0.10 <none> 53/UDP,53/TCP,9153/TCP 7d23h
kube-system metrics-server ClusterIP 10.244.229.81 <none> 443/TCP 7d23h
kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.244.111.97 <none> 8000/TCP 7d23h
kubernetes-dashboard kubernetes-dashboard NodePort 10.244.133.204 <none> 443:31286/TCP 7d23h
在实际使用中,Label的更改是经常发生的事情,可以使用–overwrite参数修改标签
# 查看当前svc 的标签
[root@k8s-master-01 ~]# kubectl get svc --show-labels
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE LABELS
kubernetes ClusterIP 10.244.0.1 <none> 443/TCP 8d component=apiserver,provider=kubernetes
nginx-hpa ClusterIP 10.244.53.55 <none> 80/TCP 22h app=nginx-hpa,hpa=true,version=v1.0
# 将 nginx-hpa的version修改为v1.1
[root@k8s-master-01 ~]# kubectl label svc nginx-hpa version=v1.1 --overwrite
service/nginx-hpa labeled
[root@k8s-master-01 ~]# kubectl get svc -l version=v1.1
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-hpa ClusterIP 10.244.53.55 <none> 80/TCP 24h
删除标签方式是 leabekey- 比如version-
[root@k8s-master-01 ~]# kubectl label svc nginx-hpa version-
service/nginx-hpa unlabeled
[root@k8s-master-01 ~]# kubectl get svc --show-labels
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE LABELS
kubernetes ClusterIP 10.244.0.1 <none> 443/TCP 8d component=apiserver,provider=kubernetes
nginx-hpa ClusterIP 10.244.53.55 <none> 80/TCP 24h app=nginx-hpa,hpa=true