一個DaemonSet對象能確保其建立的Pod在集羣中的每一臺(或指定)Node上都運行一個副本。若是集羣中動態加入了新的Node,DaemonSet中的Pod也會被添加在新加入Node上運行。刪除一個DaemonSet也會級聯刪除全部其建立的Pod。下面是一些典型的DaemonSet的使用場景:node
下面的描述文件建立了一個運行着fluentd-elasticsearch鏡像的DaemonSet對象:git
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-elasticsearch
namespace: kube-system
labels:
k8s-app: fluentd-logging
spec:
selector:
matchLabels:
name: fluentd-elasticsearch
template:
metadata:
labels:
name: fluentd-elasticsearch
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers:
- name: fluentd-elasticsearch
image: k8s.gcr.io/fluentd-elasticsearch:1.20
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
terminationGracePeriodSeconds: 30
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
在Kubernetes 1.8以後,必須指定.spec.selector
來肯定這個DaemonSet對象管理的Pod,一般與.spec.template.metadata.labels
中定義的Pod的label一致。github
經過指定.spec.template.spec.nodeSelector
或.spec.template.spec.affinity
,DaemonSet Controller會將Pod建立在特定的Node上。更過關於NodeSelector和NodeAffinity的知識,請參考:https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#node-affinity-beta-featuredocker
默認狀況下,Pod被分配到具體哪一臺Node上運行是由Scheduler(負責分配調度Pod到集羣內的Node上,它經過監聽ApiServer,查詢還未分配Node的Pod,而後根據調度策略爲這些Pod分配Node)決定的。可是,DaemonSet對象建立的Pod卻擁有一些特殊的特性:api
unschedulable
屬性會被DaemonSet Controller忽略。Daemon Pods支持taints and tolerations, 可是這些Pods在建立時就默認容忍下列effect爲NoExecute的taints(未設置tolerationSeconds):app
Toleration Key | Effect | Version | Description |
---|---|---|---|
node.kubernetes.io/not-ready | NoExecute | 1.13+ | DaemonSet pods will not be evicted when there are node problems such as a network partition. |
node.kubernetes.io/unreachable | NoExecute | 1.13+ | DaemonSet pods will not be evicted when there are node problems such as a network partition. |
node.kubernetes.io/disk-pressure | NoSchedule | 1.8+ | ... |
node.kubernetes.io/memory-pressure | NoSchedule | 1.8+ | ... |
node.kubernetes.io/unschedulable | NoSchedule | 1.12+ | DaemonSet pods tolerate unschedulable attributes by default scheduler. |
node.kubernetes.io/network-unavailable | NoSchedule | 1.12+ | DaemonSet pods, who uses host network, tolerate network-unavailable attributes by default scheduler. |