|NO.Z.00153|——————————|CloudNative|——|KuberNetes&服務釋出.V04|-------------------------------------------------------|label&selector.v04|selector查詢|
阿新 • • 發佈:2022-03-30
[CloudNative:KuberNetes&服務釋出.V04] [Applications.KuberNetes] [|DevOps|k8s|服務釋出|Label&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
二、通過不符合條件的labelpod過濾出來### --- 過濾符合條件的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
### --- 給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
===============================END===============================
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
來自為知筆記(Wiz)