守护进程集

理解 DaemonSets

请参阅 Kubernetes 官方文档:DaemonSets

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

WARNING

DaemonSet 运行说明

  1. 行为特征

    • Pod 分布:DaemonSet 会在每个满足条件且可调度的 Node 上部署恰好一个 Pod 副本:

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

    • 双角色节点处理:同时承担 Control PlaneWorker Node 角色的节点,只要可调度,无论其角色标签如何,都只会运行一个 DaemonSet 的 Pod 实例。

  2. 关键约束(被排除的节点)

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

创建 DaemonSets

使用 CLI 创建 DaemonSet

前提条件

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

YAML 文件示例

# example-daemonSet.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  labels:
    k8s-app: fluentd-logging
spec:
  selector: # defines how the DaemonSet identifies its managed Pods. Must match `template.metadata.label`s.
    matchLabels:
      name: fluentd-elasticsearch
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
  template: # defines the Pod Template for the DaemonSet. Each Pod created by this DaemonSet will conform to this template
    metadata:
      labels:
        name: fluentd-elasticsearch
    spec:
      tolerations: # these tolerations are to have the daemonset runnable on control plane nodes, remove them if your control plane nodes should not run pods
        - 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
      # it may be desirable to set a high priority class to ensure that a DaemonSet Pod
      # preempts running Pods
      # priorityClassName: important
      terminationGracePeriodSeconds: 30
      volumes:
        - name: varlog
          hostPath:
            path: /var/log

通过 YAML 创建 DaemonSet

# Step 1: To create the DaemonSet defined in *example-daemonSet.yaml*, execute the following command
kubectl apply -f example-daemonSet.yaml

# Step 2: To verify the creation and status of your DaemonSet and its associated Pods:
kubectl get daemonset fluentd-elasticsearch # View DaemonSet
kubectl get pods -l name=fluentd-elasticsearch -o wide # Check Pods managed by this DaemonSet on specific nodes

使用 Web 控制台创建 DaemonSet

前提条件

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

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

  • 如果是第三方平台的镜像仓库,请确保在当前集群中可以直接从该仓库拉取镜像。

  • 如果镜像仓库需要认证,则需要配置相应的 image pull secret。更多信息,请参阅 为 ServiceAccount 添加 ImagePullSecrets

  • 通过 OCI Connector 选择镜像时,还需要为 Connector 代理选择一个 image pull Secret,或者确认工作负载的 ServiceAccount 已经引用了该 Secret。请参阅 通过 OCI Connector 使用 ImagePullSecret

操作步骤 - 配置基本信息

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

  2. 点击 Create DaemonSet

  3. 选择输入一个镜像,然后点击 Confirm

INFO

注意:当使用 Web 控制台中已集成的镜像仓库里的镜像时,可以通过 Already Integrated 进行筛选。Integration Project Name,例如镜像(registry-projectname),其中包含此 Web 控制台中的项目名称 projectname 以及镜像仓库中的项目名称 containers。

Basic Info 部分,为 DaemonSet 工作负载配置声明式参数:

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

说明:
  • 默认值:如果未显式设置,maxSurge 默认值为 0,maxUnavailable 默认值为 1(如果 maxUnavailable 以百分比指定,则为 10%)。
  • 未运行的 Pod:处于 PendingCrashLoopBackOff 等状态的 Pod 会被视为不可用。
  • 同时限制maxSurgemaxUnavailable 不能同时为 0 或 0%。如果百分比值计算后两者都为 0,Kubernetes 会强制将 maxUnavailable=1,以确保更新继续进行。

操作步骤 - 配置 Pod

Pod 部分请参阅 Deployment - 配置 Pod

操作步骤 - 配置容器

Containers 部分请参阅 Deployment - 配置容器

操作步骤 - 创建

点击 Create

点击 Create 后,DaemonSet 将:

  • ✅ 自动将 Pod 副本部署到所有满足以下条件的合格节点上:

    • 满足 nodeSelector 条件(如果已定义)。
    • 配置了 tolerations(允许调度到带污点的节点)。
    • 节点处于 Ready 状态且 Schedulable: true
  • ❌ 被排除的节点:

    • 带有 NoSchedule 污点的节点(除非显式容忍)。
    • 手动封锁的节点(kubectl cordon)。
    • 处于 NotReadyUnschedulable 状态的节点。

使用新 Web 控制台创建 DaemonSet

新 Web 控制台可以从不同视图暴露 DaemonSet 的创建入口。请根据你是从项目范围还是从具体集群出发,选择相应入口。

  • Applications view:从当前项目开始。打开 Workloads > DaemonSets,选择目标 ClusterNamespace,然后选择会生成 DaemonSet 的工作负载模型或创建流程。
  • Clusters view:从具体集群开始。先打开目标集群,进入目标命名空间,然后导航到 Workloads > DaemonSets,点击 Create DaemonSet

管理 DaemonSets

使用 CLI 管理 DaemonSet

查看 DaemonSet

  • 获取某个命名空间中所有 DaemonSet 的摘要信息。

    kubectl get daemonsets -n <namespace>
  • 获取某个特定 DaemonSet 的详细信息,包括其事件和 Pod 状态。

    kubectl describe daemonset <daemonset-name>

更新 DaemonSet

当你修改 DaemonSet 的 Pod Template 时(例如更改容器镜像或添加卷挂载),Kubernetes 会默认自动执行滚动更新(如果 updateStrategy.typeRollingUpdate,这也是默认值)。

在更改镜像或 OCI Connector 之前,请确认 Connector 代理的 image pull Secret 可通过 Pod 直接访问,或通过其 ServiceAccount 访问。每个目标节点的容器运行时都必须能够访问 Connector 代理地址。更新期间,请检查每个节点上 Pod 的 Connector 注解、重写后的代理镜像、状态和 Events。

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

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

    kubectl rollout status daemonset/<daemonset-name>

删除 DaemonSet

删除 DaemonSet 及其管理的所有 Pod:

kubectl delete daemonset <daemonset-name>

使用 Web 控制台管理 DaemonSet

查看 DaemonSet

  1. Container Platform 中,导航到 Workloads > DaemonSets
  2. 找到你想查看的 DaemonSet。
  3. 单击 DaemonSet 名称,查看 DetailsTopologyLogsEventsMonitoring 等信息。

更新 DaemonSet

  1. Container Platform 中,导航到 Workloads > DaemonSets
  2. 找到你想更新的 DaemonSet。
  3. Actions 下拉菜单中选择 Update,进入 Edit DaemonSet 页面,你可以更新 ReplicasimageupdateStrategy 等。

在更改镜像或 OCI Connector 时,还要检查 Connector 代理的 image pull Secret、工作负载的 ServiceAccount 以及令牌有效性。在滚动更新期间,请在每个目标节点上验证实际的 Pod 镜像和 Events。

删除 DaemonSet

  1. Container Platform 中,导航到 Workloads > DaemonSets
  2. 找到你想删除的 DaemonSet。
  3. Actions 下拉菜单中,点击操作列中的 Delete 按钮并确认。