DaemonSet的主要作用是让你在Kubernetes中运行一个Daemon Pod,这个Pod有如下三个特征:

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
      containers:
      - name: fluentd-elasticsearch
       image: quay.io/fluentd_elasticsearch/fluentd:v3.0.0
	  resorces:
	    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

DaemonSet是通过控制器模型来保证其特征的,简单来说,DaemonSet Controller会首先从etcd里获取所有的节点列表,然后便利所有节点,如果没有这种Pod,就创建一个,有但是多于1个,就删除

具体的做法是在创建Pod时,自动在这个Pod的API对象里加上一个nodeAffinity定义(nodeAffinity出现是为了替代nodeSelector),这个字段的用法如下:

apiVersion: v1
kind: Pod
metadata:
  name: with-node-affinity
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIngnoreDuringExecution:
        nodeSelectorTerms:
          - key: metadata.name
            operator: In
            values:
            - node-ituring

同时,DaemonSet Controller还会给Pod上一个tolerations字段,使其可以容忍被标记为unschedulable的节点,允许其调度到本不允许任何Pod调度上去的节点上