02 - Labels and Selectors

Q: How many PODs exist in the dev environment (env)?

※ Use selectors to filter the output

$ kubectl get pods --selector env=dev                                      

$ kubectl get pods --selector bu=finance --no-headers | wc -l   

$ kubectl get pods --show-labels=true                                     


Q: How many objects are in the prod environment including PODs, ReplicaSets and any other objects?

$ kubectl get all --selector env=prod                                        

$ kubectl get all --selector env=prod --no-headers | wc -l         


Q: Identify the POD which is part of the prod environment, the finance BU and of frontend tier?

$ kubectl get all --selector env=prod,bu=finance,tier=frontend      


Q: Error Message

執行以下命令後,出現如下錯誤訊息

$ kubectl apply -f replicaset-definition-1.yaml                             

The ReplicaSet "replicaset-1" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"tier":"nginx"}: `selector` does not match template `labels`


解析

這個標籤(template.metadata.labels)必須與 selector.matchLabels 一致,否則 ReplicaSet 無法正確匹配 Pod。

Update the replicaset-definition-1.yaml file as follows:

---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
   name: replicaset-1
spec:
   replicas: 2
   selector:
      matchLabels:
        tier: front-end
   template:
     metadata:
       labels:
        tier: front-end
     spec:
       containers:
       - name: nginx
         image: nginx 

定義 Pod 的部分

```
  # Pod 的模板
  template:
     metadata:
       labels:
        tier: front-end          # 這個 Label 讓 ReplicaSet 可以選擇這個 Pod
     spec:                          # Pod 的規範
       containers:                # Pod 內的容器
       - name: nginx
         image: nginx
```


matchLabels(精確匹配)

這個 ReplicaSet 只會管理 tier: front-end 的 Pod。

```

apiVersion: apps/v1

kind: ReplicaSet

metadata:

  name: frontend-replicaset

spec:

  replicas: 3

  selector:

    matchLabels:

      tier: front-end

  template:

    metadata:

      labels:

        tier: front-end

    spec:

      containers:

        - name: nginx

          image: nginx

```

※ 任何 擁有 tier: front-end 標籤 的 Pod,ReplicaSet 都會確保它的數量保持在 3 個。

※ 但如果 Pod 有 額外標籤(例如 tier: front-end, env: prod),仍然會被選擇。



matchExpressions(條件匹配)


允許更靈活的選擇條件,例如:
  • In(屬於某個集合)
  • NotIn(不屬於某個集合)
  • Exists(存在該標籤)
  • DoesNotExist(不存在該標籤)
```
selector:
  matchExpressions:
    - key: tier
      operator: In                     ✅ Pod 的 tier 必須是 front-end 或 backend
      values:
        - front-end
        - backend
    - key: env
      operator: NotIn                ✅ Pod 的 env 不能是 test
      values:
        - test
    - key: region
      operator: Exists               ✅ Pod 必須 有 region 這個標籤(值不限)
```

Service 使用 Selectors

```
apiVersion: v1
kind: Service
metadata:
  name: frontend-service
spec:
  selector:
    app: nginx
    env: production
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
```
這個 Service 只會將流量導向 app: nginx, env: production 的 Pod。
✅ 如果 Pod 沒有 env: production,則不會被這個 Service 選中!


留言