DaemonSet控制器

1、概述

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

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

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

3、典型应用场景

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

4、DaemonSet 与 Deployment 的区别

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

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

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

1、查看定义Daemonset资源需要的字段

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@xianchaomaster1 ~]# 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> #状态信息,不能改

2、查看DaemonSet的spec字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@xianchaomaster1 ~]# 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的升级策略

6、DaemonSet使用案例:部署日志收集组件fluentd

1、编写配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@master1 daemonset]# cat daemonset.yml 
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd
namespace: kube-system
labels:
k8s-app: fluentd-logging
spec:
selector:
matchLabels:
name: fluentd
updateStrategy:
template:
metadata:
labels:
name: fluentd
spec:
containers:
- name: fluentd
image: xianchao/fluentd:v2.5.1
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 100m
memory: 200Mi
limits:
cpu: 200m
memory: 400Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: datadockercontainers
mountPath: /data/docker/containers
volumes:
- name: varlog
hostPath:
path: /var/log
- name: datadockercontainers
hostPath:
path: /data/docker/containers

2、动态更新与回滚

1