概述

k8s中label(标签)的作用是很巨大的,对各种资源对象打标签帮助实现不同的功能。

例如:对node打标签,可以实现pod的调度,某个pod是否可以在某个node上运行。对pod打标签,可以实现pod和services资源的关联。也可以实现多个pod之间的亲和与反亲和。

举例,创建deployment和service资源,名称都是nginx,deployment、service、pod三个资源通过标签“app=nginx”关联。

在这里插入图片描述

[root@vm28 ~]# kubectl describe deploy nginx 
Name:                   nginx
Namespace:              default
CreationTimestamp:      Thu, 12 May 2022 15:13:35 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  ## deployment副本控制器管理标签为“app=nginx”的pod
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.15.4
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-94f885966 (3/3 replicas created)
Events:          <none>


[root@vm28 ~]# kubectl describe svc nginx 
Name:                     nginx
Namespace:                default
Labels:                   app=nginx
Annotations:              <none>
## service控制器将流量转给含有标签“app=nginx”的pod
Selector:                 app=nginx
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.68.124.188
IPs:                      10.68.124.188
Port:                     http  80/TCP
TargetPort:               80/TCP
NodePort:                 http  32046/TCP
Endpoints:                172.20.197.200:80,172.20.254.108:80,172.20.70.153:80
Session Affinity:         None
External Traffic Policy:  Cluster

获取标签

## 查看node标签
[root@vm28 ~]# kubectl get nodes --show-labels
[root@vm28 ~]# kubectl get nodes --show-labels=true
NAME           STATUS                     ROLES    AGE   VERSION   LABELS
192.168.0.28   Ready,SchedulingDisabled   master   34d   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.0.28,kubernetes.io/os=linux,kubernetes.io/role=master
192.168.0.29   Ready,SchedulingDisabled   master   34d   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.0.29,kubernetes.io/os=linux,kubernetes.io/role=master
192.168.0.30   Ready                      node     34d   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.0.30,kubernetes.io/os=linux,kubernetes.io/role=node
192.168.0.31   Ready                      node     34d   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.0.31,kubernetes.io/os=linux,kubernetes.io/role=node
192.168.0.33   Ready                      node     34d   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.0.33,kubernetes.io/os=linux,kubernetes.io/role=node


[root@vm28 ~]# kubectl describe node 192.168.0.28 
Name:               192.168.0.28
Roles:              master
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=192.168.0.28
                    kubernetes.io/os=linux
                    kubernetes.io/role=master

## 查看pod标签 
[root@vm28 ~]# kubectl get pods --show-labels
NAME                    READY   STATUS    RESTARTS       AGE   LABELS
nginx-94f885966-6xh2r   1/1     Running   15 (19m ago)   33d   app=nginx,pod-template-hash=94f885966
nginx-94f885966-d997t   1/1     Running   23 (13m ago)   33d   app=nginx,pod-template-hash=94f885966
nginx-94f885966-xwpkq   1/1     Running   7 (18m ago)    33d   app=nginx,pod-template-hash=94f885966

[root@vm28 ~]# kubectl describe pod nginx-94f885966-6xh2r 
Name:         nginx-94f885966-6xh2r
Namespace:    default
Priority:     0
Node:         192.168.0.31/192.168.0.31
Start Time:   Thu, 12 May 2022 15:13:36 +0800
Labels:       app=nginx
              pod-template-hash=94f885966

编辑node标签

https://blog.csdn.net/cd_yourheart/article/details/107670949

格式

# 添加node标签
kubectl label nodes <node_name> <key>=<value>

# 删除标签
kubectl label nodes <node_name> <key>-

示例

## 在节点上添加标签
[root@vm28 ~]# kubectl label nodes 192.168.0.31 disktype='ssd'
[root@vm28 ~]# kubectl describe node 192.168.0.31 
Name:               192.168.0.31
Roles:              node
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    disktype=ssd
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=192.168.0.31
                    kubernetes.io/os=linux
                    kubernetes.io/role=node

## 删除标签
[root@vm28 ~]# kubectl label nodes 192.168.0.31 disktype-

Node初始标签

k8s节点中有安装完成后就有的初始标签

beta.kubernetes.io/arch: 指令集平台
beta.kubernetes.io/os: 操作系统

kubernetes.io/arch: 指令集平台
kubernetes.io/hostname: 节点名称
kubernetes.io/os 操作系统
kubernetes.io/role 节点角色
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐