DEV Community

Cover image for Kubernetes DaemonSet: Everything You Need to Know
Avesh
Avesh

Posted on

Kubernetes DaemonSet: Everything You Need to Know

Kubernetes, a powerful container orchestration tool, provides a variety of resources to manage workloads effectively. One such resource is the DaemonSet, which ensures that a particular Pod runs on all (or some) nodes in a Kubernetes cluster. This article dives deep into Kubernetes DaemonSet, its features, use cases, and practical examples.


What is a DaemonSet in Kubernetes?

A DaemonSet ensures that a specific Pod is deployed on every eligible node in a Kubernetes cluster. Unlike Deployments, which manage the number of replicas across the cluster, DaemonSets are designed to ensure that a single Pod runs on each node. This is particularly useful for tasks like node monitoring, log collection, or any node-level operation.

Key Features of DaemonSets

  1. Automatic Pod Deployment: Deploys a Pod to all or a subset of nodes.
  2. Dynamic Updates: Automatically adds Pods to new nodes added to the cluster.
  3. Node Affinity: Allows targeting specific nodes using labels or taints.
  4. Rolling Updates: Facilitates seamless updates to DaemonSet-managed Pods.

How DaemonSet Works

When you create a DaemonSet, Kubernetes ensures that:

  • A Pod is scheduled on all eligible nodes.
  • If new nodes are added to the cluster, the Pod is automatically deployed on them.
  • If a node is removed, the associated DaemonSet Pod is also removed.

Types of DaemonSet Updates

DaemonSet updates are controlled using the updateStrategy field:

  1. RollingUpdate: Gradual updates to Pods on nodes.
  2. OnDelete: Pods are updated only when manually deleted.

Common Use Cases for DaemonSet

  1. Log Collection: Tools like Fluentd or Logstash collect logs from all nodes.
  2. Monitoring and Metrics Collection: Tools like Prometheus Node Exporter or Datadog Agent run as DaemonSets to gather metrics.
  3. Networking: Services like Calico or Weave Net, which provide networking for Kubernetes clusters, run as DaemonSets.
  4. Node-Level Configuration: Custom scripts or processes that need to run on every node.

Creating a DaemonSet

Here’s an example to deploy a simple Nginx DaemonSet:

DaemonSet YAML Example

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx-daemonset
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
        ports:
        - containerPort: 80
  updateStrategy:
    type: RollingUpdate
Enter fullscreen mode Exit fullscreen mode

Explanation

  • apiVersion: apps/v1: Specifies the API version.
  • kind: DaemonSet: Defines the resource type.
  • metadata: Provides a name and optional labels for the DaemonSet.
  • selector: Matches Pods with the specified labels.
  • template: Specifies the Pod configuration, including the container image (Nginx in this case).
  • updateStrategy: Defines the update strategy as RollingUpdate.

Advanced Configuration

1. Node Affinity

You can restrict a DaemonSet to specific nodes using labels:

spec:
  template:
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: environment
                operator: In
                values:
                - production
Enter fullscreen mode Exit fullscreen mode

2. Tolerations

To schedule Pods on tainted nodes, use tolerations:

spec:
  template:
    spec:
      tolerations:
      - key: "node-role.kubernetes.io/master"
        operator: "Exists"
        effect: "NoSchedule"
Enter fullscreen mode Exit fullscreen mode

3. Host Networking

To allow Pods to access host network interfaces, enable host networking:

spec:
  template:
    spec:
      hostNetwork: true
Enter fullscreen mode Exit fullscreen mode

Updating a DaemonSet

DaemonSet updates are managed using the kubectl apply command. For example:

kubectl apply -f nginx-daemonset.yaml
Enter fullscreen mode Exit fullscreen mode

To update Pods without downtime, ensure the updateStrategy is set to RollingUpdate.


Managing DaemonSets

View DaemonSets

kubectl get daemonset
Enter fullscreen mode Exit fullscreen mode

Check DaemonSet Pods

kubectl get pods -o wide
Enter fullscreen mode Exit fullscreen mode

Delete a DaemonSet

kubectl delete daemonset nginx-daemonset
Enter fullscreen mode Exit fullscreen mode

DaemonSet Best Practices

  1. Use Labels: Clearly label nodes and DaemonSet Pods for better targeting and monitoring.
  2. Optimize Resources: Assign appropriate resource limits to ensure Pods don't overwhelm nodes.
  3. Monitor Logs: Regularly monitor DaemonSet Pods for issues using tools like kubectl logs.
  4. Test Updates: Test configuration updates in a staging environment before applying to production.

Conclusion

Kubernetes DaemonSet is a powerful tool for managing node-level tasks in a cluster. Whether it's for monitoring, log collection, or networking, DaemonSets ensure consistent Pod deployment across all eligible nodes. By understanding its features, use cases, and best practices, you can harness DaemonSet effectively in your Kubernetes setup.

Start exploring DaemonSet today to optimize your Kubernetes workflows!

Top comments (0)