daemonset是k8s 1.2版本新增的一種資源對象,用於管理在集羣中每個Node上僅運行一份Pod的副本實例。
這種用法適合有以下需求的應用:
-
在每個node上都運行一個GlusterFS存儲或者Ceph存儲的Daemon進程。
-
在每個node上都運行一個日誌採集程序,例如Fluentd或者Logstach。
-
在每個node上都運行一個性能監控程序,採集該node的運行性能數據,例如prometheus node exporter、colletcd、new relic agent,或者ganglia gmond等。
daemonset的pod調度策略與rc類似,除了使用系統內置的算法在每個node上進行調度,也可以在pod的定義中使用nodeselector或nodeaffinity來指定滿足條件的node範圍進行調度。
下面的例子定義爲每個node上都啓動一個fluentd容器,配置文件fluentd-ds.yaml的內容如下,其中掛載了物理機的兩個目錄“/var/log”和“/var/lib/docker/containers”:
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: fluentd-cloud-logging
namespace: kube-system
labels:
k8s-app: fluentd-cloud-logging
spec:
template:
metadata:
namespace: kube-system
labels:
k8s-app: fluentd-cloud-logging
spec:
containers:
- name: fluentd-cloud-logging
image: ist0ne/fluentd-elasticsearch
resources:
limits:
cpu: 100m
memory: 200Mi
env:- name: FLUENTD_ARGS
value: -q
volumeMounts: - name: varlog
mountPath: /var/log
readOnly: false - name: containers
mountPath: /var/lib/docker/containers
readOnly: false
volumes:
- name: FLUENTD_ARGS
- name: containers
hostPath:
path: /var/lib/docker/containers - name: varlog
hostPath:
path: /var/log
在k8s 1.6以後的版本中,daemonset也能執行滾動升級了,即在更新一個daemonset模板的時候,舊的pod副本會被自動刪除,同時新的pod副本會被自動創建,此時daemonset的更新策略(updateStrategy)爲RollingUpdate,如下所示:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: goldpinger
spec:
updateStrategy:
type: RollingUpdate
updateStrategy 的另外一個值是OnDelete,即只有手動刪除了DaemonSet創建的pod副本,新的Pod副本纔會被創建出來。如果不設置updateStrategy的值,則在k8s 1.6之後的版本中會被默認設置爲RollingUpdate。