iT邦幫忙

2021 iThome 鐵人賽

DAY 13
0

蝦米係Label

當大家都完成第一步往k8s部署,第二步就是要來好好管理這些container,k8s提供了一個簡單又好記的東西,方便大家管理container,那就是label

沒有透過label管理的pod長的像下圖

圖片來源
透過定義label的方式來管理pod就像下圖

圖片來源
使用label看起來是不是比較潮呢/images/emoticon/emoticon01.gif

label主要是對pod提供一個可識別的資料,而label是採用key-value的方式儲存,在一個pod上面定義的label的key值是要唯一的喔,ex

#正確
"metadata": {
  "labels": {
    "key1" : "value1",
    "key2" : "value2"
  }
}
#錯誤
"metadata": {
  "labels": {
    "key" : "value1",
    "key" : "value2"
  }
}

Label有什麼限制

  • 少於63個字
  • 字母或是數字([a-z0-9A-Z])開始或結束
  • 合法的符號 -_.

Label selector

因為label的key值不是唯一值(同個pod底下是唯一值),所以多個pod可能會有相同的label,所以就可以使用selector把相同label的pod撈出來,如果有寫過jquery的話,跟jquery selector 87分像的東西。
Label selector有二種類型

  • equality-based :可用 ===(都是同於)、!=(不等於)
  • set-based :可用innotinexists

動手寫label

部署二個redis,一個是for前端用,一個是for後端用

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-redis-backend
  labels:
    app.kubernetes.io/name: my-redis
    env: dev
    user: backend
spec:
  serviceName: my-redis-backend
  replicas: 1
  selector:
    matchLabels:
       app.kubernetes.io/name: my-redis
  template:
    metadata:
      labels:
        app.kubernetes.io/name: my-redis
        env: dev
        user: backend
    spec:
      containers:
        - name: my-redis-backend
          image: "redis:latest"
          imagePullPolicy: IfNotPresent
          args: ["--appendonly", "yes", "--save", "600", "1"]
          ports:
            - name: redis
              containerPort: 6379
              protocol: TCP
          volumeMounts:
            - name: data
              mountPath: /data
          resources: {}

  volumeClaimTemplates:
      - metadata:
          name: data
        spec:
          accessModes: ["ReadWriteOnce"]
          resources:
            requests:
              storage: 1Gi
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-redis-frontend
  labels:
    app.kubernetes.io/name: my-redis
    env: dev
    user: frontend
spec:
  serviceName: my-redis-frontend
  replicas: 1
  selector:
    matchLabels:
       app.kubernetes.io/name: my-redis
  template:
    metadata:
      labels:
        app.kubernetes.io/name: my-redis
        env: dev
        user: frontend
    spec:
      containers:
        - name: my-redis-frontend
          image: "redis:latest"
          imagePullPolicy: IfNotPresent
          args: ["--appendonly", "yes", "--save", "600", "1"]
          ports:
            - name: redis
              containerPort: 6379
              protocol: TCP
          volumeMounts:
            - name: data
              mountPath: /data
          resources: {}

  volumeClaimTemplates:
      - metadata:
          name: data
        spec:
          accessModes: ["ReadWriteOnce"]
          resources:
            requests:
              storage: 1Gi

部署完成後來看一下pod的資料是否跟預期的一樣

kubectl get pod --show-labels
NAME                                         READY   STATUS    RESTARTS   AGE     LABELS
dashboard-metrics-scraper-778b77d469-v85pw   1/1     Running   11         11d     k8s-app=dashboard-metrics-scraper,pod-template-hash=778b77d469
k0s-nginx-585449566-4nhvn                    1/1     Running   0          9h      app=nginx,pod-template-hash=585449566
kubernetes-dashboard-5cd89984f5-9vtsm        1/1     Running   11         11d     k8s-app=kubernetes-dashboard,pod-template-hash=5cd89984f5
my-redis-0                                   1/1     Running   0          3m23s   app.kubernetes.io/name=my-redis,controller-revision-hash=my-redis-55cc9bcb54,env=dev,statefulset.kubernetes.io/pod-name=my-redis-0,user=frontend
my-redis-backend-0                           1/1     Running   0          100s    app.kubernetes.io/name=my-redis,controller-revision-hash=my-redis-backend-8d4cc9789,env=dev,statefulset.kubernetes.io/pod-name=my-redis-backend-0,user=backend
my-redis-frontend-0                          1/1     Running   0          105s    app.kubernetes.io/name=my-redis,controller-revision-hash=my-redis-frontend-dfdc9dc7c,env=dev,statefulset.kubernetes.io/pod-name=my-redis-frontend-0,user=frontend

這時候就可以透過Label Selector進行查詢

kubectl get pod -l env=dev                                              
NAME                  READY   STATUS    RESTARTS   AGE
my-redis-0            1/1     Running   0          4m11s
my-redis-backend-0    1/1     Running   0          2m28s
my-redis-frontend-0   1/1     Running   0          2m33s

除了在yaml裡面宣告label外,也可以透過kubectl語法進行設定

#新增label
kubectl label pods my-redis-backend-0 iamrich=true  
pod/my-redis-backend-0 labeled

kubectl get pod -l iamrich=true
NAME                 READY   STATUS    RESTARTS   AGE
my-redis-backend-0   1/1     Running   0          5m23s
#刪除label
kubectl label pods my-redis-backend-0 iamrich-
pod/my-redis-backend-0 labeled

kubectl get pod -l iamrich=true
No resources found in kubernetes-dashboard namespace.

以上大致上就是label的動手做囉/images/emoticon/emoticon01.gif


上一篇
[DAY12]就是要持久-Statefulset
下一篇
[DAY14]Label進階使用-Affinity and anti-affinity
系列文
k8s新手船長船難記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言