【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
相关推荐
Linux-18749 小时前
分布式链路追踪系统之docker-compose安装skywalking
云原生·docker-compose·skywalking·分布式链路追踪系统·应用程序性能监控
生活爱好者!14 小时前
我把NAS当作下载机,docker一键部署qb
运维·docker·容器
隔窗听雨眠14 小时前
Spring Boot在云原生时代的编程范式革新研究
spring boot·后端·云原生
Hui Baby16 小时前
K8S使用CRD和控制器简单应用
云原生·容器·kubernetes
阿里云云原生17 小时前
从“告警驱动”到“巡检驱动”:Agent 智能体如何实现前端体验治理的闭环?
云原生
AOwhisky18 小时前
Python 学习笔记(第十三期)——运维自动化(下·前篇):远程命令执行——paramiko基础篇
运维·python·学习·云原生·自动化·运维开发·paramiko
AOwhisky18 小时前
Python 学习笔记(第十四期)——运维自动化(下·中篇):远程文件传输——paramiko进阶篇
运维·python·学习·云原生·自动化·文件传输·paramiko
做个文艺程序员18 小时前
Linux第22篇:用Docker容器化你的Java SaaS应用:一次构建,随处运行
java·docker·容器
Zhu75821 小时前
在k8s环境部署Apache Superset最新版
容器·kubernetes·apache
小的博客21 小时前
windows下安装Docker Desptop
运维·docker·容器