K8S 之 DaemonSet

👨‍🎓博主简介

🏅CSDN博客专家

🏅云计算领域优质创作者

🏅华为云开发者社区专家博主

🏅阿里云开发者社区专家博主

💊交流社区: 运维交流社区 欢迎大家的加入!

🐋 希望大家多多支持,我们一起进步!😄

🎉如果文章对你有帮助的话,欢迎 点赞 👍🏻 评论 💬 收藏 ⭐️ 加关注+💗


文章目录

  • [一、DaemonSet 简介](#一、DaemonSet 简介)
    • [1.1 DaemonSet 是什么](#1.1 DaemonSet 是什么)
    • [1.2 DaemonSet 与 Deployment 的区别](#1.2 DaemonSet 与 Deployment 的区别)
    • [1.3 DaemonSet 的应用场景](#1.3 DaemonSet 的应用场景)
    • [1.4 节点污点 注意事项](#1.4 节点污点 注意事项)
  • [二、DaemonSet 举例](#二、DaemonSet 举例)
    • [2.1 写 nginx-daemonset.yaml](#2.1 写 nginx-daemonset.yaml)
    • [2.2 启动运行](#2.2 启动运行)
    • [2.3 验证](#2.3 验证)

一、DaemonSet 简介

1.1 DaemonSet 是什么

DaemonSet 是 Kubernetes 的一种控制器,缩写:DS,确保集群内每个节点上运行且仅运行一个 Pod 副本,当有节点加入集群时, 也会为他们新增一个 Pod 。当有节点从集群移除时,这些 Pod 也会被回收。删除 DaemonSet 将会删除它创建的所有 Pod。

官方文档:K8s中文官方文档 - DaemonSet

1.2 DaemonSet 与 Deployment 的区别

特性 DaemonSet Deployment
调度目标 每个节点强制一个 按副本数随机分布
适用场景 节点级守护进程 (监控、日志、网络) 无状态应用 无状态应用(Web、API、微服务)
扩缩容 自动随节点增减 手动调整 replicas

1.3 DaemonSet 的应用场景

  • 节点日志的收集:fluentdfilebeat
  • 节点监控指标采集:node_exportercadvisor
  • 网络代理/CNI 插件:calicoflannel
  • 存储守护进程:Ceph OSDGlusterFSOpenEBSLonghorn
  • 安全/审计代理:FalcoAuditdSysdig

1.4 节点污点 注意事项

从 Kubernetes 1.6 开始,Master 节点默认排斥 DaemonSet Pod

原因 :Master 节点带有 node-role.kubernetes.io/master:NoSchedule 污点,而 DaemonSet 创建的 Pod 默认无此容忍度,因此不会被调度。

建议 :如无特殊需求(如控制面监控、API Server 指标采集),不要将 DaemonSet Pod 调度到 Master 节点,避免占用控制面资源。


如需调度到 Master 节点

在 DaemonSet 的 Pod 模板中添加容忍度:

yaml 复制代码
tolerations:
  - key: node-role.kubernetes.io/master
    effect: NoSchedule
    operator: Exists
  # K8s 1.20+ 推荐使用 control-plane 标签
  - key: node-role.kubernetes.io/control-plane
    effect: NoSchedule
    operator: Exists

⚠️ 注意 :K8s 1.24+ 已废弃 master 标签,改用 control-plane,建议同时容忍两者以保证兼容性。

如何判断节点有没有污点

bash 复制代码
kubectl describe node <master-node> | grep Taints

返回:
<none> 节点无污点,任何 Pod 都可调度;
node-role.kubernetes.io/master:NoSchedulenode-role.kubernetes.io/control-plane:NoSchedule 有污点,Pod 需添加对应 容忍:tolerations 或 去除污点:taints Pod才能进行调度;

  • 去除污点
bash 复制代码
# 去除 master 污点(K8s 1.23 及以下)
kubectl taint node <master-node-name> node-role.kubernetes.io/master:NoSchedule-

# 去除 control-plane 污点(K8s 1.24+)
kubectl taint node <master-node-name> node-role.kubernetes.io/control-plane:NoSchedule-
  • 恢复污点
bash 复制代码
# 重新添加 Master 污点(K8s 1.23 及以下)
kubectl taint node <master-node> node-role.kubernetes.io/master:NoSchedule

# 或 control-plane 污点(K8s 1.24+)
kubectl taint node <master-node> node-role.kubernetes.io/control-plane:NoSchedule

二、DaemonSet 举例

需求:再每一个节点上部署一个nginx服务;

2.1 写 nginx-daemonset.yaml

  • nginx-daemonset.yaml
yaml 复制代码
apiVersion: v1
kind: Namespace
metadata: 
  name: nginx
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx-daemonset
  namespace: nginx
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      hostNetwork: true
      tolerations:           # ← 添加容忍度,如果不添加,则不会再有污点的节点上部署
        - key: node-role.kubernetes.io/master
          effect: NoSchedule
          operator: Exists
        - key: node-role.kubernetes.io/control-plane
          effect: NoSchedule
          operator: Exists
      containers:
      - name: nginx
        image: nginx:1.24.0
        ports:
        - containerPort: 80
        resources:         
          requests:
            cpu: "100m"
            memory: "128Mi"
          limits:
            cpu: "500m"
            memory: "256Mi"
  • 注意事项
    • DaemonSet 不能有 replicas 字段

2.2 启动运行

bash 复制代码
kubectl apply -f nginx-daemonset.yaml

2.3 验证

bash 复制代码
# 查看nginx pod是否每个节点都部署了一个
kubectl get pods -n nginx -o wide

# 查看DaemonSet中有没有nginx
kubectl get ds -n nginx -o wide

如图:可以看到三个节点都部署了nginx,那么就没问题了

  • 页面访问测试;
    172.16.11.230
    172.16.11.231
    172.16.11.232

访问没问题,成功!!!

相关推荐
2601_949146536 小时前
Shell语音通知接口使用指南:运维自动化中的语音告警集成方案
运维·自动化
儒雅的晴天6 小时前
大模型幻觉问题
运维·服务器
Gofarlic_OMS7 小时前
科学计算领域MATLAB许可证管理工具对比推荐
运维·开发语言·算法·matlab·自动化
通信大师7 小时前
深度解析PCC策略计费控制:核心网产品与应用价值
运维·服务器·网络·5g
dixiuapp7 小时前
智能工单系统如何选,实现自动化与预测性维护
运维·自动化
Elastic 中国社区官方博客7 小时前
如何防御你的 RAG 系统免受上下文投毒攻击
大数据·运维·人工智能·elasticsearch·搜索引擎·ai·全文检索
小锋学长生活大爆炸7 小时前
【教程】免Root在Termux上安装Docker
运维·docker·容器
进击切图仔7 小时前
常用 Docker 命令备份
运维·docker·容器
NotStrandedYet8 小时前
《国产系统运维笔记》第8期:挑战国产化流媒体部署——银河麒麟+龙芯架构编译SRS实战全记录
运维·kylin·国产化·银河麒麟·龙芯·信创运维·srs编译安装
默|笙9 小时前
【Linux】fd_重定向本质
linux·运维·服务器