16.3.6 DaemonSet与节点级服务

DaemonSet 用于确保集群中每个(或特定)节点上始终运行一个 Pod,典型场景包括日志采集、监控探针、网络与存储插件、节点级安全代理等。其核心价值是“节点级一致性”和“随节点自动扩缩”,当新增节点加入集群时自动创建 Pod,节点移除时自动回收。

原理草图(节点级一致性)

文章图片

适用场景与特性要点
- 每个节点一份副本,适合与节点生命周期绑定的服务
- 支持 nodeSelectornodeAffinitytolerations 精准投放
- 支持滚动更新与版本回滚,节点级服务平滑升级
- 与 CNI/CSI、日志与监控系统天然配合

核心字段说明(带命令解释)
- selector:必须匹配 template.metadata.labels,否则控制器无法管理 Pod
- template:Pod 模板,定义容器、资源、环境变量、卷
- updateStrategy:更新策略,RollingUpdate 逐步替换
- tolerations:允许在带污点节点运行(如控制面节点)
- hostNetwork/hostPID/hostIPC:节点级服务常用权限
- minReadySeconds:Pod 就绪后等待时间,避免过快滚动


示例:部署 Node Exporter DaemonSet(含安装与验证)#

1)准备命名空间

kubectl create ns monitoring

命令解释:创建隔离监控资源的命名空间。

2)创建 DaemonSet(保存为 /opt/k8s/node-exporter-ds.yaml)

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: monitoring
  labels:
    app: node-exporter
spec:
  selector:
    matchLabels:
      app: node-exporter
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: node-exporter
    spec:
      hostNetwork: true
      hostPID: true
      tolerations:
      - key: "node-role.kubernetes.io/control-plane"
        operator: "Exists"
        effect: "NoSchedule"
      containers:
      - name: node-exporter
        image: prom/node-exporter:v1.6.1
        args:
        - "--path.procfs=/host/proc"
        - "--path.sysfs=/host/sys"
        - "--path.rootfs=/host/root"
        ports:
        - name: metrics
          containerPort: 9100
          hostPort: 9100
        volumeMounts:
        - name: proc
          mountPath: /host/proc
          readOnly: true
        - name: sys
          mountPath: /host/sys
          readOnly: true
        - name: root
          mountPath: /host/root
          readOnly: true
      volumes:
      - name: proc
        hostPath:
          path: /proc
      - name: sys
        hostPath:
          path: /sys
      - name: root
        hostPath:
          path: /

3)应用与查看

kubectl apply -f /opt/k8s/node-exporter-ds.yaml
kubectl -n monitoring get ds,node -o wide
kubectl -n monitoring get pods -o wide

命令解释:
- apply 创建或更新 DaemonSet
- get ds,node 查看 DaemonSet 期望/可用数量与节点列表
- get pods -o wide 验证每个节点是否都有 Pod

4)验证 metrics 端口

# 在任意节点上执行(或通过 kubectl exec 进入节点工具容器)
curl -s http://127.0.0.1:9100/metrics | head

预期效果:返回以 node_ 开头的指标。


选择范围与调度控制示例#

只在标记了监控标签的节点运行

kubectl label node node-1 monitor=true
# 在 DaemonSet 的 spec.template.spec 中增加
nodeSelector:
  monitor: "true"

仅在 GPU 节点运行(nodeAffinity 示例)

affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
      - matchExpressions:
        - key: node-type
          operator: In
          values: ["gpu"]

更新与回滚演示#

滚动更新

kubectl -n monitoring set image ds/node-exporter node-exporter=prom/node-exporter:v1.7.0
kubectl -n monitoring rollout status ds/node-exporter

命令解释:
- set image 修改镜像触发滚动升级
- rollout status 观察升级进度

回滚

kubectl -n monitoring rollout undo ds/node-exporter

常见故障排查#

1)Pod 未创建

kubectl -n monitoring describe ds node-exporter
kubectl get events -n monitoring --sort-by=.lastTimestamp | tail

排查点:selectortemplate.labels 是否匹配;nodeSelector 是否无节点满足。

2)Pod 未调度

kubectl -n monitoring describe pod <pod-name>

排查点:taints/tolerations 是否配置;节点资源不足或不可达。

3)更新卡住

kubectl -n monitoring get ds node-exporter -o yaml | grep -A3 updateStrategy
kubectl -n monitoring describe pod <pod-name> | grep -A2 Readiness

排查点:readiness 探针失败、maxUnavailable 设置过小。


练习(动手验证)#

  1. 在两个节点打标签 monitor=true,验证 DaemonSet 仅在标记节点运行。
  2. 模拟新增节点(或更改标签),观察新 Pod 自动创建。
  3. 修改镜像版本触发滚动更新,再执行回滚,记录状态变化。
  4. 给控制面节点加污点,验证 tolerations 对投放的影响。

通过合理的节点选择、权限与更新策略,DaemonSet 可构建稳定的节点级服务底座,是生产集群不可或缺的基础控制器之一。