CodeBlanks Logo Icon CodeBlanks

DaemonSet YAML

Learn the DaemonSet YAML structure.

DaemonSet YAML

The DaemonSet YAML is a Deployment without replicas: a selector and a pod template are enough:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: izleme
spec:
  selector:
    matchLabels:
      app: node-exporter
  template:
    metadata:
      labels:
        app: node-exporter
    spec:
      containers:
        - name: exporter
          image: prom/node-exporter:v1.8.2
          ports:
            - containerPort: 9100

The node-exporter in this example exposes each node's CPU, memory, disk and network metrics to Prometheus.

Access to the node

DaemonSets doing node-level work usually read the node's file system with hostPath, or use the node's network (hostNetwork: true). These are powerful privileges; they should only be given when needed and read-only:

    spec:
      hostNetwork: true
      volumes:
        - name: proc
          hostPath:
            path: /proc
      containers:
        - name: exporter
          volumeMounts:
            - name: proc
              mountPath: /host/proc
              readOnly: true

Practice Write a DaemonSet

Write a DaemonSet YAML: name node-exporter, namespace izleme; selector and template label app: node-exporter; container named exporter, image prom/node-exporter:v1.8.2, containerPort: 9100.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: izleme
spec:
  selector:
    matchLabels:
      app: node-exporter
  template:
    metadata:
      labels:
        app: node-exporter
    spec:
      containers:
        - name: exporter
          image: prom/node-exporter:v1.8.2
          ports:
            - containerPort: 9100

Expected output

daemonset.apps/node-exporter created

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What happens if replicas: 3 is written in a DaemonSet YAML?

  • 3 pods run on each node
  • It is ignored
  • It is rejected as an invalid field Correct answer
  • It runs on 3 nodes

Explanation: There is no replicas field in a DaemonSet spec.

2 Mount the node's directory as a volume.

Question
      volumes:
        - name: proc
          ____:
            path: /proc
Answer
      volumes:
        - name: proc
          hostPath:
            path: /proc

Explanation: hostPath mounts a path on the node into the pod.

3 Match.

Term Meaning
kind: DaemonSet apps/v1
hostPath Node file system
hostNetwork Node network
node-exporter Node metrics

All lessons in Advanced