当前位置: 首页 > news >正文

Kubernetes DaemonSet深度解析:管理集群守护进程的最佳实践

Kubernetes DaemonSet深度解析:管理集群守护进程的最佳实践

一、DaemonSet概述

DaemonSet是Kubernetes中用于在集群的每个节点上运行一个Pod副本的控制器。它确保所有节点(或满足特定条件的节点)都运行该Pod的一个实例。

1.1 DaemonSet应用场景

场景说明示例
日志收集每个节点运行日志收集器Fluentd、Filebeat
监控代理每个节点运行监控采集器Prometheus Node Exporter
网络插件每个节点运行网络组件Calico、Flannel
存储代理每个节点运行存储驱动CSI节点插件
安全代理每个节点运行安全组件入侵检测系统

1.2 DaemonSet vs Deployment

特性DaemonSetDeployment
副本数每个节点一个任意数量
调度节点级别Pod级别
更新策略滚动更新/替换滚动更新/重建
适用场景节点守护进程应用服务

二、DaemonSet核心配置

2.1 基本DaemonSet配置

apiVersion: apps/v1 kind: DaemonSet metadata: name: fluentd namespace: kube-system labels: k8s-app: fluentd-logging spec: selector: matchLabels: name: fluentd template: metadata: labels: name: fluentd spec: tolerations: - key: node-role.kubernetes.io/control-plane operator: Exists effect: NoSchedule containers: - name: fluentd image: fluentd:v1.12 resources: limits: memory: 200Mi requests: cpu: 100m memory: 200Mi volumeMounts: - name: varlog mountPath: /var/log - name: varlibdockercontainers mountPath: /var/lib/docker/containers readOnly: true volumes: - name: varlog hostPath: path: /var/log - name: varlibdockercontainers hostPath: path: /var/lib/docker/containers

2.2 节点选择器配置

apiVersion: apps/v1 kind: DaemonSet metadata: name: node-exporter spec: selector: matchLabels: app: node-exporter template: metadata: labels: app: node-exporter spec: nodeSelector: kubernetes.io/os: linux node-role.kubernetes.io/worker: "" containers: - name: node-exporter image: prom/node-exporter:v1.2.0

2.3 污点容忍配置

apiVersion: apps/v1 kind: DaemonSet metadata: name: calico-node spec: selector: matchLabels: k8s-app: calico-node template: metadata: labels: k8s-app: calico-node spec: tolerations: - key: node-role.kubernetes.io/control-plane operator: Exists effect: NoSchedule - key: node-role.kubernetes.io/master operator: Exists effect: NoSchedule - key: node.kubernetes.io/not-ready operator: Exists effect: NoExecute

三、DaemonSet更新策略

3.1 滚动更新

apiVersion: apps/v1 kind: DaemonSet metadata: name: fluentd spec: updateStrategy: type: RollingUpdate rollingUpdate: maxUnavailable: 1 maxSurge: 0

3.2 替换更新

apiVersion: apps/v1 kind: DaemonSet metadata: name: legacy-daemon spec: updateStrategy: type: OnDelete

四、DaemonSet部署与管理

4.1 部署DaemonSet

kubectl apply -f daemonset.yaml # 查看DaemonSet状态 kubectl get daemonset # 查看Pod分布 kubectl get pods -l name=fluentd -o wide # 查看DaemonSet详情 kubectl describe daemonset fluentd

4.2 滚动更新操作

# 更新镜像版本 kubectl set image daemonset/fluentd fluentd=fluentd:v1.13 # 查看更新状态 kubectl rollout status daemonset/fluentd # 暂停更新 kubectl rollout pause daemonset/fluentd # 恢复更新 kubectl rollout resume daemonset/fluentd # 回滚更新 kubectl rollout undo daemonset/fluentd

4.3 查看历史版本

kubectl rollout history daemonset/fluentd kubectl rollout history daemonset/fluentd --revision=2

五、DaemonSet最佳实践

5.1 日志收集DaemonSet

apiVersion: apps/v1 kind: DaemonSet metadata: name: filebeat namespace: logging spec: selector: matchLabels: app: filebeat template: metadata: labels: app: filebeat annotations: co.elastic.logs/module: docker spec: serviceAccountName: filebeat terminationGracePeriodSeconds: 30 containers: - name: filebeat image: elastic/filebeat:7.15.0 args: - -e - -c - /etc/filebeat.yml env: - name: ELASTICSEARCH_HOSTS value: "elasticsearch:9200" securityContext: runAsUser: 0 volumeMounts: - name: config mountPath: /etc/filebeat.yml subPath: filebeat.yml - name: data mountPath: /usr/share/filebeat/data - name: varlibdockercontainers mountPath: /var/lib/docker/containers readOnly: true - name: varlog mountPath: /var/log readOnly: true volumes: - name: config configMap: name: filebeat-config - name: data hostPath: path: /var/lib/filebeat-data type: DirectoryOrCreate - name: varlibdockercontainers hostPath: path: /var/lib/docker/containers - name: varlog hostPath: path: /var/log

5.2 节点监控DaemonSet

apiVersion: apps/v1 kind: DaemonSet metadata: name: node-exporter namespace: monitoring spec: selector: matchLabels: app: node-exporter template: metadata: labels: app: node-exporter annotations: prometheus.io/scrape: "true" prometheus.io/port: "9100" spec: hostNetwork: true hostPID: true containers: - name: node-exporter image: prom/node-exporter:v1.2.0 args: - --path.procfs=/host/proc - --path.sysfs=/host/sys - --collector.filesystem.ignored-mount-points=^/(dev|proc|sys|var/lib/docker/.+|var/lib/kubelet/.+)($|/) resources: limits: cpu: 100m memory: 100Mi requests: cpu: 100m memory: 100Mi volumeMounts: - name: proc mountPath: /host/proc readOnly: true - name: sys mountPath: /host/sys readOnly: true volumes: - name: proc hostPath: path: /proc - name: sys hostPath: path: /sys

5.3 网络插件DaemonSet

apiVersion: apps/v1 kind: DaemonSet metadata: name: flannel namespace: kube-system spec: selector: matchLabels: app: flannel template: metadata: labels: app: flannel spec: hostNetwork: true tolerations: - operator: Exists containers: - name: kube-flannel image: quay.io/coreos/flannel:v0.14.0 command: - /opt/bin/flanneld args: - --ip-masq - --kube-subnet-mgr securityContext: privileged: true env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace volumeMounts: - name: run mountPath: /run/flannel - name: cni mountPath: /etc/cni/net.d - name: etc-flannel mountPath: /etc/flannel volumes: - name: run hostPath: path: /run/flannel - name: cni hostPath: path: /etc/cni/net.d - name: etc-flannel hostPath: path: /etc/flannel

六、DaemonSet监控与调试

6.1 状态检查

# 查看DaemonSet状态 kubectl get ds # 查看DaemonSet详情 kubectl describe ds <name> # 查看Pod状态 kubectl get pods -l <label> -o wide # 查看节点上的Pod分布 kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.addresses[0].address}{"\n"}{end}'

6.2 日志查看

# 查看所有Pod日志 kubectl logs -l app=node-exporter # 查看特定节点的Pod日志 kubectl logs -l app=node-exporter -n monitoring --field-selector spec.nodeName=node-1 # 流式日志 kubectl logs -f <pod-name>

6.3 调试命令

# 在特定节点上执行命令 kubectl exec <pod-name> -- cat /var/log/messages # 查看节点信息 kubectl describe node <node-name> # 查看节点污点 kubectl get node <node-name> -o jsonpath='{.spec.taints}'

七、性能优化

7.1 资源限制配置

apiVersion: apps/v1 kind: DaemonSet metadata: name: optimized-daemon spec: template: spec: containers: - name: daemon image: my-daemon resources: requests: cpu: "100m" memory: "200Mi" limits: cpu: "500m" memory: "500Mi"

7.2 优先级配置

apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: system-node-critical value: 2000001000 description: "Priority class for system node critical components." --- apiVersion: apps/v1 kind: DaemonSet metadata: name: critical-daemon spec: template: spec: priorityClassName: system-node-critical containers: - name: daemon image: critical-component

7.3 调度约束

apiVersion: apps/v1 kind: DaemonSet metadata: name: constrained-daemon spec: template: spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: topology.kubernetes.io/zone operator: In values: - zone-a - zone-b containers: - name: daemon image: zone-aware-daemon

八、常见问题与解决方案

8.1 Pod无法调度到节点

问题:DaemonSet Pod在某些节点上Pending

原因分析

  • 节点有污点且DaemonSet没有相应的容忍
  • 节点资源不足
  • 节点选择器不匹配

解决方案

kubectl describe node <node-name> | grep Taints kubectl get ds <name> -o yaml | grep tolerations

8.2 更新卡住

问题:滚动更新卡在某个节点

原因分析

  • 节点不可用
  • Pod健康检查失败
  • 资源不足

解决方案

kubectl rollout status ds <name> kubectl describe pod <pod-name> kubectl rollout pause ds <name>

8.3 镜像拉取失败

问题:DaemonSet无法拉取镜像

原因分析

  • 镜像仓库不可达
  • 镜像名称或标签错误
  • 认证配置问题

解决方案

kubectl describe pod <pod-name> | grep -A 5 Events kubectl get secret regcred -o yaml

九、总结

DaemonSet是管理集群级守护进程的核心控制器,适用于需要在每个节点上运行的系统级服务。通过合理配置,可以实现:

  1. 节点级部署:确保每个节点都运行守护进程
  2. 自动扩展:新节点加入时自动部署Pod
  3. 滚动更新:安全地更新守护进程版本
  4. 节点隔离:通过污点和容忍控制Pod部署位置

建议在部署日志收集、监控代理、网络插件等系统服务时使用DaemonSet,并结合资源限制和优先级配置确保系统稳定性。


参考资料

  • Kubernetes DaemonSet官方文档
  • DaemonSet更新策略
  • 污点与容忍文档
http://www.jsqmd.com/news/873922/

相关文章:

  • 限时解密:Midjourney未公开的复古风格隐藏指令集(--grain 0.8 --fade 0.65 --halation true),仅剩最后87个测试席位
  • 第 2 篇:Agent 的三种工作模式,选错了事倍功半
  • Easysearch 版本进化全图——从 ES 国产替代到 AI Native 搜索数据库
  • 从零入门 OpenAI Codex|登录、权限、终端、记忆配置全实操
  • qKnow 智能体构建平台 v2.2.0 重磅更新!视觉焕新 + 数据看板 + 功能拓展全方位升级
  • 嵌入式C语言开发中的三大致命陷阱
  • 【Linux驱动开发】第12天:Linux设备树核心:树形结构+节点+属性 完整全解
  • 合肥市内10家防水补漏公司实战推荐 - 资讯纵览
  • AI正在重构工程师岗位:被替代的不是“人”,而是低维度能力
  • GPS测速仪SpeedView 3.2.0汉化版 精准速度 实时测速工具
  • 从 MacBook Air 到机器人:Caitlin Kalinowski 谈「硬件只有五次编译机会」
  • 第二周学习
  • 清远厂房搬家无缝攻略:费用明细 靠谱公司实测推荐 - 从来都是英雄出少年
  • pod创建
  • 永磁同步电机-叶片耦合激振系统数学建模
  • 从Java全栈开发到云原生:一次真实的面试对话与技术剖析
  • 2026高口碑木薯猫砂排行榜!兼顾安全与实用性,养猫党闭眼入 - 资讯纵览
  • C166 Class B硬件陷阱解析与调试实战
  • Shutter Encoder:构建高效媒体工作流的FFmpeg图形化解决方案
  • 【电机】基于matlab电机温度的BLDC冷却系统【含Matlab源码 15554期】
  • JDK常用类与工具(速览版)
  • 传统FPM项目怎么渐进式迁移到Swoole/Hyperf?
  • 清远搬厂公司推荐:实惠靠谱、无缝搬家全攻略2026 - 从来都是英雄出少年
  • MNBVC:重塑中文AI数据生态的突破性基础设施
  • 陈彪院士:一生奉献太阳物理,一心报国照亮苍穹
  • 企业部署文件加密系统后,员工嫌卡顿怎么办?我们这样优化策略
  • 最近调研了几套开源商城系统,聊聊真实二开体验
  • synapse-graph,图记忆skills——给全栈个体户的图拓扑工程记忆系统
  • Keil C166嵌入式开发中的宽字符实现与优化
  • 宣威龙泉汽修,宣威修车哪家好 - 资讯纵览