DEV Community

Cover image for DaemonSets - Why, when and how
Ashok Nagaraj
Ashok Nagaraj

Posted on

DaemonSets - Why, when and how

Why & When

The basic unit of deployment in kubernetes is a Pod and a managed objected with guaranteed number of replicas is a Deployment. In-case of a deployment the nodes on which pods land-up is decided by the scheduler based on the scheduling policy.

Use DaemonSet when

  • When you want one pod of your application to run on each node (or identified subset of nodes)
  • When you want to address those pods directly and not through the load-balancer

Typical use-cases

  1. Running agents on kubernetes worker nodes - monitoring agents
  2. Running aggregators on a per node basis - log aggregators
  3. Running node-wise daemon processes for distributing load - Interesting usecase from this dev.to post

How

Creation

cat /tmp/ds.yaml
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-ds
  namespace: kube-system
spec:
  selector:
    matchLabels:
      name: fluentd-ds
  template:
    metadata:
      labels:
        name: fluentd-ds
    spec:
      tolerations:
        # this toleration is to have the daemonset runnable on master nodes
        # remove it if your masters can't run pods
        - key: node-role.kubernetes.io/master
          effect: NoSchedule
      containers:
        - name: fluentd-ds
          image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2

❯ kubectl create -f /tmp/ds.yaml
daemonset.apps/fluentd-ds created

❯ kubectl get daemonsets.apps/fluentd-ds -n kube-system
NAME         DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
fluentd-ds   4         4         4       4            4           <none>          116s

❯ kubectl get pods -n kube-system -o wide | grep fluentd-ds
fluentd-ds-mm26b                                1/1     Running   0          2m45s   10.244.0.6   macbook-control-plane   <none>           <none>
fluentd-ds-nx9hb                                1/1     Running   0          2m45s   10.244.3.4   macbook-worker2         <none>           <none>
fluentd-ds-tfrvn                                1/1     Running   0          2m45s   10.244.2.3   macbook-worker          <none>           <none>
fluentd-ds-vv5rx                                1/1     Running   0          2m45s   10.244.1.4   macbook-worker3         <none>           <none>

Enter fullscreen mode Exit fullscreen mode

Communication
All usual modes of services - ClusterIP, NodePort, LoadBalancer and Headless service work (though ClusterIP and LoadBalancer does not make too much sense). General norm is to use ports.hostPort and then map it through a NodePort and address via <node-ip>:<node-port>

ports:
- containerPort: 24224
  hostPort: 24224
  name: "fluentd-port"
Enter fullscreen mode Exit fullscreen mode

More info

Top comments (0)