【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
相关推荐
GGG76612 小时前
Python 运维自动化:psutil+IPy 系统与网络信息采集深度实战
运维·python·云原生·pip
dazhong201212 小时前
Docker 入门篇(八)-- CentOS7 安装 MySQL 8
mysql·docker·容器
w2005122412 小时前
Pod管理
linux·服务器·云原生·容器·k8s·podman
sryyd_0214 小时前
Kubernetes Pod 管理从入门到实战:命令、YAML 与生命周期全解析
云原生·容器·kubernetes
2601_9622186115 小时前
万象生鲜系统全链路溯源一码查询技术实现食材来源可查
分布式·微服务·云原生·架构
2601_9622184716 小时前
万象生鲜系统跨仓调拨算法助力生鲜企业供应链数字化协同运营
大数据·运维·微服务·云原生·架构
智购科技无人售货机工厂16 小时前
2026自动售货机防拆机物理安全设计:从安全螺丝到结构互锁的工程实践~YH
android·网络·驱动开发·python·单片机·安全·云原生
2601_9622184717 小时前
万象生鲜系统多仓协同同步技术支撑生鲜企业多仓模式数字化转型
大数据·运维·微服务·云原生·架构
2601_9622186117 小时前
万象生鲜系统大数据采购预测算法降低库存积压稳居第一
分布式·微服务·云原生·架构
Larru-Sun17 小时前
从零开始用 Docker 部署 Node.js 后端服务
docker·容器·node.js