Kubernetes DaemonSets: Run Pods on Every Node
DaemonSets ensure a pod runs on every (or selected) node in the cluster.
Use Cases
- Log collection (Fluentd, Filebeat)
- Monitoring agents (Prometheus Node Exporter, Datadog)
- Network plugins (Calico, Weave)
- Storage daemons (GlusterFS, Ceph)
- Node configuration
Creating a DaemonSet
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter
namespace: monitoring
spec:
selector:
matchLabels:
app: node-exporter
template:
metadata:
labels:
app: node-exporter
spec:
containers:
- name: node-exporter
image: prom/node-exporter:latest
ports:
- containerPort: 9100
hostPort: 9100DaemonSet vs Deployment
| Feature | DaemonSet | Deployment |
|---|---|---|
| Scheduling | One per node | Based on replicas |
| Scaling | Automatic with nodes | Manual/HPA |
| Use case | Node-level services | Application workloads |
Node Selection
All Nodes (Default)
spec:
template:
spec:
tolerations:
- operator: Exists # Tolerate all taintsSpecific Nodes
spec:
template:
spec:
nodeSelector:
node-type: loggingUsing Affinity
spec:
template:
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: zone
operator: In
values:
- us-east-1aTolerating Control Plane Nodes
spec:
template:
spec:
tolerations:
- key: node-role.kubernetes.io/control-plane
operator: Exists
effect: NoScheduleUpdate Strategy
spec:
updateStrategy:
type: RollingUpdate # Default
rollingUpdate:
maxUnavailable: 1Fluentd Log Collector Example
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd
namespace: logging
spec:
selector:
matchLabels:
app: fluentd
template:
metadata:
labels:
app: fluentd
spec:
containers:
- name: fluentd
image: fluent/fluentd:latest
volumeMounts:
- name: varlog
mountPath: /var/log
- name: dockerlogs
mountPath: /var/lib/docker/containers
readOnly: true
volumes:
- name: varlog
hostPath:
path: /var/log
- name: dockerlogs
hostPath:
path: /var/lib/docker/containersManaging DaemonSets
# List DaemonSets
kubectl get daemonsets -A
# Check status
kubectl rollout status daemonset/node-exporter
# Update image
kubectl set image daemonset/node-exporter node-exporter=prom/node-exporter:v1.5.0Practice DaemonSets
DaemonSets appear in CKA exams. Practice at Sailor.sh.