DaemonSets

目录

理解守护进程集

参考官方 Kubernetes 文档:DaemonSets

DaemonSet 是 Kubernetes 中的一种控制器,用于确保所有(或部分)集群节点上运行指定 Pod 的一个副本。与以应用为中心的 Deployment 不同,DaemonSet 以节点为中心,非常适合部署集群范围的基础设施服务,如日志收集器、监控代理或存储守护进程。

WARNING

DaemonSet 操作注意事项

  1. 行为特征

    • Pod 分布:DaemonSet 会在每个符合条件的可调度 Node 上部署且仅部署一个 Pod 副本:

      • 在每个符合以下条件的可调度节点上部署且仅部署一个 Pod 副本:
        • 匹配 nodeSelectornodeAffinity 条件(如果指定)。
        • 节点状态不是 NotReady
        • 节点没有 NoScheduleNoExecute Taints,除非 Pod 模板中配置了相应的 Tolerations
    • Pod 数量公式:DaemonSet 管理的 Pod 数量等于符合条件的节点数量。

    • 双重角色节点处理:同时担任 控制平面工作节点 角色的节点,只会运行一个 DaemonSet Pod 实例(无论其角色标签如何),前提是该节点可调度。

  2. 关键限制(排除节点)

    • 明确标记为 Unschedulable: true 的节点(例如通过 kubectl cordon 设置)。
    • 状态为 NotReady 的节点。
    • 具有不兼容 Taints 且在 DaemonSet Pod 模板中未配置匹配 Tolerations 的节点。

创建守护进程集

使用 CLI 创建守护进程集

前提条件

  • 确保已配置并连接到集群的 kubectl

YAML 文件示例

# example-daemonSet.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  namespace: kube-system
  labels:
    k8s-app: fluentd-logging
spec:
  selector: # 定义 DaemonSet 如何识别其管理的 Pods,必须匹配 `template.metadata.labels`
    matchLabels:
      name: fluentd-elasticsearch
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
  template: # 定义 DaemonSet 的 Pod 模板,DaemonSet 创建的每个 Pod 都符合此模板
    metadata:
      labels:
        name: fluentd-elasticsearch
    spec:
      tolerations: # 这些容忍配置允许 daemonset 在控制平面节点上运行,如不需运行可移除
        - key: node-role.kubernetes.io/control-plane
          operator: Exists
          effect: NoSchedule
        - key: node-role.kubernetes.io/master
          operator: Exists
          effect: NoSchedule
      containers:
        - name: fluentd-elasticsearch
          image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
          resources:
            limits:
              memory: 200Mi
            requests:
              cpu: 100m
              memory: 200Mi
          volumeMounts:
            - name: varlog
              mountPath: /var/log
      # 可以设置较高的优先级类,确保 DaemonSet Pod 优先抢占运行中的 Pod
      # priorityClassName: important
      terminationGracePeriodSeconds: 30
      volumes:
        - name: varlog
          hostPath:
            path: /var/log

通过 YAML 创建守护进程集

# 第一步:执行以下命令创建 *example-daemonSet.yaml* 中定义的 DaemonSet
kubectl apply -f example-daemonSet.yaml

# 第二步:验证守护进程集及其管理的 Pods 状态
kubectl get daemonset fluentd-elasticsearch # 查看 DaemonSet
kubectl get pods -l name=fluentd-elasticsearch -o wide # 查看该 DaemonSet 管理的 Pods 及其所在节点

使用 Web 控制台创建守护进程集

前提条件

获取镜像地址。镜像来源可以是平台管理员通过工具链集成的镜像仓库,也可以是第三方平台的镜像仓库。

  • 对于前者,管理员通常会将镜像仓库分配给你的项目,你可以使用其中的镜像。如果找不到所需镜像仓库,请联系管理员进行分配。

  • 对于第三方平台的镜像仓库,请确保当前集群可以直接拉取该镜像。

操作步骤 - 配置基本信息

  1. Container Platform 中,左侧导航栏进入 Workloads > DaemonSets

  2. 点击 Create DaemonSet

  3. 选择输入镜像地址,点击 Confirm

INFO

注意:使用 Web 控制台集成的镜像仓库时,可以通过 Already Integrated 过滤镜像。Integration Project Name 例如 images (docker-registry-projectname),其中 projectname 是该 Web 控制台中的项目名,也是镜像仓库中的项目名。

Basic Info 部分,配置守护进程集工作负载的声明式参数:

参数说明
More > Update Strategy配置 DaemonSet Pod 零停机更新的 rollingUpdate 策略。
最大不可用数maxUnavailable):更新过程中允许临时不可用的最大 Pod 数量。支持绝对值(如 1)或百分比(如 10%)。
示例:若有 10 个节点且 maxUnavailable 为 10%,则允许最多 floor(10 * 0.1) = 1 个 Pod 不可用。

注意事项:
  • 默认值:若未显式设置,maxSurge 默认为 0,maxUnavailable 默认为 1(或百分比时为 10%)。
  • 非运行状态 Pod:处于 PendingCrashLoopBackOff 等状态的 Pod 被视为不可用。
  • 同时限制maxSurgemaxUnavailable 不能同时为 0 或 0%。若百分比计算结果均为 0,Kubernetes 会强制将 maxUnavailable 设为 1 以保证更新进度。

操作步骤 - 配置 Pod

Pod 部分,请参考 Deployment - Configure Pod

操作步骤 - 配置容器

Containers 部分,请参考 Deployment - Configure Containers

操作步骤 - 创建

点击 Create

点击 Create 后,DaemonSet 将:

  • ✅ 自动在所有符合以下条件的节点上部署 Pod 副本:

    • 满足 nodeSelector 条件(如果定义)。
    • 配置了 tolerations(允许调度到带有 Taints 的节点)。
    • 节点状态为 ReadySchedulable: true
  • ❌ 排除节点:

    • 带有 NoSchedule Taint(除非显式容忍)。
    • 手动标记为不可调度的节点(kubectl cordon)。
    • 状态为 NotReadyUnschedulable 的节点。

管理守护进程集

使用 CLI 管理守护进程集

查看守护进程集

  • 获取某命名空间下所有守护进程集的摘要信息:

    kubectl get daemonsets -n <namespace>
  • 获取指定守护进程集的详细信息,包括事件和 Pod 状态:

    kubectl describe daemonset <daemonset-name>

更新守护进程集

当修改守护进程集的 Pod 模板(例如更改容器镜像或添加卷挂载)时,Kubernetes 默认会执行滚动更新(前提是 updateStrategy.typeRollingUpdate,这是默认值)。

  • 首先编辑 YAML 文件(如 example-daemonset.yaml),进行所需修改,然后应用:

    kubectl apply -f example-daemonset.yaml
  • 可以监控滚动更新的进度:

    kubectl rollout status daemonset/<daemonset-name>

删除守护进程集

删除守护进程集及其管理的所有 Pod:

kubectl delete daemonset <daemonset-name>

使用 Web 控制台管理守护进程集

查看守护进程集

  1. Container Platform 中,进入 Workloads > DaemonSets
  2. 找到要查看的守护进程集。
  3. 点击守护进程集名称,查看 DetailsTopologyLogsEventsMonitoring 等信息。

更新守护进程集

  1. Container Platform 中,进入 Workloads > DaemonSets
  2. 找到要更新的守护进程集。
  3. Actions 下拉菜单中选择 Update,进入编辑守护进程集页面,可更新 ReplicasimageupdateStrategy 等参数。

删除守护进程集

  1. Container Platform 中,进入 Workloads > DaemonSets
  2. 找到要删除的守护进程集。
  3. Actions 下拉菜单中点击操作列的 Delete 按钮并确认。