iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 13
0
Software Development

K8S - 30天從擦槍到提槍上陣學習筆記。系列 第 13

day 13 Pod Controller(3) - DaemonSet

DaemonSet 用在確保每個Node上都會執行指定的 DaemonSet Pod, 它透過node selector 來檢查每個node的狀態, 用戶可以針對node特性自定義標籤; 對於新建立的node會自動創建DaemonSet Pod, 而node終止後, DaemonSet Pod 也會被自動回收, 每個Node同時只會存在一個DaemonSet Pod。它適合獨立運作、主動對外溝通的情境, 通常應用在執行系統級任務, 此任務需要運行在特定的node上, 且任務需要執行在其他的Pod運行之前,例如: node 服務探索或node log蒐集...等, 如果不在這個前提之下建議使用Deployment

應用場景

  • 儲存cluster歷程, 例如: glusterd, ceph
  • 工作日誌收集, 例如: fluentd, logstash
  • 監控系統代理, 例如: Prometheus Node Exporter, colletcd, Data agent...等

創建 DaemonSet

DaemonSet spec 字段和Deployment相同, template 必填, 但是不支援replicas
提供兩種更新策略:RollingUpdate,OnDelete, 滾動更新為默認的更新策略。RollingUpdate僅支援maxUnavailable; OnDelete的方式是刪除Node上的Pod之後再重建為新版本。

  • 創建YAML範例

    apiVersion: apps/v1
    kind: DaemonSet
    metadata:
      name: daemon-example
      labels:
        app: daemon-ds
    spec:
      selector:
        matchLabels:
          app: daemon-ds
      updateStrategy:
        type: RollingUpdate
        rollingUpdate:
          maxUnavailable: 1
      template:
        metadata:
          labels:
            app: daemon-ds
          name: daemon-ds
        spec:
          containers:
          - name: daemon-ds
            image: nginx:latest
            ports:
            - name: http
              containerPort: 80
      minReadySeconds: 5
    
  • 執行

    -> % kubectl apply  -f daemon-demo.yaml
    daemonset.apps/daemon-example created
    
  • 查看 daemonsets 狀態

    -> % kubectl get daemonsets -L app
    NAME             DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE   APP
    daemon-example   1         1         1       1            1           <none>          85s   daemon-ds
    
  • 查看配置內容
    https://ithelp.ithome.com.tw/upload/images/20200917/20129656sCBejncxu7.png

    • Node-Selector 字段為空, 表示需要運行在每一個Node上
    • Desired Number of Nodes Scheduled: 1 , 期望的Pod數量為 1
    • Current Number of Nodes Scheduled: 1 , 完成的Pod數量為 1
  • 查看Pod

    -> % kubectl get pods -L app
    NAME                                READY   STATUS             RESTARTS   AGE     APP   
    daemon-example-hvsh5                1/1     Running            0          2m55s   daemon-ds
    

更新 Daemonset

  • 修改他的image
    -> % kubectl set image daemonsets daemon-example daemon-ds=nginx:v1.0.0
    daemonset.apps/daemon-example image updated
    
  • 查看配置內容
    https://ithelp.ithome.com.tw/upload/images/20200917/20129656uPFVNh28AG.png
    • 從Events內容可以看到默認的滾動更新方式是先刪除一個Node上的Pod再重建一個新的Pod

今日小結

Deployment 和 Daemonset 同樣都可以確保Pod在運行的過程中不會被終止, Deployment適合用在無狀態的服務, Daemonset則是需要在指定的主機上運行服務且此服務必須比Node內其他Pod還要早運行的情境。


上一篇
day 12 Pod Controller(2) - Deployment
下一篇
day 14 Pod Controller(4) - Job , CronJob & PodDisruptionBudget
系列文
K8S - 30天從擦槍到提槍上陣學習筆記。30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言