守护进程集

理解 DaemonSets

请参阅 Kubernetes 官方文档:DaemonSets

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

WARNING

DaemonSet 运行说明

  1. 行为特征

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

      • 为每个满足以下条件的可调度节点恰好部署一个 Pod 副本:
        • 匹配 nodeSelector 或 nodeAffinity 条件(如果已指定)。
        • 不处于 NotReady 状态。
        • 没有 NoSchedule 或 NoExecute Taints,除非在 Pod Template 中配置了相应的 Tolerations。
    • Pod 数量公式:DaemonSet 管理的 Pod 数量 等于 符合条件的 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 console 创建 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 console 中集成的镜像仓库镜像时,可以通过 Already Integrated 进行筛选。Integration Project Name,例如镜像(registry-projectname),其中在此 web console 中包含项目名称 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:处于 Pending 或 CrashLoopBackOff 等状态的 Pod 视为不可用。
  • 同时限制:maxSurge 和 maxUnavailable 不能同时为 0 或 0%。如果百分比值换算后两个参数都为 0,Kubernetes 会强制设置 maxUnavailable=1 以确保更新能够继续进行。

操作步骤 - 配置 Pod

Pod 部分请参阅 Deployment - 配置 Pod

操作步骤 - 配置容器

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

操作步骤 - 创建

点击 Create。

点击 Create 后,DaemonSet 将:

  • ✅ 自动将 Pod 副本部署到所有符合条件的节点,这些节点满足:

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

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

使用新 web console 创建 DaemonSet

新的 web console 可以从不同视图暴露 DaemonSet 创建入口。根据你是从项目范围还是从具体集群进行操作,选择相应入口。

  • Applications 视图:从当前项目开始。打开 Workloads > DaemonSets,选择目标 Cluster 和 Namespace,然后选择会生成 DaemonSet 的工作负载模型或创建流程。
  • Clusters 视图:从具体集群开始。先打开目标集群,进入目标命名空间,然后导航到 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.type 为 RollingUpdate,这是默认值)。

在更改镜像或 OCI Connector 之前,请确认 Connector 代理的 image pull Secret 可以直接供 Pod 使用,或可通过其 ServiceAccount 使用。目标节点上的容器运行时必须能够访问 Connector 代理地址。在更新过程中,请检查每个节点上 Pod 的 Connector annotation、重写后的代理镜像、状态以及 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 console 管理 DaemonSet

查看 DaemonSet

  1. 进入 Container Platform,然后导航到 Workloads > DaemonSets。
  2. 找到要查看的 DaemonSet。
  3. 点击 DaemonSet 名称查看 Details、Topology、Logs、Events、Monitoring 等信息。

更新 DaemonSet

  1. 进入 Container Platform,然后导航到 Workloads > DaemonSets。
  2. 找到要更新的 DaemonSet。
  3. 在 Actions 下拉菜单中选择 Update,进入 Edit DaemonSet 页面,你可以更新 Replicas、image、updateStrategy 等。

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

删除 DaemonSet

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