✦ For everyone, free.

Practical knowledge for real and everyday life

Home

Kubernetes DaemonSet Pod Template Management

Kubernetes DaemonSet Pod Template Management defines how pods run across nodes, ensuring consistent and scalable containerized application deployment.

Kubernetes DaemonSet Pod Template Management is the practice of designing and maintaining the template block within an apps/v1 DaemonSet specification — the definition stamped onto every node the DaemonSet targets. Because a single Pod template must run correctly and safely on every eligible node in a cluster, potentially spanning heterogeneous hardware, operating systems, and privilege boundaries, the design considerations for a DaemonSet's Pod template differ meaningfully from those of a Deployment's, where every replica typically runs in a more uniform, interchangeable environment.

The template shares the same underlying PodTemplateSpec shape used by every other Pod-based controller, but the practices around sizing, privilege, host access, and node-awareness that apply to it are shaped specifically by the fact that this one template must work correctly across the entire, potentially varied, fleet of nodes it targets.


Host-Level Access Patterns

hostPath Volumes

Many daemon workloads (log collectors, node monitoring agents) need to read files that live on the node's filesystem rather than in a container-local volume — container log files, /proc, /sys, or a container runtime's socket. This is accomplished via hostPath volumes mounted read-only wherever possible, since a daemon writing carelessly to shared host paths risks affecting the node itself or other workloads scheduled there.

volumes:
  - name: varlog
    hostPath:
      path: /var/log
      type: Directory
volumeMounts:
  - name: varlog
    mountPath: /var/log
    readOnly: true

hostNetwork and hostPID

Some daemons (network plugins, certain security monitoring agents) require hostNetwork: true to observe or configure the node's own network namespace directly, or hostPID: true to observe processes across the whole node rather than just within their own Pod's process namespace. Both settings substantially widen the Pod's access to the underlying node and should be applied only when the daemon's function genuinely requires it, since they meaningfully increase the security surface of the workload.

Privileged Containers and Capabilities

Rather than reaching for full privileged: true by default, daemon templates should scope permissions to the minimum required Linux capabilities (NET_ADMIN, SYS_PTRACE, and similar) via securityContext.capabilities.add, reserving full privileged mode for daemons that genuinely need broad kernel-level access (certain CNI or storage plugins), since a compromised privileged daemon Pod has a much larger blast radius across every node it runs on.

securityContext:
  capabilities:
    add: ["NET_ADMIN"]
  allowPrivilegeEscalation: false

Resource Sizing for Fleet-Wide Pods

Conservative, Consistent Requests and Limits

Because a DaemonSet's Pods run on every eligible node — competing for capacity with whatever else is scheduled there — resource requests should be sized conservatively and consistently, since an oversized request across hundreds of nodes multiplies into a substantial, cluster-wide capacity reservation that reduces room for application workloads.

resources:
  requests:
    cpu: "50m"
    memory: "64Mi"
  limits:
    cpu: "100m"
    memory: "128Mi"

Accounting for Heterogeneous Nodes

A template that assumes uniform node capacity can behave inconsistently across a cluster mixing small and large instance types; daemons with meaningfully variable resource needs based on node size sometimes use a VerticalPodAutoscaler in recommendation mode, or environment variables computed from resources.limits via the downward API, to adjust internal buffer sizes or worker counts based on the actual node's allocatable resources.


Node-Awareness Inside the Container

Injecting Node Identity

Daemon containers often need to know which node they are running on, both for logging/metrics tagging and for behavior that depends on node identity (a device plugin needing the node's hardware inventory). This is exposed via the downward API rather than any daemon-specific mechanism:

env:
  - name: NODE_NAME
    valueFrom:
      fieldRef:
        fieldPath: spec.nodeName

Tolerations for Full Coverage

Since the default scheduler excludes tainted nodes, daemon templates intended to cover every node — including control-plane nodes — must explicitly add matching tolerations; omitting them silently produces gaps in coverage on tainted nodes rather than an obvious error.

tolerations:
  - operator: "Exists"
    effect: "NoSchedule"
  - operator: "Exists"
    effect: "NoExecute"

Immutability and Update Propagation

Like Deployments, a DaemonSet's Pod template can be updated after creation, and the controller propagates the change to every node's Pod according to spec.updateStrategy — but because a DaemonSet's template change affects every single node, template edits (especially to resource limits or image versions) are typically staged and tested on a labeled subset of nodes first, when the daemon's own nodeSelector scoping allows for it.


Example

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: codartium-template-example
spec:
  selector:
    matchLabels:
      app: codartium-node-agent
  template:
    metadata:
      labels:
        app: codartium-node-agent
    spec:
      hostNetwork: false
      tolerations:
        - operator: "Exists"
          effect: "NoSchedule"
      containers:
        - name: agent
          image: codartium/node-agent:latest
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop: ["ALL"]
          env:
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
          resources:
            requests:
              cpu: "50m"
              memory: "64Mi"
            limits:
              cpu: "100m"
              memory: "128Mi"
          volumeMounts:
            - name: varlog
              mountPath: /var/log
              readOnly: true
      volumes:
        - name: varlog
          hostPath:
            path: /var/log
            type: Directory