Sub topic 3
Anatomy of a DaemonSet manifest
This chapter assumes you can read and apply YAML with kubectl. It also assumes you know Pods, labels and selectors, namespaces, and how a Deployment relates to a ReplicaSet. A DaemonSet manifest looks much like a Deployment manifest. The differences are small, and each one matters.
The full example
This DaemonSet runs one log-reading Pod on every node, including control plane nodes. It mounts the node's /var/log read-only.
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-log-agent
namespace: kube-system
spec:
selector:
matchLabels:
app: node-log-agent
template:
metadata:
labels:
app: node-log-agent
spec:
tolerations:
- key: node-role.kubernetes.io/control-plane
operator: Exists
effect: NoSchedule
containers:
- name: agent
image: busybox:1.36
command:
- sh
- -c
- while true; do ls /host-logs | head -n 5; sleep 60; done
resources:
requests:
cpu: 20m
memory: 32Mi
limits:
memory: 64Mi
volumeMounts:
- name: varlog
mountPath: /host-logs
readOnly: true
volumes:
- name: varlog
hostPath:
path: /var/logThe parts of the manifest
The manifest has four areas to read in order.
- Header fields.
apiVersion: apps/v1andkind: DaemonSetselect the API.metadata.nameandmetadata.namespaceidentify the object. Node agents often live inkube-system. spec.selector. It tells the controller which Pods belong to this DaemonSet.spec.template. It is the Pod template. Every Pod the controller creates is built from it.- Optional update settings.
spec.updateStrategycontrols how changes roll out. A later chapter covers it.
Unlike a Deployment, a DaemonSet has no replicas field. The number of Pods follows the number of eligible nodes.
Selector and template labels must match
The selector app: node-log-agent must match the labels in template.metadata.labels. If it doesn't, the API server rejects the manifest. Choose labels that no other workload in the namespace uses. Otherwise the controller can treat another workload's Pods as its own.
The selector is immutable in practice. Changing it means deleting and recreating the DaemonSet, so choose it with care.
The template's restartPolicy must be Always, which is the default. Kubernetes rejects a DaemonSet whose Pods use any other value.
The Pod template
The template is an ordinary Pod spec. This example uses four parts of it.
tolerationslet the Pods run on nodes with matching taints. This one allows scheduling on control plane nodes. Taints are covered in their own chapter.resourcesdeclares a CPU and memory request, plus a memory limit. A node agent shares every node with real workloads, so keep these small and explicit. The request lets the scheduler account for the agent. The limit stops a leak from starving the node.volumeMountsattaches the volume to the container at/host-logs. SettingreadOnly: truemeans the agent can read logs but cannot change them.volumesdefinesvarlogas ahostPathvolume. It exposes the node's/var/logdirectory to the Pod.
A hostPath volume gives a container access to the node's filesystem. Mount only the paths you need, and mount them read-only when you can.
Per-node information
Agents often need to know which node they run on. The downward API can pass the node's IP address to the container as an environment variable.
env:
- name: NODE_IP
valueFrom:
fieldRef:
fieldPath: status.hostIPEach Pod receives the IP of its own node, even though all Pods come from one template.
Applying and inspecting it
Create the DaemonSet and check that it works.
kubectl apply -f node-log-agent.yaml
kubectl get daemonset node-log-agent -n kube-system
kubectl get pods -n kube-system -l app=node-log-agent -o wideThe get daemonset output shows DESIRED, CURRENT, READY, UP-TO-DATE, and AVAILABLE counts. DESIRED equals the number of nodes that should run the Pod. The -o wide output adds a NODE column, so you can confirm one Pod per node.
Exercise
A teammate removes app: node-log-agent from template.metadata.labels but leaves spec.selector unchanged. What happens when they apply the manifest, and why?
Show answer
The API server rejects it. The selector must match the template's labels, because the controller finds its Pods by selector. Without the label, the Pods it creates would not match the selector, so it could not manage them.
Exercise
Why does the DaemonSet manifest have no replicas field, and how do you check how many Pods it expects?
Show answer
The controller creates one Pod per eligible node, so the count follows the node count. Run kubectl get daemonset node-log-agent -n kube-system and read the DESIRED column.