标签管理,所有资源都可以有label,识别资源,便签可相同,也可不同,如果需要关联,则便签为一直或多个标签,
标签相互关联。pod-deamonset-svc 都通过label标记,一个很重要的概念,标签
标签和名字不一样,在同一namespace中,相同的资源名字是唯一标示符,而标签为一类资源,可以多个pod使用同一个标签。
如蓝绿发布时,就是采用两套不一样的集群,通过更改deployment关联的标签实现管理。由service标签发布
还可以实现node选择。
筛选资源。关联资源。区分资源。

[root@master01 ~]# kubectl get pods -l run=nginx
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          70m
[root@master01 ~]# kubectl get pods --show-labels 
NAME                 READY   STATUS    RESTARTS   AGE   LABELS
init                 2/2     Running   0          57m   run=initpod
multi                2/2     Running   0          65m   run=multipod
nginx                1/1     Running   0          70m   run=nginx
staticpod-master01   1/1     Running   0          50m   run=static
[root@master01 ~]# kubectl explain  svc.spec.selector
KIND:     Service
VERSION:  v1

FIELD:    selector <map[string]string>

DESCRIPTION:
     Route service traffic to pods with label keys and values matching this
     selector. If empty or not present, the service is assumed to have an
     external process managing its endpoints, which Kubernetes will not modify.
     Only applies to types ClusterIP, NodePort, and LoadBalancer. Ignored if
     type is ExternalName. More info:
     https://kubernetes.io/docs/concepts/services-networking/service/
[root@master01 ~]# kubectl get nodes --show-labels 
NAME       STATUS   ROLES                  AGE     VERSION   LABELS
master01   Ready    control-plane,master   5d19h   v1.23.8   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master01,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
node01     Ready    <none>                 5d19h   v1.23.8   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux
node02     Ready    <none>                 5d19h   v1.23.8   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node02,kubernetes.io/os=linux

节点打标签便于筛选

[root@master01 ~]# kubectl get nodes
NAME       STATUS   ROLES                  AGE     VERSION
master01   Ready    control-plane,master   5d22h   v1.23.8
node01     Ready    <none>                 5d22h   v1.23.8
node02     Ready    <none>                 5d22h   v1.23.8
[root@master01 ~]# kubectl label  nodes node01  node-role.kubernetes.io/woker=""
node/node01 labeled
[root@master01 ~]# kubectl label  nodes node02  node-role.kubernetes.io/woker=""
node/node02 labeled
[root@master01 ~]# kubectl get nodes
NAME       STATUS   ROLES                  AGE     VERSION
master01   Ready    control-plane,master   5d22h   v1.23.8
node01     Ready    woker                  5d22h   v1.23.8
node02     Ready    woker                  5d22h   v1.23.8
[root@master01 ~]#  kubectl get nodes -l node-role.kubernetes.io/woker=""
NAME     STATUS   ROLES   AGE     VERSION
node01   Ready    woker   5d22h   v1.23.8
node02   Ready    woker   5d22h   v1.23.8

实验一下,给node1添加storage=ssd的标签,给node2 添加storage=hdd 且利用便签选择器使把pod只调度在node01上

打标签)
[root@master01 ~]# kubectl label nodes node01 storage=ssd 
node/node01 labeled
[root@master01 ~]# kubectl label nodes node02 storage=hdd
node/node02 labeled
[root@master01 ~]# kubectl get nodes  -o wide
NAME       STATUS   ROLES                  AGE     VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION                CONTAINER-RUNTIME
master01   Ready    control-plane,master   5d22h   v1.23.8   192.168.5.170   <none>        CentOS Linux 7 (Core)   5.4.230-1.el7.elrepo.x86_64   docker://20.10.6
node01     Ready    woker                  5d22h   v1.23.8   192.168.5.171   <none>        CentOS Linux 7 (Core)   5.4.230-1.el7.elrepo.x86_64   docker://20.10.6
node02     Ready    woker                  5d22h   v1.23.8   192.168.5.172   <none>        CentOS Linux 7 (Core)   5.4.230-1.el7.elrepo.x86_64   docker://20.10.6
[root@master01 ~]# kubectl get node --show-labels 
NAME       STATUS   ROLES                  AGE     VERSION   LABELS
master01   Ready    control-plane,master   5d22h   v1.23.8   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master01,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
node01     Ready    woker                  5d22h   v1.23.8   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux,node-role.kubernetes.io/woker=,storage=ssd
node02     Ready    woker                  5d22h   v1.23.8   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node02,kubernetes.io/os=linux,node-role.kubernetes.io/woker=,storage=hdd
创建pod)
[root@master01 ~]# vim pod1.yaml 
[root@master01 ~]# cat pod1.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: label-test
  name: lable
  namespace: default
spec:
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: label
    resources: {}
  nodeSelector:
    storage: ssd 
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
[root@master01 ~]# kubectl get pods
NAME                 READY   STATUS    RESTARTS   AGE
init                 2/2     Running   0          3h45m
multi                2/2     Running   0          3h54m
nginx                1/1     Running   0          3h59m
staticpod-master01   1/1     Running   0          3h38m
[root@master01 ~]# kubectl create  -f pod1.yaml 
pod/lable created
[root@master01 ~]# kubectl get pods -o wide
NAME                 READY   STATUS    RESTARTS   AGE     IP               NODE       NOMINATED NODE   READINESS GATES
init                 2/2     Running   0          3h45m   10.244.140.92    node02     <none>           <none>
lable                1/1     Running   0          5s      10.244.196.155   node01     <none>           <none>
multi                2/2     Running   0          3h54m   10.244.196.154   node01     <none>           <none>
nginx                1/1     Running   0          3h59m   10.244.140.90    node02     <none>           <none>
staticpod-master01   1/1     Running   0          3h38m   10.244.241.65    master01   <none>           <none>

验证
[root@master01 ~]# kubectl describe pods lable 
Name:         lable
Namespace:    default
Priority:     0
Node:         node01/192.168.5.171
Start Time:   Wed, 15 Feb 2023 14:12:00 +0800
Labels:       run=label-test
Annotations:  cni.projectcalico.org/containerID: 768d5fbdd8b8e7ed47f988717c5ac47e8c67445c45fbe66271ef15783ff64082
              cni.projectcalico.org/podIP: 10.244.196.155/32
              cni.projectcalico.org/podIPs: 10.244.196.155/32
Status:       Running
IP:           10.244.196.155
IPs:
  IP:  10.244.196.155
Containers:
  label:
    Container ID:   docker://2657ba499d2a3c34f7588223567745e5b03b13fdd1abe0eb1c7d8613af16a5a7
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Wed, 15 Feb 2023 14:12:02 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-xzkz8 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-xzkz8:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
**Node-Selectors:              storage=ssd**
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  2m49s  default-scheduler  Successfully assigned default/lable to node01
  Normal  Pulled     2m47s  kubelet            Container image "nginx" already present on machine
  Normal  Created    2m47s  kubelet            Created container label
  Normal  Started    2m47s  kubelet            Started container label
文章作者: emporer
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Emporer-Linux
kubernetes kubernetes label
喜欢就支持一下吧