This content originally appeared on DEV Community and was authored by Laxman Patel
Kubernetes is a bustling ecosystem of pods, services, and deployments. But what about those tasks that need to run on every node in your cluster?
Enter DaemonSetsβthe unsung heroes of Kubernetes. In this article, weβll explore what DaemonSets are, why theyβre essential, and how to use them effectively.
οΏ½ The Problem: Node-Level Tasks in Kubernetes
Imagine you need to:
Run a logging agent on every node.
Deploy a monitoring tool like Prometheus Node Exporter.
Ensure a security agent is always present on all nodes.
Using a regular Deployment or Pod wonβt cut it because:
You canβt guarantee a pod will run on every node.
Scaling manually is tedious and error-prone.
New nodes wonβt automatically get the required pods.
This is where DaemonSets come to the rescue.
What Are DaemonSets?
A DaemonSet is a Kubernetes controller that ensures a copy of a pod runs on every node (or a subset of nodes) in your cluster. If a new node is added, the DaemonSet automatically schedules a pod on it. If a node is removed, the pod is garbage-collected.
Key features:
Node-Level Coverage: Runs a pod on every node (or specific nodes using labels).
Automatic Scaling: Scales with your clusterβno manual intervention needed.
Self-Healing: If a pod is deleted, the DaemonSet recreates it.
Resource Efficiency: Ensures only one pod runs per node (unless overridden).
Why Are DaemonSets Needed?
Node-Specific Tasks: Perfect for logging, monitoring, and security agents.
Cluster-Wide Consistency: Ensures every node has the required software.
Automatic Scaling: Handles node additions and removals seamlessly.
Resource Optimization: Avoids over-provisioning by running only one pod per node.
How to Use DaemonSets
Letβs create a DaemonSet to deploy a logging agent on every node in your cluster.
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: logging-agent
namespace: kube-system
labels:
app: logging-agent
spec:
selector:
matchLabels:
name: logging-agent
template:
metadata:
labels:
name: logging-agent
spec:
containers:
- name: logging-agent
image: fluent/fluentd:latest
resources:
limits:
memory: "200Mi"
cpu: "100m"
requests:
memory: "100Mi"
cpu: "50m"
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
selector: Matches the pods managed by this DaemonSet.
template: Defines the pod specification.
tolerations: Allows the DaemonSet to run on master nodes (optional).
Apply the DaemonSet:
kubectl apply -f daemonset.yaml
Now, a logging-agent
pod will run on every node in your cluster. If you add or remove nodes, the DaemonSet will handle it automatically.
Advanced Use Cases
Node-Specific Workloads: Use node labels to run DaemonSets on specific nodes.
Example: Run a GPU monitoring tool only on GPU-enabled nodes.Custom Taints and Tolerations: Control which nodes the DaemonSet can run on.
Rolling Updates: Update DaemonSet pods in a controlled manner using
updateStrategy
.
Key Takeaways
DaemonSets ensure a pod runs on every node in your cluster.
Theyβre perfect for node-specific tasks like logging, monitoring, and security.
They scale automatically with your cluster and handle node changes seamlessly.
Use them to maintain consistency and efficiency across your nodes.
So, the next time you need to run a pod on every node, think DaemonSetsβyour silent guardians in the Kubernetes world.
This content originally appeared on DEV Community and was authored by Laxman Patel