kkwen

kubernetes之标签label
Kubernetes中的任意API对象都是通过Label进行标识,Label的实质是一系列的Key/Value键值...
扫描右侧二维码阅读全文
04
2019/03

kubernetes之标签label

Kubernetes中的任意API对象都是通过Label进行标识,Label的实质是一系列的Key/Value键值对,其中key于value由用户自己指定。Label可以附加在各种资源对象上,如Node、Pod、Service、RC等,一个资源对象可以定义任意数量的Label,同一个Label也可以被添加到任意数量的资源对象上去。Label是Replication Controller和Service运行的基础,二者通过Label来进行关联Node上运行的Pod。

标签选择器

  • 等值关系
=,==,!=
  • 集合关系
KEY in (VALUE1,VALUE2,...)
KEY notin (VALUE1,VALUE2,...)
KEY
!KEY ???

标签的使用

  • 根据标签条件过滤对象
kubectl get pods -l  <string> 
    kubectl get pods --show-labels
    kubectl get pods -l app # 显示标签中key为app的pod对象
    ubectl get pods -l app=myapp # 显示标签中key为app且value等于myapp的对象
  • 显示指定标签key的value字段 kubectl get pods -L <string>
[root@ _108_ ~/k8s]# kubectl get pods --show-labels
NAME                     READY   STATUS    RESTARTS   AGE     LABELS
myapp-7cf55d679f-jx6nm   1/1     Running   0          5m43s   pod-template-hash=7cf55d679f,run=myapp
myapp-7cf55d679f-q4cwd   1/1     Running   0          5m43s   pod-template-hash=7cf55d679f,run=myapp
pod-example              2/2     Running   0          42m     app=myapp,tier=frontend
[root@ _109_ ~/k8s]# kubectl get pods -L run
NAME                     READY   STATUS    RESTARTS   AGE     RUN
myapp-7cf55d679f-jx6nm   1/1     Running   0          5m54s   myapp
myapp-7cf55d679f-q4cwd   1/1     Running   0          5m54s   myapp
pod-example              2/2     Running   0          42m     
[root@ _110_ ~/k8s]# kubectl get pods -L app
NAME                     READY   STATUS    RESTARTS   AGE     APP
myapp-7cf55d679f-jx6nm   1/1     Running   0          5m57s   
myapp-7cf55d679f-q4cwd   1/1     Running   0          5m57s   
pod-example              2/2     Running   0          42m     myapp
[root@ _111_ ~/k8s]# kubectl get pods -L app=myapp
NAME                     READY   STATUS    RESTARTS   AGE    APP=MYAPP
myapp-7cf55d679f-jx6nm   1/1     Running   0          6m8s   
myapp-7cf55d679f-q4cwd   1/1     Running   0          6m8s   
pod-example              2/2     Running   0          42m   
  • 打标签 kubectl label,多个标签使用空格隔开
[root@ _113_ ~/k8s]# kubectl get pods --show-labels
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
myapp-7cf55d679f-jx6nm   1/1     Running   0          10m   pod-template-hash=7cf55d679f,run=myapp
myapp-7cf55d679f-q4cwd   1/1     Running   0          10m   pod-template-hash=7cf55d679f,run=myapp
pod-example              2/2     Running   0          47m   app=myapp,tier=frontend
[root@ _114_ ~/k8s]# kubectl label podspod-example release=stable
error: resource(s) were provided, but no name, label selector, or --all flag specified
[root@ _115_ ~/k8s]# kubectl label pods pod-example release=stable
pod/pod-example labeled
[root@ _116_ ~/k8s]# kubectl get pods -l app --show-labels
NAME          READY   STATUS    RESTARTS   AGE   LABELS
pod-example   2/2     Running   0          47m   app=myapp,release=stable,tier=frontend
  • 替换标签 --overwrite
[root@ _117_ ~/k8s]# kubectl label pods pod-example release=canary
error: 'release' already has a value (stable), and --overwrite is false
[root@ _118_ ~/k8s]# kubectl label pods pod-example release=canary --overwrite
pod/pod-example labeled
[root@ _119_ ~/k8s]# kubectl get pods -l app --show-labels
NAME          READY   STATUS    RESTARTS   AGE   LABELS
pod-example   2/2     Running   0          49m   app=myapp,release=canary,tier=frontend
  • 标签选择表达式
[root@ _124_ ~/k8s]# kubectl get pods -l "release notin (canary, beta, alphe)"
NAME                     READY   STATUS    RESTARTS   AGE
myapp-7cf55d679f-jx6nm   1/1     Running   0          14m
myapp-7cf55d679f-q4cwd   1/1     Running   0          14m
  • 给节点打标签
[root@ _130_ ~/k8s]# kubectl label nodes node2 disktype=ssd
node/node2 labeled
[root@ _131_ ~/k8s]# kubectl get nodes --show-labels
NAME    STATUS     ROLES    AGE   VERSION   LABELS
node1   Ready      master   49d   v1.13.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node1,node-role.kubernetes.io/master=
node2   Ready      <none>   49d   v1.13.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/hostname=node2
node3   NotReady   <none>   49d   v1.13.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node3

资源支持内嵌字段定义其使用的标签选择器:

matchLabels: 直接给定键值
matchExpressions: 基于给定的表达式来定义使用标签选择,{key:"KEY", operator:"OPERATOR",values:[VAL1,VAL2,...]}
    操作符:
        In、Notin: values字段的值必须为非空列表
        Exsits,NotExists:value字段的值必须为空列表
最后修改:2019 年 03 月 04 日 09 : 52 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论