标签是一种简单却功能强大的Kubernetes 特性,不仅可以组织pod ,也可以组织所有其他的Kubernetes 资源。详细来讲, 标签是可以附加到资源的任意键值对,用以选择具有该确切标签的资源(这是通过标签选择器完成的) 。只要标签的key 在资源内是唯一的, 一个资源便可以拥有多个标签。通常在我们创建资源时就会将标
签附加到资源上,但之后我们也可以再添加其他标签,或者修改现有标签的值,而无须重新创建资源。

04.学习kubernetes labels - 图1

创建pod时指定yaml

以Pod为例,Label定义在metadata中

  1. [root@master01 ~]# kubectl explain pod.metadata.labels
  2. KIND: Pod
  3. VERSION: v1
  4. FIELD: labels <map[string]string>
  5. DESCRIPTION:
  6. Map of string keys and values that can be used to organize and categorize
  7. (scope and select) objects. May match selectors of replication controllers
  8. and services. More info: http://kubernetes.io/docs/user-guide/labels

demo-pod

cat > demo-pod.yaml <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: demo-pod01
  namespace: default
  labels:
    app: myapp01
    release: beta
    disk: ssd
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
---
apiVersion: v1
kind: Pod
metadata:
  name: demo-pod02
  namespace: default
  labels:
    app: myapp02
    release: stable
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
EOF
kubectl apply -f demo-pod.yaml

查看labels

[root@master01 ~]# kubectl get pod
NAME         READY   STATUS    RESTARTS   AGE
demo-pod01   1/1     Running   0          80s
demo-pod02   1/1     Running   0          10s

[root@master01 ~]# kubectl get pod --show-labels 
NAME         READY   STATUS    RESTARTS   AGE   LABELS
demo-pod01   1/1     Running   0          86s   app=myapp01,disk=ssd,release=beta
demo-pod02   1/1     Running   0          16s   app=myapp02,release=stable

-l disk 列出含有disk这个key的pod

[root@master01 ~]# kubectl get pod --show-labels  -l disk
NAME         READY   STATUS    RESTARTS   AGE     LABELS
demo-pod01   1/1     Running   0          3m38s   app=myapp01,disk=ssd,release=beta

-L app,disk 列出指定key。

[root@master01 ~]# kubectl get pod --show-labels -L app,disk
NAME         READY   STATUS    RESTARTS   AGE     APP       DISK   LABELS
demo-pod01   1/1     Running   0          6m33s   myapp01   ssd    app=myapp01,disk=ssd,release=beta
demo-pod02   1/1     Running   0          5m23s   myapp02          app=myapp02,release=stable

利用in来查询标签

[root@master01 ~]# kubectl get pod  -l "release in (beta,stable)" --show-labels
NAME         READY   STATUS    RESTARTS   AGE   LABELS
demo-pod01   1/1     Running   0          15m   app=myapp01,disk=ssd,release=beta
demo-pod02   1/1     Running   0          14m   app=myapp02,disk=sata,release=stable

[root@master01 ~]# kubectl get pod  -l "release in (beta)" --show-labels
NAME         READY   STATUS    RESTARTS   AGE   LABELS
demo-pod01   1/1     Running   0          15m   app=myapp01,disk=ssd,release=beta

[root@master01 ~]# kubectl get pod  -l "release notin (beta)" --show-labels
NAME         READY   STATUS    RESTARTS   AGE   LABELS
demo-pod02   1/1     Running   0          14m   app=myapp02,disk=sata,release=stable

添加labels

[root@master01 ~]# kubectl label pod demo-pod02 disk=nvme
pod/demo-pod02 labeled

[root@master01 ~]# kubectl get pod --show-labels 
NAME         READY   STATUS    RESTARTS   AGE     LABELS
demo-pod01   1/1     Running   0          8m26s   app=myapp01,disk=ssd,release=beta
demo-pod02   1/1     Running   0          7m16s   app=myapp02,disk=nvme,release=stable

修改labels

在更改现有标签时,需要使用 —overwrite 选项

[root@master01 ~]# kubectl label pod demo-pod02 disk=sata --overwrite 
pod/demo-pod02 labeled
[root@master01 ~]# kubectl get pod --show-labels
NAME         READY   STATUS    RESTARTS   AGE     LABELS
demo-pod01   1/1     Running   0          9m36s   app=myapp01,disk=ssd,release=beta
demo-pod02   1/1     Running   0          8m26s   app=myapp02,disk=sata,release=stable

删除labels

[root@master01 ~]# kubectl label pod demo-pod01  app-
pod/demo-pod01 labeled

[root@master01 ~]# kubectl get pod  --show-labels
NAME         READY   STATUS    RESTARTS   AGE   LABELS
demo-pod01   1/1     Running   0          24m   disk=ssd,release=beta
demo-pod02   1/1     Running   0          23m   app=myapp02,disk=sata,release=stable

使用标签分类工作节点

先给node打标签

[root@master01 ~]# kubectl get nodes --show-labels 
NAME       STATUS   ROLES    AGE   VERSION   LABELS
master01   Ready    master   39h   v1.16.4   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/master=
master02   Ready    master   39h   v1.16.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master02,kubernetes.io/os=linux,node-role.kubernetes.io/master=
master03   Ready    master   39h   v1.16.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master03,kubernetes.io/os=linux,node-role.kubernetes.io/master=
node01     Ready    node     39h   v1.16.4   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/node=
node02     Ready    node     39h   v1.16.4   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/node=
node03     Ready    node     39h   v1.16.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node03,kubernetes.io/os=linux,node-role.kubernetes.io/node=

[root@master01 ~]# kubectl label node node01 app=myapp
node/node01 labeled

[root@master01 ~]# kubectl get node --show-labels 
NAME       STATUS   ROLES    AGE   VERSION   LABELS
master01   Ready    master   39h   v1.16.4   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/master=
master02   Ready    master   39h   v1.16.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master02,kubernetes.io/os=linux,node-role.kubernetes.io/master=
master03   Ready    master   39h   v1.16.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master03,kubernetes.io/os=linux,node-role.kubernetes.io/master=
node01     Ready    node     39h   v1.16.4   app=myapp,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/node=
node02     Ready    node     39h   v1.16.4   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/node=
node03     Ready    node     39h   v1.16.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node03,kubernetes.io/os=linux,node-role.kubernetes.io/node=

yaml里使用nodeSelector

[root@master01 ~]# kubectl explain pod.spec.nodeSelector
KIND:     Pod
VERSION:  v1

FIELD:    nodeSelector <map[string]string>

DESCRIPTION:
     NodeSelector is a selector which must be true for the pod to fit on a node.
     Selector which must match a node's labels for the pod to be scheduled on
     that node. More info:
     https://kubernetes.io/docs/concepts/configuration/assign-pod-node/
cat > demo-pod-node01.yaml <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: demo-pod-node01
  namespace: default
  labels:
    app: myapp
    release: beta
    disk: ssd
spec:
  nodeSelector: 
    app: myapp
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
EOF
kubectl apply -f demo-pod-node01.yaml
[root@master01 ~]# kubectl get pod -o wide
NAME              READY   STATUS    RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
demo-pod-node01   1/1     Running   0          64s   10.244.196.132   node01   <none>           <none>