K8S-daemonset控制器

一、 DaemonSet 概述

DaemonSet 控制器能够确保 k8s 集群所有的节点都运行一个相同的 pod 副本,当向 k8s 集群中增加 node 节点时,这个 node 节点也会自动创建一个 pod 副本,当 node 节点从 集群移除,这些 pod 也会自动删除;删除 Daemonset 也会删除它们创建的 pod

DaemonSet 工作原理

daemonset 的控制器会监听 kuberntes 的 daemonset 对象、pod 对象、node 对象,这些被监听的对象之变动,就会触发 syncLoop 循环让 kubernetes 集群朝着 daemonset 对象描述的状态进行演进。

Daemonset 典型的应用场景

在集群的每个节点上运行存储,比如:glusterd 或 ceph。 在每个节点上运行日志收集组件,比如:flunentd 、 logstash、filebeat 等。 在每个节点上运行监控组件,比如:Prometheus、 Node Exporter 、collectd 等。

DaemonSet 与 Deployment 的区别

Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本。

DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本。

二、DaemonSet 资源清单文件编写技巧

root@k8s-master01 \~\]# kubectl explain ds

字段 作用
apiVersion 当前资源使用的 api 版本,跟 VERSION: apps/v1 保持 一致
kind 资源类型,跟 KIND: DaemonSet 保持一致
metadata 元数据,定义 DaemonSet 名字的
spec 定义容器的
status 状态信息,不能改

root@k8s-master01 \~\]# kubectl explain ds.spec

字段 作用
minReadySeconds 当新的 pod 启动几秒种后,再 kill 掉旧的 pod。
revisionHistoryLimit 历史版本
selector -required- 用于匹配 pod 的标签选择器
template -required- 定义 Pod 的模板,基于这个模板定义的所有 pod 是一样的
updateStrategy daemonset 的升级策略

root@k8s-master01 \~\]# kubectl explain ds.spec.template

字段 作用
containers 容器配置列表。每个元素都描述了一个要运行在Pod内的容器。
initContainers 化容器配置列表。这些容器在主容器之前运行,用于设置环境或执行预备工作。
ephemeralContainers 临时容器配置列表。这些容器是临时的,仅在Pod运行时存在。
restartPolicy Pod的重启策略。可以是Always、OnFailure或Never。
terminationGracePeriodSeconds 优雅终止周期,以秒为单位。在强制终止Pod前,系统将等待此周期内的终止。
activeDeadlineSeconds Pod活动的截止时间,超过此时间系统将杀死Pod。
dnsPolicy Pod的DNS策略,可以是ClusterFirst、ClusterFirstWithHostNet、Default或None。
Context Pod的安全上下文,用于设置Pod的安全相关属性,如RBAC规则、SELinux标签等。
schedulerName 调度器名称,用于指定用于调度Pod的特定调度器。
tolerations Pod容忍的污点,用于允许Pod调度到有特定污点的节点上。
affinity Pod的亲和性设置,用于指定Pod偏好或必须运行的节点属性。
hostNetwork 如果设置为true,P将使用主机的网络命名空间。
imagePullSecrets 用于获取镜像的密钥,这些密钥会被注入到Pod中。
configMap ConfigMap的名称和键值对列表,用于注入配置数据到Pod
secret Secret的名称和键值对列表,用于注入敏感数据到Pod。
volumeMounts 卷挂载配置列表,用于指定Pod中容器如何挂载卷。
volumes 卷配置列表,为Pod中的容器存储卷。

清单模版

apiVersion: apps/v1

kind: DaemonSet

metadata:

name: pod-controller # ds名称

labels: # 给ds打标签

controller: daemonset

spec:

revisionHistoryLimit: 3 # 保留历史版本数量,默认为10

updateStrategy: # Pod更新策略,默认是RollingUpdate

type: RollingUpdate # 滚动更新策略。另一种是OnDelete,其没有子属性配置参数

rollingUpdate: # 当type为RollingUpdate的时候生效,为其配置参数

maxSurge: 25% # 升级过程中可以超过期望的Pod的最大数量,可以为百分比,也可以为整数。默认是25%

maxUnavailable: 25% # 升级过程中最大不可用状态的Pod数量,可以为百分比,也可以为整数。默认是25%

selector: # 选择器,通过该控制器管理哪些pod

matchLabels: # Labels匹配规则。和matchExpressions类似

app: nginx-pod ###或者

matchExpressions: # Expressions匹配规则。和matchLabels类似

  • {key: app, operator: 'In', values: ["nginx-pod"]}

template: # pod副本创建模板。属性和Pod的属性一样

metadata:

labels:

app: nginx-pod

spec:

containers:

  • name: nginx

image: nginx:latest

ports:

  • name: nginx-port

containerPort: 80

protocol: TCP

三、DaemonSet 使用案例

root@k8s-master01 \~\]# cat pod-controller.yaml apiVersion: apps/v1 kind: DaemonSet metadata: name: pod-controller labels: controller: daemonset spec: selector: matchLabels: app: nginx-pod template: metadata: labels: app: nginx-pod spec: containers: - name: nginx image: nginx:latest ports: - name: nginx-port containerPort: 80 protocol: TCP ##查看 \[root@k8s-master01 \~\]# kubectl apply -f pod-controller.yaml daemonset.apps/pod-controller created

相关推荐
蝎子莱莱爱打怪11 小时前
GitLab CI/CD + Docker Registry + K8s 部署完整实战指南
后端·docker·kubernetes
阿里云云原生4 天前
阿里云获评 Agentic AI 开发平台领导者,函数计算 AgentRun 赢下关键分!
云原生
蝎子莱莱爱打怪4 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
崔小汤呀4 天前
Docker部署Nacos
docker·容器
缓解AI焦虑4 天前
Docker + K8s 部署大模型推理服务:资源划分与多实例调度
docker·容器
阿里云云原生4 天前
MSE Nacos Prompt 管理:让 AI Agent 的核心配置真正可治理
微服务·云原生
阿里云云原生4 天前
当 AI Agent 接管手机:移动端如何进行观测
云原生·agent
阿里云云原生4 天前
AI 原生应用开源开发者沙龙·深圳站精彩回顾 & PPT下载
云原生
阿里云云原生4 天前
灵感启发:日产文章 100 篇,打造“实时热点洞察”引擎
云原生
1candobetter4 天前
Docker Compose Build 与 Up 的区别:什么时候必须重建镜像
docker·容器·eureka