在所有的节点上都启动一个 DaemonSet
单独对每种 daemon 类型使用多个 DaemonSet,但具有不同的标志,并且对不同硬件类型具有不同的内存、CPU要求。
vi /root/test2/daemonset-nginx.yaml
内容
apiVersion: apps/v1
kind: DaemonSet
metadata:
# DaemonSet的名称
name: daemonset-nginx
# 命名空间
namespace: kube-system
labels:
# DaemonSet 标签
la-nginx: daemonset-nginx
spec:
# 标签选择器
selector:
matchLabels:
# 使用 k2-nginx: daemonset-nginx2 标签的template
k2-nginx: daemonset-nginx2
# 定义模板
template:
metadata:
labels:
# 定义标签
k2-nginx: daemonset-nginx2
spec:
# 定义容忍度,容忍哪些污点
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
# 定义容器
containers:
- name: c-nginx
image: nginx
# 定义资源
resources:
# 最大CPU和内存
limits:
cpu: 200m
memory: 200Mi
# 最小CPU和内存
requests:
cpu: 100m
memory: 100Mi
volumeMounts:
# 引入名称为nginx-volume1的存储定义
- name: nginx-volume1
# 容器存储卷目录
mountPath: /usr/share/nginx/html
- name: nginx-volume2
mountPath: /usr/share/nginx/conf
readOnly: true
# pod中断所需的时间
terminationGracePeriodSeconds: 30
volumes:
# 存储卷名称
- name: nginx-volume1
# 存储卷类型
hostPath:
# 节点的目录
path: /data/nginx-volume1
- name: nginx-volume2
hostPath:
path: /data/nginx-volume2
kubectl apply -f /root/test2/daemonset-nginx.yaml
# 查看
kubectl -n kube-system get pods -o wide

DaemonSet只在某个节点上创建Pod

apiVersion: apps/v1
kind: DaemonSet
metadata:
# DaemonSet的名称
name: daemonset-nginx
# 命名空间
namespace: kube-system
labels:
# DaemonSet 标签
la-nginx: daemonset-nginx
spec:
# 标签选择器
selector:
# 匹配拥有哪些标签的pod
matchLabels:
# 使用 k2-nginx: daemonset-nginx2 标签的template
k2-nginx: daemonset-nginx2
# 定义模板
template:
metadata:
labels:
# 定义标签
k2-nginx: daemonset-nginx2
spec:
# DaemonSet将在node1节点上创建Pod
nodeName: node1
# 定义容忍度,容忍哪些污点
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
# 定义容器
containers:
- name: c-nginx
image: nginx

DaemonSet控制器将在与该节点关联相匹配的节点上创建Pod。
kubectl explain daemonset.spec.template.spec.affinity
DaemonSets方法是将NodeAffinity添加到DaemonSet pods,而不是.spec.nodeName。 然后使用默认调度器将pod绑定到目标主机。 如果DaemonSet pod的亲和节点已存在,则替换它。
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchFields:
- key: metadata.name
operator: In
values:
- target-host-name
尽管Daemon Pods遵循污点和容忍度规则,根据相关特性,会自动将以下容忍度添加到 DaemonSet Pods 中。

与DaemonSet中的Pod进行通信的几种可能模式如下
将DaemonSet中的Pod配置为将更新发送到其他服务,例如统计数据库。
创建具有相同PodSelector的 Headless Service,然后通过使用 endpoints 资源或从 DNS 中检索到多个 A 记录来发现 DaemonSet。
创建具有相同PodSelector 的 Service,并使用该 Service 随机访问到某个节点上的DaemonSet pod(没有办法访问到特定节点)。
我们很可能希望直接在一个节点上启动daemon(守护)进程(例如,使用 init、upstartd、systemd)。这非常好,但基于DaemonSet来运行这些进程有如下一些好处
无状态的Sevice使用Deployments,需要实现对副本的数量进行扩缩容、平滑升级,就用Deployments
需要Pod副本总是运行在全部或特定主机上,并需要先于其他Pod启动,就用daemonset。