Kubernetes 的 Label 和 Annotation 小記
Label
實作
1 2 3 4
| kubectl run alpaca-prod \ --image=gcr.io/kuar-demo/kuard-amd64:blue \ --replicas=2 \ --labels="ver=1,app=alpaca,env=prod"
|
1 2 3 4
| kubectl run alpaca-test \ --image=gcr.io/kuar-demo/kuard-amd64:green \ --replicas=1 \ --labels="ver=2,app=alpaca,env=test"
|
1 2 3 4
| kubectl run bandicoot-prod \ --image=gcr.io/kuar-demo/kuard-amd64:green \ --replicas=2 \ --labels="ver=2,app=bandicoot,env=prod"
|
1 2 3 4
| kubectl run bandicoot-staging \ --image=gcr.io/kuar-demo/kuard-amd64:green \ --replicas=1 \ --labels="ver=2,app=bandicoot,env=prod"
|
Flag --replicas has been deprecated, has no effect and will be removed in the future.
在K8S v1.18.0 以後,–replicas已棄用 ,推薦用 deployment 創建 pods
解决k8s 1.18.0版本 replicas 被弃用问题 - 菜鸟bai - 博客园
1 2 3 4 5 6
| kubectl get pods --show-labels
|
Remove all the generators from kubectl run. It will now only create pods.
minikube - How to start a pod in command line without deployment in kubernetes? - Stack Overflow
這邊我書上都寫 deployments 想說怎麼跟我電腦跑的結果不一樣
1 2 3 4 5 6 7
| kubectl label pods alpaca-test "canary=true" ✔ 22 22:27:42
kubectl get pods alpaca-test -L canary ✔ 23 22:28:03
kubectl label podsalpaca-test "canary-"
|
Label 選擇器
1 2 3 4 5 6
| kubectl get pods --show-labels
|
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
| kubectl get pods --selector="ver=2"
kubectl get pods --selector="app=bandicoot,ver=2"
kubectl get pods --selector="app in (alpaca,bandicoot)"
kubectl get pods --selector="canary"
kubectl get pods --selector='!canary'
kubectl get pods -l 'ver=2,!canary'
|
Yaml 寫 IN 方法
kubectl get pods -l ‘environment in (production, qa)’
1 2 3 4 5 6
| selector: matchLabels: component: redis matchExpressions: - {key: tier, operator: In, values: [cache]} - {key: environment, operator: NotIn, values: [dev]}
|
可參考:Labels and Selectors | Kubernetes
舊版寫法(因為相容還是可以跑)
1 2 3
| selector: app: alpaca ver: 1
|
Annotation
我看書沒特別介紹
感覺他就是Java HashMap 可以任意存任何key value 值
1 2 3 4
| ... metadata: anotations: xxxx.com/url: "http://xxxx"
|
移除範例 pods
1
| kubectl delete pods --all
|
delete 也可以搭配篩選器selector