DaemonSet 守护进程集
DaemonSet 保证在"每一个满足条件的节点"上都恰好运行一个 Pod 副本:新节点加入集群会自动补上,节点被删除则副本随之消失。日志采集、监控探针这类"每个节点都要有一份"的代理程序,正是它的典型用法。
用在哪里
- 日志收集:如 Filebeat、Fluentd,采集每台节点上的容器日志。
- 监控采集:如 Prometheus Node Exporter,暴露每台节点的指标。
- 网络与存储组件:kube-proxy、CNI 插件等常以 DaemonSet 形式跑在每台节点上。
- 安全与缓存代理:需要覆盖全节点的轻量代理。
与 Deployment 的区别一句话:Deployment 的副本数由你指定、分散调度;DaemonSet 的副本数由"符合条件的节点数"决定,每节点恰一个。
YAML 骨架
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter
labels:
app: node-exporter
spec:
selector:
matchLabels:
app: node-exporter
template:
metadata:
labels:
app: node-exporter
spec:
nodeSelector: # 只跑在带 disktype=ssd 标签的节点上
disktype: ssd
tolerations: # 容忍主节点等带污点的节点(默认不调度)
- key: node-role.kubernetes.io/control-plane
effect: NoSchedule
containers:
- name: exporter
image: prom/node-exporter:1.8.2
ports:
- containerPort: 9100
节点约束:nodeSelector 与 tolerations
- nodeSelector:把 Pod 限定到带指定标签的节点。不写就默认跑遍所有节点。
- tolerations(污点容忍):节点可打污点(taint)拒绝普通 Pod;控制面节点默认带 NoSchedule 污点,DaemonSet 想在上面也放副本,就得加对应 tolerations。
kubectl get daemonset -n kube-system # 看看 kube-proxy 是否以 DaemonSet 运行
kubectl get pods -o wide -l app=node-exporter
# NAME READY STATUS RESTARTS AGE NODE
# node-exporter-abc12 1/1 Running 0 5m node-1
# node-exporter-def34 1/1 Running 0 5m node-2
节点数变化时 DaemonSet 会自动跟随:给集群加一台工作节点,片刻后新节点上就会出现对应副本,无需手动扩缩容。
更新策略
DaemonSet 默认也用滚动更新(RollingUpdate),逐个替换旧 Pod,避免整批重启造成采集断档;可通过 maxUnavailable 控制同时不可用的数量:
spec:
updateStrategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1 # 同一时刻最多 1 个副本不可用
kubectl set image daemonset node-exporter node-exporter=prom/node-exporter:1.9.0
kubectl rollout status daemonset node-exporter
# daemon set "node-exporter" successfully rolled out
kubectl rollout history daemonset node-exporter # 同样支持查看与回滚
小结:DaemonSet 让"每节点一份"成为集群层面的自动保证,节点增删自动跟随。配合 nodeSelector 限定范围、tolerations 打入特殊节点、滚动更新平滑升级,日志与监控这类基础设施就能全集群无死角覆盖。