一、DaemonSet控制器:概念、原理解读

1.1、DaemonSet概述

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

1.2、DaemonSet工作原理:如何管理Pod?

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

1.3 、Daemonset典型的应用场景

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

1.4 、DaemonSet 与 Deployment 的区别

Deployment部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本。DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本。

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

2.1、查看定义Daemonset资源需要的字段有哪些?

帮助命令:kubectl explain daemonset

root@k8s-master:~/K8sStudy/Chapter2-13# kubectl explain daemonset |grep -E '^[A-Z]|<*>'
GROUP:      apps
KIND:       DaemonSet
VERSION:    v1
DESCRIPTION:
FIELDS:
  apiVersion    <string>	#当前资源使用的api版本,由GROUP/VERSION组成,即apps/v1
  kind  <string>	#资源类型,跟KIND:  DaemonSet保持一致
  metadata      <ObjectMeta>	#元数据,定义DaemonSet名字的
  spec  <DaemonSetSpec>	#定义容器的
  status        <DaemonSetStatus>	#状态信息,不能改

2.2、查看DaemonSet的spec字段如何定义?

帮助命令:kubectl explain daemonset.spec

root@k8s-master:~/K8sStudy/Chapter2-13# kubectl explain daemonset.spec |grep -E '^[A-Z]|<*>'
GROUP:      apps
KIND:       DaemonSet
VERSION:    v1
FIELD: spec <DaemonSetSpec>
DESCRIPTION:
FIELDS:
  minReadySeconds       <integer>	#当新的pod启动几秒种后,再kill掉旧的pod。
  revisionHistoryLimit  <integer>	#历史版本
  selector      <LabelSelector> -required-	#用于匹配pod的标签选择器
  template      <PodTemplateSpec> -required-	#定义Pod的模板,基于这个模板定义的所有pod是一样的
  updateStrategy        <DaemonSetUpdateStrategy>	#daemonset的升级策略

2.3、查看DaemonSet的spec.template字段如何定义?

对于template而言,其内部定义的就是pod,pod模板是一个独立的对象。

帮助命令:kubectl explain daemonset.spec.template

root@k8s-master:~/K8sStudy/Chapter2-13# kubectl explain daemonset.spec.template |grep -E '^[A-Z]|<*>'
GROUP:      apps
KIND:       DaemonSet
VERSION:    v1
FIELD: template <PodTemplateSpec>
DESCRIPTION:
FIELDS:
  metadata      <ObjectMeta>	# pod元数据
  spec  <PodSpec>		# 定义容器

三、DaemonSet使用案例:部署prometheus主机监控组件node-exporter

在master和node节点上都部署node-exporter监控组件。

查看yaml资源清单文件:Eg-DaemonSet-node-exporter.yaml

cat Eg-DaemonSet-node-exporter.yaml 
apiVersion: apps/v1	#DaemonSet使用的api版本
kind: DaemonSet	# 资源类型
metadata:
  name: prometheus-node-exporter	#资源的名字
  namespace: kube-system	#资源所在的名称空间
  labels:
    k8s-app: prom-node-exporter	#资源具有的标签
spec:
  selector:	#标签选择器
    matchLabels:
      name: node-exporter	# 关联具有该标签的pod
  template:
    metadata:
      labels:	# 基于这个模板定义的pod具有的标签
        name: node-exporter
    spec:
      tolerations:	#定义容忍度
      - key: node-role.kubernetes.io/control-plane	# node上标签的名字
        effect: NoSchedule	# 影响NoSchedule污点
      containers:	#定义容器
      - name: node-exporter
        image: docker.io/prom/node-exporter:latest
        imagePullPolicy: IfNotPresent
        resources:	#资源配额
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
      terminationGracePeriodSeconds: 30	#优雅的关闭服务

应用/更新yaml资源清单文件:Eg-DaemonSet-node-exporter.yaml

root@k8s-master:~/K8sStudy/Chapter2-13# kubectl apply -f Eg-DaemonSet-node-exporter.yaml 
daemonset.apps/prometheus-node-exporter created
root@k8s-master:~/K8sStudy/Chapter2-13# 

查看DaemonSet资源信息:kubectl get daemonset -l k8s-app=prom-node-exporter -n kube-system

k8s控制器DaemonSet_DaemonSet

查看DaemonSet管理的pod:kubectl get pods -l name=node-exporter -n kube-system -owide

k8s控制器DaemonSet_k8s_02

通过上面可以看到在k8s的两个节点均创建了prometheus-node-exporter这个pod。pod的名字是由控制器的名字-随机数组成的


四、Daemonset管理pod:滚动更新

4.1、查看daemonset的滚动更新策略

帮助命令:kubectl explain ds.spec.updateStrategy

root@k8s-master:~/K8sStudy/Chapter2-13# kubectl explain ds.spec.updateStrategy |grep -E '^[A-Z]|<*>'
GROUP:      apps
KIND:       DaemonSet
VERSION:    v1
FIELD: updateStrategy <DaemonSetUpdateStrategy>
DESCRIPTION:
FIELDS:
  rollingUpdate <RollingUpdateDaemonSet>	# 滚动更新
  type  <string>	# 更新方式:"RollingUpdate" or "OnDelete",默认rollingUpdate

4.2、查看rollingUpdate支持的更新策略

帮助命令:kubectl explain ds.spec.updateStrategy.rollingUpdate

root@k8s-master:~/K8sStudy/Chapter2-13# kubectl explain ds.spec.updateStrategy.rollingUpdate |grep -E '^[A-Z]|<*>'
GROUP:      apps
KIND:       DaemonSet
VERSION:    v1
FIELD: rollingUpdate <RollingUpdateDaemonSet>
DESCRIPTION:
FIELDS:
  maxSurge      <IntOrString>		# 和期望ready的副本数比,超过期望副本数最大比例(或最大值),这个值调的越大,副本更新速度越快。
  maxUnavailable        <IntOrString>	# 和期望ready的副本数比,不可用副本数最大比例(或最大值),这个值越小,越能保证服务稳定,更新越平滑

maxUnavailable:和期望ready的副本数比,不可用副本数最大比例(或最大值),这个值越小,越能保证服务稳定,更新越平滑;
maxSurge:和期望ready的副本数比,超过期望副本数最大比例(或最大值),这个值调的越大,副本更新速度越快。

4.3、取值范围

4.3.1、根据数值定义取值范围

maxUnavailable:[0, 副本数]

maxSurge:[0, 副本数]

注意:两者不能同时为0。

4.3.2、根据比例定义取值范围

maxUnavailable:[0%, 100%] 向下取整,比如10个副本,5%的话==0.5个,但计算按照0个;

maxSurge:[0%, 100%] 向上取整,比如10个副本,5%的话==0.5个,但计算按照1个;

注意:两者不能同时为0。

4.3.3、生产建议配置

maxUnavailable == 0

maxSurge == 1

生产环境建议使用的配置。即“一上一下,先上后下”最平滑原则:1个新版本pod ready(结合readiness)后,才销毁旧版本pod。此配置适用场景是平滑更新、保证服务平稳,但也有缺点,就是“太慢”了。