DaemonSet 用在確保每個Node上都會執行指定的 DaemonSet Pod, 它透過node selector 來檢查每個node的狀態, 用戶可以針對node特性自定義標籤; 對於新建立的node會自動創建DaemonSet Pod, 而node終止後, DaemonSet Pod 也會被自動回收, 每個Node同時只會存在一個DaemonSet Pod。它適合獨立運作、主動對外溝通的情境, 通常應用在執行系統級任務, 此任務需要運行在特定的node上, 且任務需要執行在其他的Pod運行之前,例如: node 服務探索或node log蒐集...等, 如果不在這個前提之下建議使用Deployment 。
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
查看配置內容
Node-Selector 字段為空, 表示需要運行在每一個Node上Desired Number of Nodes Scheduled: 1 , 期望的Pod數量為 1Current 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
-> % kubectl set image daemonsets daemon-example daemon-ds=nginx:v1.0.0
daemonset.apps/daemon-example image updated
 
Deployment 和 Daemonset 同樣都可以確保Pod在運行的過程中不會被終止, Deployment適合用在無狀態的服務, Daemonset則是需要在指定的主機上運行服務且此服務必須比Node內其他Pod還要早運行的情境。