【Kubernetes专项】K8s 控制器 DaemonSet 从入门到企业实战应用

十五、K8s 控制器 DaemonSet 从入门到企业实战应用

15.1 DaemonSet 控制器相关概念

15.1.1 DaemonSet 概述

DaemonSet 控制器能够确保 k8s集群 所有的节点都运行一个相同的 pod副本,当向 k8s集群中增加 node 节点时,这个 node 节点也会自动创建一个 pod 副本,当 node 节点从集群移除,这些 pod 也会自动删除;删除 Daemonset 也会删除它们创建的 pod。

15.1.2 DaemonSet 工作原理:如何管理 Pod?

​ Daemonset 的控制器会监听 kuberntes 的 daemonset对象、pod对象、node对象 ,这些被监听的对象之变动,就会触发 syncLoop循环 让 k8s集群 朝着daemonset对象描述的状态进行演进。

15.1.3 DaemonSet 典型应用场景
  1. 在集群的每个节点上运行存储,比如:glusterd 或 ceph。
  2. 在每个节点上运行日志收集组件,比如:flunentd、logstash、filebeat 等。
  3. 在每个节点上运行监控组件,比如:Prometheus、Node Exporter、collectd 等。
15.1.4 DaemonSet 与 Deployment 的区别?

15.2 DaemonSet 资源清单文件编写技巧

15.2.1 ds.spec字段
bash 复制代码
[root@k8s-master1 ~]# kubectl explain  ds.spec
  minReadySeconds       <integer>
  # Pod 就绪后的等待时间,默认为0
  
  revisionHistoryLimit  <integer>
  # 保留历史版本数量
  
  selector      <LabelSelector> -required-
  # 标签选择器
  
  template      <PodTemplateSpec> -required-
  # 资源模版
  
  updateStrategy        <DaemonSetUpdateStrategy>
  # 更新策略
15.2.2 ds.spec.selector字段
bash 复制代码
[root@k8s-master1 ~]# kubectl explain  ds.spec.selector
  matchExpressions      <[]LabelSelectorRequirement>
  matchLabels   <map[string]string>
15.2.3 ds.spec.updateStrategy字段
bash 复制代码
[root@k8s-master1 ~]# kubectl explain  ds.spec.updateStrategy
  rollingUpdate <RollingUpdateDaemonSet>
  type  <string>
     - `"OnDelete"`
     - `"RollingUpdate"`

15.3 DaemonSet 案例:部署日志收集组件 fluentd

bash 复制代码
# 导入资源包所需的镜像
m1 && n1 && n2 ~]#ctr -n k8s.io images import fluentd_2_5_1.tar.gz
docker.io/xianchao/fluentd:v2.5.1

[root@k8s-master1 daemonset]# kubectl get nodes
NAME                    STATUS   ROLES           AGE   VERSION
k8s-master1.kaser.org   Ready    control-plane   32d   v1.31.3
k8s-node1.kaser.org     Ready    work            32d   v1.31.3
k8s-node2.kaser.org     Ready    work            32d   v1.31.3

[root@k8s-master1 daemonset]# kubectl get nodes --show-labels
node-role.kubernetes.io/control-plane
# 这里也有可能是:node-role.kubernetes.io/master	版本问题导致的,自己查看

[root@k8s-master1 daemonset]# vim daemonset.yaml
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/control-plane"
          effect: NoSchedule
      containers:
        - name: fluentd-elasticsearch
          image: docker.io/xianchao/fluentd:v2.5.1
          imagePullPolicy: IfNotPresent
          resources:	# 资源配额
            limits:
              memory: 200Mi
              cpu: 100m
            requests:
              memory: 200Mi
              cpu: 100m
          volumeMounts:
            - name: var-log
              mountPath: /var/log
            - name: var-lib-docker-containers
              mountPath: /var/lib/docker/containers
      volumes:
        - name: var-log
          hostPath:
            path: /var/log
        - name: var-lib-docker-containers
          hostPath:
            path: /var/lib/docker/containers
      terminationGracePeriodSeconds: 30		# 优雅关闭服务

[root@k8s-master1 daemonset]# kubectl apply -f daemonset.yaml
# 查看DaemonSet资源
[root@k8s-master1 daemonset]# kubectl get ds -n kube-system | grep flu
fluentd-elasticsearch   3         3         3       3            3        

# 查看pod资源并观察master控制节点是否被调度
[root@k8s-master1 daemonset]# kubectl get pods -n kube-system -owide | grep flu
fluentd-elasticsearch-4gg6q                     1/1     Running   10.244.90.152    k8s-node2.kaser.org
fluentd-elasticsearch-8sr7k                     1/1     Running   10.244.160.66    k8s-master1.kaser.org
fluentd-elasticsearch-gbh8q                     1/1     Running   10.244.147.216   k8s-node1.kaser.org

15.4 DaemonSet 更新

15.4.1 ds.spec.updateStrategy字段
bash 复制代码
]# kubectl explain ds.spec.updateStrategy
  rollingUpdate <RollingUpdateDaemonSet>
  type  <string>
     - `"OnDelete"`
     - `"RollingUpdate"`
15.4.2 ds.spec.updateStrategy.rollingUpdate
bash 复制代码
]# kubectl explain ds.spec.updateStrategy.rollingUpdate
  maxSurge      <IntOrString>
  maxUnavailable
相关推荐
http阿拉丁神猫5 小时前
kubernetes知识点汇总37-42
云原生·容器·kubernetes
L1624766 小时前
Containerd 完整部署安装与使用指南
容器
360智汇云6 小时前
在OpenStack使用Ceph纠删码存储
云原生
2301_771717216 小时前
登录生成 Token + 网关解析 Token + 微服务透传 userId
微服务·云原生·架构
恼书:-(空寄6 小时前
K8s 网关(Ingress-Nginx/Envoy/云原生网关)20 个高频故障速查手册
云原生·k8s·ingress
w6100104667 小时前
CKA-2026-Gateway
kubernetes·gateway·cka
苏渡苇18 小时前
Docker 网络完全指南
网络·docker·容器·docker容器·容器通信
风向决定发型丶19 小时前
K8S CPU绑核详解
云原生·容器·kubernetes
KubeSphere 云原生19 小时前
KubeSphere Skills 正式发布:让 OpenClaw 更懂 KubeSphere
云原生
斯普信云原生组20 小时前
Docker 开源软件应急处理方案及操作手册——镜像管理与构建故障
docker·容器·eureka