概述
DaemonSet 确保全部(或者某些)节点上只运行一个 Pod 的副本。 当有节点加入集群时, 也会为他们新增一个 Pod 。 当有节点从集群移除时,这些 Pod 也会被回收。删除 DaemonSet 将会删除它创建的所有 Pod。
应用场景
DaemonSet 的典型应用场景有:
- 集群存储守护程序,如 glusterd、ceph 要部署在每个节点上以提供持久性存储;
- 节点监控守护进程,如 Prometheus 监控集群,可以在每个节点上运行一个 node-exporter 进程来收集监控节点的信息;
- 日志收集守护程序,如 fluentd 或 logstash,在每个节点上运行以收集容器的日志
- 节点网络插件,比如 flannel、calico,在每个节点上运行为 Pod 提供网络服务。
HelloWorld
创建nginx-ds.yaml ```yaml apiVersion: apps/v1 kind: DaemonSet metadata: name: nginx-ds namespace: default labels: app: nginx-ds spec: selector: matchLabels: app: nginx template: metadata: labels:
spec: containers:app: nginx
- name: nginx
image: nginx:1.14
resources:
requests:
volumeMounts:cpu: 100mmemory: 200Mi
- name: localtime mountPath: /etc/localtime terminationGracePeriodSeconds: 60 volumes:
- name: localtime hostPath: path: /usr/share/zoneinfo/Asia/Shanghai
执行```shell$ kubectl apply -f nginx-ds.yamldaemonset.apps/nginx-ds created$ kubectl get pod -l app=nginx -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-ds-kjv22 1/1 Running 0 2m4s 100.111.156.80 k8s-node1 <none> <none>nginx-ds-x65np 1/1 Running 0 2m4s 100.116.59.92 k8s-master <none> <none>
DaemonSet用于在每个 Kubernetes 节点中将守护进程的副本作为后台进程运行,也就是在每个node上部署一个 Pod副本,上面的例子中把master上的污点去掉了 所以也可以部署pod。
下面的 daemonset.yaml 文件描述了一个运行 fluentd-elasticsearch Docker 镜像的 DaemonSet:
apiVersion: apps/v1kind: DaemonSetmetadata:name: fluentd-elasticsearchnamespace: kube-systemlabels:k8s-app: fluentd-loggingspec:selector:matchLabels:name: fluentd-elasticsearchtemplate:metadata:labels:name: fluentd-elasticsearchspec:tolerations:# this toleration is to have the daemonset runnable on master nodes# remove it if your masters can't run pods- key: node-role.kubernetes.io/mastereffect: NoSchedulecontainers:- name: fluentd-elasticsearchimage: quay.io/fluentd_elasticsearch/fluentd:v2.5.2resources:limits:memory: 200Mirequests:cpu: 100mmemory: 200MivolumeMounts:- name: varlogmountPath: /var/log- name: varlibdockercontainersmountPath: /var/lib/docker/containersreadOnly: trueterminationGracePeriodSeconds: 30volumes:- name: varloghostPath:path: /var/log- name: varlibdockercontainershostPath:path: /var/lib/docker/containers
基于 YAML 文件创建 DaemonSet:
kubectl apply -f https://k8s.io/examples/controllers/daemonset.yaml
参数配置
必需字段
- DaemonSet 需要 apiVersion、kind 和 metadata 字段
- DaemonSet 对象的名称必须是一个合法的 DNS 子域名。
-
Pod 模板
.spec中唯一必需的字段是.spec.template。
.spec.template 是一个 Pod 模板。 除了它是嵌套的,因而不具有 apiVersion 或 kind 字段之外,它与 Pod 具有相同的 schema。
除了 Pod 必需字段外,在 DaemonSet 中的 Pod 模板必须指定合理的标签(查看 Pod 选择算符)。
在 DaemonSet 中的 Pod 模板必须具有一个值为Always的RestartPolicy。 当该值未指定时,默认是 Always。Pod 选择算符
.spec.selector字段表示 Pod 选择算符,它与 Job 的 .spec.selector 的作用是相同的。
从Kubernetes 1.8开始,您必须指定与 .spec.template 的标签匹配的 Pod 选择算符。 用户不指定 Pod 选择算符时,该字段不再有默认值。 选择算符的默认值生成结果与 kubectl apply 不兼容。 此外,一旦创建了 DaemonSet,它的 .spec.selector 就不能修改。 修改 Pod 选择算符可能导致 Pod 意外悬浮,并且这对用户来说是费解的。
spec.selector 是一个对象,如下两个字段组成: matchLabels- 与ReplicationController的 .spec.selector 的作用相同。matchExpressions- 允许构建更加复杂的选择器,可以通过指定 key、value 列表以及将 key 和 value列表关联起来的 operator。
当上述两个字段都指定时,结果会按逻辑与(AND)操作处理。
如果指定了 **.spec.selector**,必须与 **.spec.template.metadata.labels** 相匹配。 如果与后者不匹配,则 DeamonSet 会被 API 拒绝。
仅在某些节点上运行 Pod
如果指定了 .spec.template.spec.nodeSelector,DaemonSet 控制器将在能够与 Node 选择算符 匹配的节点上创建 Pod。 类似这种情况,可以指定 .spec.template.spec.affinity,之后 DaemonSet 控制器 将在能够与节点亲和性 匹配的节点上创建 Pod。 如果根本就没有指定,则 DaemonSet Controller 将在所有节点上创建 Pod。
