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 资源清单文件编写技巧

cpp 复制代码
[root@k8s-master01 ~]# kubectl explain ds 
字段 作用
apiVersion 当前资源使用的 api 版本,跟 VERSION: apps/v1 保持 一致
kind 资源类型,跟 KIND: DaemonSet 保持一致
metadata 元数据,定义 DaemonSet 名字的
spec 定义容器的
status 状态信息,不能改
cpp 复制代码
[root@k8s-master01 ~]# kubectl  explain ds.spec
字段 作用
minReadySeconds 当新的 pod 启动几秒种后,再 kill 掉旧的 pod。
revisionHistoryLimit 历史版本
selector -required- 用于匹配 pod 的标签选择器
template -required- 定义 Pod 的模板,基于这个模板定义的所有 pod 是一样的
updateStrategy daemonset 的升级策略
cpp 复制代码
[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中的容器存储卷。

清单模版

cpp 复制代码
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 使用案例

cpp 复制代码
[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
相关推荐
阿里云云原生1 小时前
一步到位!阿里云云原生 API 网关,助力 Nginx Ingress 用户实现高效、安全迁移
云原生
todoitbo1 小时前
openEuler 云原生实战:Docker Compose 部署 Nextcloud 企业级私有云
docker·云原生·容器·openeuler
一条懒鱼6662 小时前
K8S-daemonset控制器
云原生·容器·kubernetes
米花町的小侦探3 小时前
Rocky安装k8s
kubernetes
一周困⁸天.3 小时前
K8S-Service资源对象
云原生·容器·kubernetes
weixin_46683 小时前
Docker常用命令与操作
运维·docker·容器
The star"'4 小时前
docker swarm和containerd
运维·docker·容器
❀͜͡傀儡师4 小时前
Docker部署OneTerm堡垒机
运维·docker·容器·oneterm