What is a DaemonSet ?
A DaemonSet is a resource that ensures that all (or some) Nodes run a copy of a Pod.
As nodes are added to the cluster, Pods are added to them.
As nodes are removed from the cluster, those Pods are garbage collected.
Deleting a DaemonSet will clean up the Pods it created.
Some typical uses of a DaemonSet are:
– running a cluster storage daemon on every node
– running a logs collection daemon on every node (ex: fluent, filebeat)
– running a node monitoring daemon on every node
Simple case : one DaemonSet, covering all nodes
More complex cases : use multiple DaemonSets for a single type of daemon, but with different flags and/or different memory and cpu requests for different hardware types.
Creating a daemonset
Required Fields
As with all other Kubernetes config, it requires : apiVersion, kind, and metadata fields.
It also requires a .spec.template and a .spec.selector section.
The .spec.template is a pod template. It has exactly the same schema as a Pod, except it is nested and does not have an apiVersion or kind.
In addition to required fields for a Pod, a Pod template in a DaemonSet has to specify appropriate labels (see pod selector below).
A Pod Template in a DaemonSet must have a RestartPolicy equal to Always, or be unspecified, which defaults to Always
Pod Selector
.spec.selector
must match the .spec.template.metadata.labels
.
Config with these not matching will be rejected by the API
The .spec.selector
consists of two fields and at least one of them has to be valued :
– matchLabels – works the same as the .spec.selector of a ReplicationController.
– matchExpressions – allows to build more sophisticated selectors by specifying key, list of values and an operator that relates the key and values.
When the two are specified the result is ANDed.
Examples
Here is a fluentd daemonset example:
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: containers: - name: fluentd-elasticsearch image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2 resources: 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 |