How to Choose Between DaemonSet and Deployment in Kubernetes
In this tutorial, you'll learn about How to Choose Between DaemonSet and Deployment in Kubernetes. We cover key concepts, practical examples, and best practices.
The Problem
You are creating a Kubernetes workload and are unsure whether to use a DaemonSet or a Deployment, leading to pods either running on the wrong nodes or not covering all nodes.
Quick Fix
Use a DaemonSet for Node-Level Services
node -e "
const ds = {
apiVersion: 'apps/v1',
kind: 'DaemonSet',
spec: {
selector: { matchLabels: { app: 'fluentd' } },
template: {
metadata: { labels: { app: 'fluentd' } },
spec: {
containers: [{
name: 'fluentd',
image: 'fluentd:v1.16'
}]
}
}
}
};
console.log('DaemonSet runs one pod per node');
"
# DaemonSet runs one pod per node
DaemonSets ensure one pod runs on every node (or a subset defined by nodeSelector). Use them for logging agents, monitoring daemons, CNI plugins, and node-exporter tools.
Use a Deployment for Stateless Applications
node -e "
const deploy = {
apiVersion: 'apps/v1',
kind: 'Deployment',
spec: {
replicas: 3,
selector: { matchLabels: { app: 'nginx' } },
template: {
metadata: { labels: { app: 'nginx' } },
spec: {
containers: [{
name: 'nginx',
image: 'nginx:1.25'
}]
}
}
}
};
console.log('Deployment manages replicas across the cluster');
"
# Deployment manages replicas across the cluster
Deployments manage a set of identical pods distributed across available nodes. Use them for web servers, APIs, workers, and any stateless service that scales with replicas.
Run DaemonSet on Specific Nodes Only
node -e "
const ds = {
spec: {
template: {
spec: {
nodeSelector: {
'kubernetes.io/os': 'linux'
},
tolerations: [{
key: 'node-role.kubernetes.io/control-plane',
effect: 'NoSchedule'
}]
}
}
}
};
console.log('DaemonSet with nodeSelector and tolerations');
"
# DaemonSet with nodeSelector and tolerations
Add nodeSelector to limit DaemonSet pods to specific node pools. Add tolerations to run on control-plane nodes or nodes with custom taints.
Choose Based on Scaling Strategy
# DaemonSet: one pod per node, scales with node count
# Deployment: N replicas, scales with replica count
kubectl scale deployment my-app --replicas=5
# deployment.apps/my-app scaled
DaemonSets are ideal for node-level agents that need to run everywhere (logging, monitoring, security). Deployments are ideal for application workloads that scale horizontally based on traffic, not node count.
Use kubectl describe for Detailed Diagnostics
kubectl describe pod <pod-name>
# Events:
# Type Reason Age From Message
# ---- ------ ---- ---- -------
# Warning BackOff 5m kubelet Back-off restarting failed container
The Events section at the bottom of kubectl describe output is the most valuable diagnostic tool. It shows a chronological log of scheduling failures, image pull errors, and container crashes.
Prevention
- Use DaemonSet when you need exactly one pod per node (logging, monitoring, networking)
- Use Deployment when you need a specific replica count distributed across the cluster
- Use StatefulSet for stateful applications requiring stable network identities and persistent storage
- Combine nodeSelector and tolerations with DaemonSets to target specific node pools
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro