# kubectl 的標簽選擇器
(金慶的專欄 2021.1)
摘自:Kubenetes in Action
首先可以給 pod 添加 label:
```
kubectl label pod mypod abc=123
```
顯示 label:
```
kubectl get pod --show-labels
```
顯示部分 label:
```
kubectl get pod -L app,abc
```
更改
```
kubeclt label --overwrite pod mypod abc=456
```
刪除不需要 --overwrite
```
kubectl label pod mypod abc-
```
有了標簽后,可以
* 選擇沒有特定標簽
* 選擇有特定標簽
* 選擇有特定標簽并且值相等或值不等
列出 abc=123 的 pod
```
kubectl get pod -l abc=123
```
列出沒有 abc 標簽的 pod
```
kubectl get pod -l '!abc'
```
注意 Linux shell 嘆號必須用引號括起來:
還可以這樣:
```
kubectl get pod -l 'abc!=123'
kubectl get pod -l 'abc in (123,456,ddd)'
kubectl get pod -l 'abc notin (123,456)'
```
如果是多個 -l, 則僅最后一個 -l 有效.
```
kubectl get pod -l abc -l efg
```
同時滿足:
```
kubectl get pod -l abc=123,efg=456
```
好像沒有辦法表示或者關系, 見:
https://v1-16.docs.kubernetes.io/docs/concepts/overview/working-with-objects/labels/
```
Caution: For both equality-based and set-based conditions there is no logical OR (||) operator. Ensure your filter statements are structured accordingly.
```
(金慶的專欄 2021.1)
摘自:Kubenetes in Action
首先可以給 pod 添加 label:
```
kubectl label pod mypod abc=123
```
顯示 label:
```
kubectl get pod --show-labels
```
顯示部分 label:
```
kubectl get pod -L app,abc
```
更改
```
kubeclt label --overwrite pod mypod abc=456
```
刪除不需要 --overwrite
```
kubectl label pod mypod abc-
```
有了標簽后,可以
* 選擇沒有特定標簽
* 選擇有特定標簽
* 選擇有特定標簽并且值相等或值不等
列出 abc=123 的 pod
```
kubectl get pod -l abc=123
```
列出沒有 abc 標簽的 pod
```
kubectl get pod -l '!abc'
```
注意 Linux shell 嘆號必須用引號括起來:
還可以這樣:
```
kubectl get pod -l 'abc!=123'
kubectl get pod -l 'abc in (123,456,ddd)'
kubectl get pod -l 'abc notin (123,456)'
```
如果是多個 -l, 則僅最后一個 -l 有效.
```
kubectl get pod -l abc -l efg
```
同時滿足:
```
kubectl get pod -l abc=123,efg=456
```
好像沒有辦法表示或者關系, 見:
https://v1-16.docs.kubernetes.io/docs/concepts/overview/working-with-objects/labels/
```
Caution: For both equality-based and set-based conditions there is no logical OR (||) operator. Ensure your filter statements are structured accordingly.
```