|NO.Z.00153|——————————|CloudNative|——|KuberNetes&服务发布.V04|——|label&selector.v04|selector查询|

一、selector的查询语法
### --- 通过一个条件匹配label过滤符合条件的pod
~~~     通过一个条件来匹配:假设我们有这么多Pod   
~~~     假设我们需要查看metrics-scraper和dashboard这个Pod,我们如何过滤

[root@k8s-master01 ~]# kubectl get po -A --show-labels
NAMESPACE              NAME                                         READY   STATUS             RESTARTS   AGE     LABELS
default                busybox                                      1/1     Running            0          19m     app=busybox
default                nginx-sfqkz                                  1/1     Running            0          4h24m   app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
default                nginx-ts5x4                                  1/1     Running            0          4h32m   app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
default                nginx-v245j                                  1/1     Running            0          4h31m   app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
kube-public            busybox                                      0/1     CrashLoopBackOff   7          13m     app=busybox2,run=busybox
kube-system            calico-kube-controllers-5f6d4b864b-6clrl     1/1     Running            0          9d      k8s-app=calico-kube-controllers,pod-template-hash=5f6d4b864b
kube-system            calico-node-6hbtl                            1/1     Running            0          9d      controller-revision-hash=dc4b7567d,k8s-app=calico-node,pod-template-generation=1
kube-system            calico-node-77c2f                            1/1     Running            3          9d      controller-revision-hash=dc4b7567d,k8s-app=calico-node,pod-template-generation=1
kube-system            calico-node-hrqpt                            1/1     Running            1          9d      controller-revision-hash=dc4b7567d,k8s-app=calico-node,pod-template-generation=1
kube-system            calico-node-trkhw                            1/1     Running            0          9d      controller-revision-hash=dc4b7567d,k8s-app=calico-node,pod-template-generation=1
kube-system            calico-node-z4gkj                            1/1     Running            0          9d      controller-revision-hash=dc4b7567d,k8s-app=calico-node,pod-template-generation=1
kube-system            coredns-867d46bfc6-sk5dp                     1/1     Running            0          9d      k8s-app=kube-dns,pod-template-hash=867d46bfc6
kube-system            metrics-server-595f65d8d5-slhtd              1/1     Running            1          9d      k8s-app=metrics-server,pod-template-hash=595f65d8d5
kubernetes-dashboard   dashboard-metrics-scraper-7645f69d8c-7dd2b   1/1     Running            0          9d      k8s-app=dashboard-metrics-scraper,pod-template-hash=7645f69d8c
kubernetes-dashboard   kubernetes-dashboard-78cb679857-mqccg        1/1     Running            13         9d      k8s-app=kubernetes-dashboard,pod-template-hash=78cb679857
### --- 过滤符合条件的pod

[root@k8s-master01 ~]# kubectl get po -A -l 'k8s-app in(metrics-server, kubernetes-dashboard)'      //-l:就是过滤同时符合两个条件的,就是k8s-app等于后面括号里的参数
NAMESPACE              NAME                                    READY   STATUS    RESTARTS   AGE
kube-system            metrics-server-595f65d8d5-slhtd         1/1     Running   1          9d
kubernetes-dashboard   kubernetes-dashboard-78cb679857-mqccg   1/1     Running   13         9d 
二、通过不符合条件的labelpod过滤出来
### --- 给nginx再加上一个label

[root@k8s-master01 ~]# kubectl get po 
NAME          READY   STATUS    RESTARTS   AGE
nginx-sfqkz   1/1     Running   0          4h29m
 [root@k8s-master01 ~]# kubectl label po nginx-sfqkz version=v1
pod/nginx-sfqkz labeled
### --- 查看所有容器的label

[root@k8s-master01 ~]# kubectl get po --show-labels
NAME          READY   STATUS    RESTARTS   AGE     LABELS
nginx-sfqkz   1/1     Running   0          4h30m   app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4,version=v1
nginx-ts5x4   1/1     Running   0          4h37m   app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
nginx-v245j   1/1     Running   0          4h37m   app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
### --- 查看version不等于1,而是等于nginx的label对应的pod
~~~     可以查看到2个
 
[root@k8s-master01 ~]# kubectl get po -l version!=v1,app=nginx          
NAME          READY   STATUS    RESTARTS   AGE
nginx-ts5x4   1/1     Running   0          4h39m
nginx-v245j   1/1     Running   0          4h38m
三、把APP等于nginx和busybox的label符合条件的pod过滤出来
### --- 把APP等于nginx和busybox的过滤出来且不想启用version等于v1的

[root@k8s-master01 ~]# kubectl get po -A -l 'app in (busybox, nginx)'       
NAMESPACE   NAME          READY   STATUS    RESTARTS   AGE
default     busybox       1/1     Running   0          27m
default     nginx-sfqkz   1/1     Running   0          4h32m
default     nginx-ts5x4   1/1     Running   0          4h40m
default     nginx-v245j   1/1     Running   0          4h39m
### --- 但是不想启用version等于v1的
~~~     label:是对一些资源进行分组的,
~~~     selector:是把一些符合标签的,符合我们规律的,符合查询条件的过滤出来。
 
[root@k8s-master01 ~]# kubectl get po -A -l version!=v1,'app in (busybox, nginx)'       //可以查看到少了一个
NAMESPACE   NAME          READY   STATUS    RESTARTS   AGE
default     busybox       1/1     Running   0          28m
default     nginx-ts5x4   1/1     Running   0          4h42m
default     nginx-v245j   1/1     Running   0          4h41m 

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(23)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示