K8S基本操作-09-Labels and Selectors
Labels and Selectors
Labels 是附加到 Kubernetes 對象(例如 Pod、Deployment、Service 等)上的鍵值對。它們可以用來對對象進行分類和標識,以便於管理和查找。例如,您可以為所有屬於同一應用程序的 Pod 添加一個名為 “app” 的標籤,並為所有在生產環境中運行的 Pod 添加一個名為 “environment” 的標籤。
Selectors 是一種用於根據標籤過濾 Kubernetes 對象的表達式。它們可以用於在列表、命令和 API 調用中指定要包含或排除的對象。
選取特定 Label 的指令,例如此處的Label 為 env
其中數值為 dev
:
kubectl get pods -l env=dev
或者
kubectl get pods --selector env=dev
如果要數某個 Label 有多少個可以用以下放是擴充指令。
kubectl get pods --selector env=dev --no-headers | wc -l
或者
kubectl get pods -l env=dev --no-headers | wc -l
如果要取得各種 Service, Pod, ReplicaSets 的話可以用以下指令
kubectl get all
取得所有物件時,也可以加註 Label,如下:
kubectl get all --selector env=prod
或者
kubectl get all -l env=prod
選擇多個標籤可以採用下列方法
kubectl get all --selector env=prod,bu=finance,tier=frontend
手動在 Node 添加 label 的方法
kubectl label node node01 color=blue