文章目录
一、DaemonSet概述
DaemonSet(守护进程集)和守护进程类似,它在符合匹配条件的节点上均部署一个Pod。DaemonSet确保全部(或者某些)节点上运行一个Pod副本。当有新节点加入集群时,也会为它们新增一个Pod。当节点从集群中移除时,这些Pod也会被回收,删除DaemonSet将会删除它创建的所有Pod。
二、DaemonSet工作原理
Daemonset的控制器会监听kuberntes的Daemonset 对象、pod 对象、node 对象,这些被监听的对象之变动,就会触发syncLoop循环让kubernetes集群朝着Daemonset对象描述的状态进行演进。
三、Daemonset应用场景
1、在集群的每个节点上运行存储,比如:glusterd 或 ceph。
2、在每个节点上运行日志收集组件,比如:flunentd 、 logstash、filebeat 等。
3、在每个节点上运行监控组件,比如:Prometheus、 Node Exporter 、collectd 等。
四、DaemonSet与Deployment区别
Deployment部署的副本Pod会分布在各个Node上,每个Node都可能运行一个或多个副本;而DaemonSet的不同之处在于每个Node上最多只能运行一个副本。
五、DaemonSet资源清单文件编写技巧
#查看定义Daemonset资源需要的字段
[root@k8s-client-17 yaml]# 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> #元数据信息
spec <Object> #定义容器信息
status <Object> #状态信息,不能修改
#查看DaemonSet的spec字段
[root@k8s-client-17 yaml]# 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模板
updateStrategy <Object> #升级策略
六、Daemonset使用案例
部署日志收集组件fluentd,编写一个Daemonset资源清单,如下图所示:
fluentd日志收集组件yaml文件Daemonset资源清单
执行apply,部署fluentd,查看pod情况
[root@k8s-client-17 yaml]# kubectl apply -f ds-fluentd.yaml
如下图所示:
说明:通过观察发现,无论是在master节点和worker节点均只有一个pod在运行。
七、Daemonset实现pod滚动更新
Daemonset更新策略,默认为rollingUpdate
rollingUpdate更新策略只支持maxUnavailabe,先删除在更新;因为ds资源类型的pod不支持一个节点运行两个pod,因此需要先删除一个,在更新一个。
现在需要fluentd-v3.1.0升级到fluentd-v3.2.0,更新方法如下:
方法一:
修改yaml资源清单文件,将image: quay.io/fluentd_elasticsearch/fluentd:v3.1.0修改为image: quay.io/fluentd_elasticsearch/fluentd:v3.2.0,然后执行kubectl apply -f ds-fluentd.yaml
方法二:通过kubeclt set image的方法进行滚动更新,如下图所示:
如果对其命令不熟,可以使用kubectl set image --help的方式查看帮助信息。如下图所示:
总结:整理不易,如果对你有帮助,可否点赞关注一下?
更多详细内容请参考:企业级K8s集群运维实战