Skip to main content

Working with DaemonSets

1. Creating DaemonSet

Field Description Example
apiVersion API version to use apps/v1
kind Resource type DaemonSet
metadata.name Name of the DaemonSet fluentd-elasticsearch
spec.selector Label selector for pods {"matchLabels": {"name": "fluentd-elasticsearch"}}
spec.template Pod template to be used {"metadata": {"labels": {"name": "fluentd-elasticsearch"}}, "spec": {"containers": [...]}}

Example: Basic DaemonSet YAML

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
spec:
  selector:
    matchLabels:
      name: fluentd-elasticsearch
  template:
    metadata:
      labels:
        name: fluentd-elasticsearch
    spec:
      containers:
      - name: fluentd-elasticsearch
        image: quay.io/fluent/fluentd-kubernetes-daemonset:v1.15.0
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi

2. Listing DaemonSets

Command Description Notes
kubectl get daemonsets List all DaemonSets in the current namespace Alias: kubectl get ds
kubectl get daemonsets -A List all DaemonSets across all namespaces Useful for cluster-wide overview
kubectl get daemonsets -n <namespace> List DaemonSets in a specific namespace Replace <namespace> with actual namespace name
kubectl get daemonsets -o wide Get detailed information including node names Shows which nodes each DaemonSet is scheduled on

Example: Listing DaemonSets in the default namespace

kubectl get daemonsets

Example: Listing all DaemonSets across all namespaces

kubectl get daemonsets -A

3. Describing DaemonSet Details

Property Description Example Value
apiVersion The API version for the DaemonSet resource apps/v1
kind Resource type — must be DaemonSet DaemonSet
metadata.name Unique name for the DaemonSet within the namespace fluentd-elasticsearch
metadata.namespace Namespace where the DaemonSet is deployed default
spec.selector Label selector used to identify pods managed by this DaemonSet {"matchLabels": {"name": "fluentd-elasticsearch"}}
spec.template Pod template used to create pods on each node
{
  "metadata": {
    "labels": {"name": "fluentd-elasticsearch"}
  },
  "spec": {
    "containers": [
      {
        "name": "fluentd",
        "image": "fluent/fluentd-kubernetes-daemonset:v2.5.2"
      }
    ]
  }
}
spec.updateStrategy Strategy to use when updating pods (e.g., RollingUpdate) {"type": "RollingUpdate", "rollingUpdate": {"maxUnavailable": 1}}
spec.minReadySeconds Minimum number of seconds a new pod must be ready before it's considered available 30
spec.revisionHistoryLimit Number of revisions to keep for rollback purposes 10

Example: DaemonSet YAML Definition

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  namespace: default
spec:
  selector:
    matchLabels:
      name: fluentd-elasticsearch
  template:
    metadata:
      labels:
        name: fluentd-elasticsearch
    spec:
      containers:
      - name: fluentd
        image: fluent/fluentd-kubernetes-daemonset:v2.5.2
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
  minReadySeconds: 30
Note: The spec.template field defines the pod template that will be used to create pods on each node. It must include a valid metadata and spec.
Warning: DaemonSets do not support replicas, as they ensure one pod per node matching the selector.

4. Understanding DaemonSet Scheduling

Field Description Values
spec.selector Selector used to identify pods managed by the DaemonSet {matchLabels: {name: "daemon-pod"}}
spec.template Template for creating pods PodSpec object
spec.updateStrategy How to update pods when the DaemonSet is updated RollingUpdate or OnDelete

Example: DaemonSet with update strategy

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
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
  updateStrategy:
    type: RollingUpdate
Note: DaemonSets ensure one pod per node matching the selector. The update strategy determines how existing pods are replaced during updates.

5. Using Node Selectors

Field Description Example
nodeSelector Map of key-value pairs for node labels to match {"node-type": "production"}
nodeAffinity More flexible way to select nodes using operators like In, NotIn, Exists
{
  "requiredDuringSchedulingIgnoredDuringExecution": {
    "nodeSelectorTerms": [
      {
        "matchExpressions": [
          {
            "key": "environment",
            "operator": "In",
            "values": ["production"]
          }
        ]
      }
    ]
  }
}

Example: DaemonSet with Node Selector

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
spec:
  selector:
    matchLabels:
      app: fluentd-elasticsearch
  template:
    metadata:
      labels:
        app: fluentd-elasticsearch
    spec:
      nodeSelector:
        node-type: production
      containers:
      - name: fluentd-elasticsearch
        image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
Note: nodeSelector is a simple label match. For more complex logic, use nodeAffinity.

6. Using Node Affinity

Field Description Values
nodeSelector Simple label-based node selection {"key": "value"}
affinity.nodeAffinity Advanced node affinity rules {"requiredDuringSchedulingIgnoredDuringExecution": {...}, "preferredDuringSchedulingIgnoredDuringExecution": [...]}

Example: Node Affinity Configuration

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: example-daemonset
spec:
  selector:
    matchLabels:
      app: example
  template:
    metadata:
      labels:
        app: example
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/e2e-az-name
                    operator: In
                    values:
                      - e2e-az1
                      - e2e-az2
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 1
              preference:
                matchExpressions:
                  - key: another-node-label-key
                    operator: In
                    values:
                      - another-node-label-value
      containers:
        - name: example-container
          image: nginx:latest
Note: requiredDuringSchedulingIgnoredDuringExecution is a hard requirement, while preferredDuringSchedulingIgnoredDuringExecution is a soft preference.

7. Tolerating Taints

Field Description Example
tolerations List of tolerations to apply to the DaemonSet
[
  {
    "key": "node-role.kubernetes.io/master",
    "effect": "NoSchedule"
  }
]

Example: Tolerating Master Node Taints

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: example-daemonset
spec:
  selector:
    matchLabels:
      app: example
  template:
    metadata:
      labels:
        app: example
    spec:
      tolerations:
      - key: "node-role.kubernetes.io/master"
        effect: "NoSchedule"
        operator: "Exists"
      containers:
      - name: example-container
        image: nginx
Note: Tolerations allow pods to be scheduled on nodes with matching taints. The operator: "Exists" means any taint with the specified key will be tolerated.

8. Updating DaemonSets

Field Description Values
spec.updateStrategy.type Update strategy type for the DaemonSet RollingUpdate, OnDelete
spec.updateStrategy.rollingUpdate.maxUnavailable Maximum number of DaemonSet pods that can be unavailable during the update 1, 2, 50%

Example: Update DaemonSet with RollingUpdate

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: example-daemonset
spec:
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
  selector:
    matchLabels:
      app: example-app
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: example-container
        image: nginx:latest

9. Setting Max Unavailable

Field Description Default Value Notes
maxUnavailable Maximum number of DaemonSet pods that can be unavailable during an update 1 Can be specified as a number or percentage (e.g., 1, 20%)

Example: Setting maxUnavailable to 20%

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: example-daemonset
spec:
  selector:
    matchLabels:
      app: example-app
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: example-container
        image: nginx:latest
  updateStrategy:
    rollingUpdate:
      maxUnavailable: 20%
Note: When maxUnavailable is set to a percentage, it is calculated based on the total number of nodes running the DaemonSet.

10. Deleting DaemonSets

Field Description Values
metadata.name Name of the DaemonSet example-daemonset
metadata.namespace Namespace where the DaemonSet is located default

Example: Delete a DaemonSet

kubectl delete daemonset example-daemonset -n default
Note: Deleting a DaemonSet will remove all pods managed by it.
Warning: Ensure that deleting the DaemonSet won't impact critical services or nodes.

11. Troubleshooting DaemonSet Pods

Issue Possible Cause Solution
Pods stuck in Pending Node resource constraints (CPU/Memory) Check node resources with kubectl describe node <node-name>
Pods stuck in ContainerCreating Image pull issues or invalid container config Run kubectl describe pod <pod-name> to see events
Pods CrashLoopBackOff Application errors or misconfiguration Check logs with kubectl logs <pod-name>
Pods Not Scheduled NodeSelector or Taints/Tolerations mismatch Verify node labels and tolerations match DaemonSet spec

Example: Check DaemonSet Pod Status

kubectl get pods -l app=nginx-daemonset -o wide
kubectl describe pod <pod-name>
Note: Use kubectl get pods --all-namespaces to check all DaemonSet pods across namespaces.
Warning: Avoid modifying DaemonSet pods directly; changes are overwritten by the controller.

Was this article helpful?