12.DaemonSet控制器

1.DaemonSet概述

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

2.DaemonSet工作原理

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

3.Daemonset 典型的应用场景

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

4.DaemonSet 与 Deployment 的区别

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

5.DaemonSet 资源清单

[root@master1 ~]# kubectl explain ds
KIND: DaemonSet
VERSION: apps/v1
DESCRIPTION:
 DaemonSet represents the configuration of a daemon set.
FIELDS:
 apiVersion <string> #当前资源使用的 api 版本,跟 VERSION: apps/v1 保持一致
 kind <string> #资源类型,跟 KIND: DaemonSet 保持一致
 metadata <Object> #元数据,定义 DaemonSet 名字的
 spec <Object> #定义容器的
 status <Object> #状态信息,不能改

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

[root@master1 ~]# kubectl explain ds.spec
KIND: DaemonSet
VERSION: apps/v1
RESOURCE: spec <Object>
DESCRIPTION:
 The desired behavior of this daemon set. More info:
 https://git.k8s.io/community/contributors/devel/sig￾architecture/api-conventions.md#spec-and-status
 DaemonSetSpec is the specification of a daemon set.
FIELDS:
 minReadySeconds <integer> #当新的 pod 启动几秒种后,再 kill 掉旧的pod。
 revisionHistoryLimit <integer> #历史版本
 selector <Object> -required- #用于匹配 pod 的标签选择器
 template <Object> -required- #定义 Pod 的模板,基于这个模板定义的所有 pod 是一样的
 updateStrategy <Object> #daemonset 的升级策略

查看 DaemonSet 的 spec.template 字段如何定义? #对于 template 而言,其内部定义的就是 pod,pod 模板是一个独立的对象

[root@master1 ~]# kubectl explain ds.spec.template
KIND: DaemonSet
VERSION: apps/v1
RESOURCE: template <Object>
FIELDS:
 metadata <Object>
 spec <Object>

DaemonSet 使用案例:部署日志收集组件 fluentd
daemonset.yaml

apiVersion: apps/v1
kind: DaemonSet
metadata:
 name: fluentd-elasticsearch
 namespace: kube-system
 labels:
   k8s-app: fluentd-logging
spec:
 selector:
   matchLabels:
     name: fluentd-elasticsearch
 template:
   metadata:
     labels:
       name: fluentd-elasticsearch
   spec:
     tolerations:
       - key: node-role.kubernetes.io/master
     effect: NoSchedule
   ontainers:
   - name: fluentd-elasticsearch
     image: /fluentd:v2.5.1
   resources:
     limits:
       memory: 200Mi
       requests:
       cpu: 100m
       memory: 200Mi
   volumeMounts:
   - name: varlog
     mountPath: /var/log
   - name: varlibdockercontainers
     mountPath: /var/lib/docker/containers
     readOnly: true terminationGracePeriodSeconds: 30
 volumes:
 - name: varlog
   hostPath:
   path: /var/log
 - name: varlibdockercontainers
   hostPath:
   path: /var/lib/docker/containers
[root@master1 ~]# kubectl apply -f daemonset.yaml
daemonset.apps/fluentd-elasticsearch created
[root@master1 ~]# kubectl get ds -n kube-system
NAME                 DESIRED     CURRENT READY UP-TO-DATE AVAILABLE
fluentd-elasticsearch 3           3        3      3          3
[root@master1 ~]# kubectl get pods -n kube-system -o wide

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

资源清单详细说明

apiVersion: apps/v1 #DaemonSet 使用的 api 版本
kind: DaemonSet # 资源类型
metadata:
 name: fluentd-elasticsearch #资源的名字
 namespace: kube-system #资源所在的名称空间
 labels:
 k8s-app: fluentd-logging #资源具有的标签
spec:
 selector: #标签选择器
 matchLabels:
 name: fluentd-elasticsearch
 template:
 metadata:
 labels: #基于这回模板定义的 pod 具有的标签
 name: fluentd-elasticsearch
 spec:
 tolerations: #定义容忍度
 - key: node-role.kubernetes.io/master
 effect: NoSchedule
 containers: #定义容器
 - name: fluentd-elasticsearch
 image: /fluentd:v2.5.1
 resources: #资源配额
 limits:
 memory: 200Mi
 requests:
 cpu: 100m
 memory: 200Mi
 volumeMounts:
 - name: varlog
 mountPath: /var/log #把本地/var/log 目录挂载到容器
 - name: varlibdockercontainers
 mountPath: /var/lib/docker/containers #把/var/lib/docker/containers/挂载到容器里
 readOnly: true #挂载目录是只读权限
 terminationGracePeriodSeconds: 30 #优雅的关闭服务
 volumes:
 - name: varlog
 hostPath:
 path: /var/log #基于本地目录创建一个卷
 - name: varlibdockercontainers
 hostPath:
 path: /var/lib/docker/containers #基于本地目录创建一个卷 

滚动更新

查看 daemonset 的滚动更新策略

[root@master1 ~]# kubectl explain ds.spec.updateStrategy
KIND: DaemonSet
VERSION: apps/v1
RESOURCE: updateStrategy <Object>
DESCRIPTION:
 An update strategy to replace existing DaemonSet pods with new pods.
 DaemonSetUpdateStrategy is a struct used to control the update strategy for a DaemonSet.
FIELDS:
 rollingUpdate <Object>
 	Rolling update config params. Present only if type ="RollingUpdate".
 type <string>
 	Type of daemon set update. Can be "RollingUpdate" or "OnDelete".Default is RollingUpdate.

查看 rollingUpdate 支持的更新策略

[root@master1 ~]# kubectl explain ds.spec.updateStrategy.rollingUpdate
KIND: DaemonSet
VERSION: apps/v1
RESOURCE: rollingUpdate <Object>
DESCRIPTION:
FIELDS:
 maxUnavailable <string>

上面表示 rollingUpdate 更新策略只支持 maxUnavailabe,先删除在更新;因为我们不支持一个节点运行两个 pod,因此需要先删除一个,在更新一个。

更新镜像版本,可以按照如下方法:
kubectl set image daemonsets fluentd-elasticsearch=ikubernetes/filebeat:5.6.6-alpine -n kube-system

posted @ 2022-03-07 21:06  ForLivetoLearn  阅读(42)  评论(0编辑  收藏  举报