DEV Community

Cover image for Kubernetes DaemonSets vs Deployments: Key Differences and Use Cases
Yash Londhe for RubixKube

Posted on

Kubernetes DaemonSets vs Deployments: Key Differences and Use Cases

Imagine you’re hosting a big party. You need enough snacks (pods) to feed everyone, but you also want security guards (background services) at every entrance to keep things safe. In Kubernetes, Deployments are like your snack stations—they ensure there’s enough food (pods) to handle the crowd. DaemonSets, on the other hand, are like those security guards: they make sure a critical task (like monitoring or logging) runs on every node in your cluster.

Kubernetes is the ultimate organizer for your applications. It manages where they run, how they scale, and how they recover from failures. But to use it effectively, you need to pick the right tool for the job. Let’s break down when to use Deployments vs. DaemonSets, even if you’re just starting out!

What is a Kubernetes Deployment?

Deployment is your go-to tool for running stateless applications (apps that don’t store data). It acts like a manager, ensuring a specific number of identical pods (containers) are always running.

Key Features:

  1. Scaling: Need more pods? Change the replicas count, and Kubernetes adds/removes pods automatically.
    • Example: If your web app gets 10,000 visitors, scale from 5 to 20 pods to handle traffic.
  2. Rolling Updates: Update your app without downtime. Kubernetes replaces old pods with new ones gradually.
  3. Rollbacks: If an update breaks your app, revert to the previous version with one command.

Example YAML:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: pizza-api         # Name of your Deployment
spec:
  replicas: 3            # Always keep 3 identical pods running
  selector:
    matchLabels:
      app: pizza         # Tells Kubernetes which pods to manage
  template:              # Defines the "recipe" for pods
    metadata:
      labels:
        app: pizza       # Labels link the Deployment to its pods
    spec:
      containers:
      - name: pizza-api
        image: pizza-api:v2  # Container image to use
        ports:
        - containerPort: 8080
Enter fullscreen mode Exit fullscreen mode

How It Works:

  • Deployments use ReplicaSets (a helper) to manage pods.
  • When you update the app (e.g., pizza-api:v2 to v3), the Deployment creates a new ReplicaSet. It spins up pods with the new version while shutting down old ones.

Use Cases:

  • Hosting a blog or e-commerce site.
  • Backend APIs (e.g., user authentication, payment processing).

What is a Daemon?

daemon (pronounced "dee-mon" or "day-mon") is a computer program that runs in the background and does things automatically without needing you to control it.Think of it like a security guard 🚓Imagine you own a big office building. You hire a security guard to:

  • Watch the doors
  • Check who enters and leaves
  • Respond if something happens

But you don’t have to stand next to the guard and tell them what to do. They work on their own, always running in the background.A daemon works just like that on a computer. It keeps running in the background and does important tasks automatically.Real Examples of Daemons

  1. Print Daemon (cupsd) – Makes sure your printer is ready 🖨
  2. Network Daemon (NetworkManager) – Keeps your Wi-Fi connected 
  3. SSH Daemon (sshd) – Allows remote logins to your computer 
  4. Cron Daemon (cron) – Runs scheduled tasks (like backups) 

How to Spot a Daemon?On many systems, daemons often end with "d". For example:

  • sshd → Handles remote logins
  • syslogd → Handles system logs
  • crond → Runs scheduled tasks

Why Are Daemons Useful?

  • They automate things so you don’t have to do them manually.
  • They run in the background, so your computer works smoothly.
  • They respond to events, like when you connect Wi-Fi or print a file.

Think of daemons as invisible helpers that keep your computer working without bothering you!

What is a Kubernetes DaemonSet?

DaemonSet ensures a specific pod runs on every node in your cluster (or nodes matching a label). It’s ideal for cluster-wide services that need to “stick” to nodes.

Key Features:

  1. One Pod Per Node: Automatically deploys a pod to every new node added to the cluster.
    • Example: A logging agent that collects logs from all nodes.
  2. Node Selectors: Target specific nodes (e.g., only nodes with SSD disks).

Example YAML:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-monitor
spec:
  selector:
    matchLabels:
      app: node-monitor   # Links DaemonSet to its pods
  template:
    metadata:
      labels:
        app: node-monitor
    spec:
      containers:
      - name: monitor-agent
        image: prometheus/agent:latest  # Monitoring tool
      nodeSelector:
        disktype: ssd     # Only run on nodes labeled "disktype=ssd"
Enter fullscreen mode Exit fullscreen mode

How It Works:

  • When a new node joins the cluster, the DaemonSet immediately deploys a pod to it.
  • If the node is removed, the pod is deleted.

Use Cases:

  • Monitoring agents (e.g., Prometheus).
  • Log collectors (e.g., Fluentd).
  • Network plugins (e.g., Calico for networking).

Key Differences: DaemonSets vs. Deployments

Let’s compare them side by side:

comparison table

Example Scenario:

  • Deployment: You run a weather app with 10 replicas. Kubernetes might place 3 pods on Node A, 5 on Node B, and 2 on Node C.
  • DaemonSet: You deploy a security scanner. Kubernetes ensures one pod runs on every node, including Node A, B, and C.

When to Use Deployments

Use Deployments when:

  1. You need flexibility: Scale up/down based on traffic.
  2. Your app is stateless: Pods don’t store unique data (e.g., a REST API).
  3. You want easy updates: Roll out new versions safely.

Real-World Examples:

  • A social media app uses Deployments to handle its frontend web servers. During peak hours, it scales from 50 to 200 pods.
  • An online store uses Deployments for its product catalog API.

When to Use DaemonSets

Use DaemonSets when:

  1. You need a pod on every node: For example, monitoring tools that collect node-level metrics.
  2. Node-specific tasks: Like storage drivers that must run where the disk is attached.
  3. Cluster-wide services: Network plugins or security agents.

Real-World Examples:

  • Netflix uses DaemonSets to run log collectors on every node, ensuring no log data is lost.
  • A blockchain network uses DaemonSets to deploy node-specific validators.

Best Practices

  1. Resource Limits:
    • For DaemonSets: Set CPU/memory limits to avoid starving node resources.
    • For Deployments: Use autoscaling to add pods during traffic spikes.
  2. Updates:
    • Use RollingUpdate strategy for both to avoid downtime.
  3. Avoid Mixing:
    • Don’t use DaemonSets for apps that don’t need to run on every node (e.g., a blog).
  4. Alternatives:
    • Use StatefulSets for stateful apps (e.g., databases).

Conclusion

  • Deployments = Your scalable, general-purpose app manager.
  • DaemonSets = Your node-level assistant for cluster-wide tasks.

Still unsure? Ask these questions:

  1. Does my app need to run on every node? → DaemonSet.
  2. Do I need to scale based on traffic? → Deployment.

Additional Resources

With this guide, you’re ready to choose the right tool for your Kubernetes workloads! 🎯

Top comments (0)