A DaemonSet is a Kubernetes resource that ensures a copy of a specific pod runs on all (or selected) nodes in a Kubernetes cluster. DaemonSets are typically used for tasks that need to run on every node in the cluster, such as monitoring agents, logging agents, network proxies, or system-level services.
Key Characteristics of DaemonSet:
- One Pod Per Node: A DaemonSet guarantees that exactly one instance of the pod will run on each eligible node in the cluster. If new nodes are added to the cluster, the DaemonSet automatically schedules a pod to run on those nodes as well.
- Automatic Scheduling: When a new node is added to the cluster, the DaemonSet controller automatically ensures that the DaemonSet pod is scheduled on the new node.
- Pod Lifecycle Management: DaemonSets manage the lifecycle of the pods, ensuring that the specified pods are running on all the relevant nodes and will be automatically deleted when the DaemonSet itself is deleted.
- Selective Node Targeting: You can use node selectors, affinity rules, and tolerations to control on which nodes the DaemonSet pods should run. For example, you can ensure that DaemonSet pods only run on specific nodes that have certain labels.
Common Use Cases:
- Monitoring and Logging Agents: DaemonSets are commonly used for running monitoring or logging agents (like Prometheus Node Exporter, Fluentd, or Datadog agents) on all nodes to collect metrics or logs from every node in the cluster.
- Network Proxies: In some cases, DaemonSets can run network proxies or sidecar containers that perform tasks like handling network traffic, service mesh components (e.g., Istio sidecars), or load balancers.
- System Services: DaemonSets can be used for running system services that need to interact with the host machine, such as storage daemons, security agents, or device drivers.
How DaemonSets Work:
- Creating a DaemonSet: You can define a DaemonSet in YAML format (like other Kubernetes resources) and apply it with
kubectl
.
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: example-daemonset
spec:
selector:
matchLabels:
name: example-daemonset
template:
metadata:
labels:
name: example-daemonset
spec:
containers:
- name: example-container
image: nginx:latest
- DaemonSet Controller: The DaemonSet controller ensures that the pods are running on the specified nodes. It monitors the cluster and manages the lifecycle of pods in response to node changes (e.g., adding/removing nodes).
Operations with DaemonSets:
- Scaling: DaemonSets do not have a replica count because there is inherently one pod per node. You control the pods running on nodes through labels, selectors, and affinity, not through replicas.
- Updates and Rolling Upgrades: When you update a DaemonSet (e.g., changing the pod template), Kubernetes will perform a rolling update to gradually replace the pods on all nodes without downtime.
- Deleting DaemonSets: When a DaemonSet is deleted, the pods managed by the DaemonSet are also removed from all nodes.
Example Command to Create a DaemonSet:
kubectl apply -f daemonset.yaml
Example Command to Get DaemonSet Info:
kubectl get daemonset
Difference Between DaemonSet and ReplicaSet:
- DaemonSet ensures one pod per node, whereas a ReplicaSet ensures a specific number of pod replicas, but not necessarily one per node.
- ReplicaSet is used for stateless applications (e.g., web servers), while DaemonSet is typically used for stateful or system-level services that need to run on every node.
Conclusion:
A DaemonSet is an essential Kubernetes resource for running background tasks that need to be distributed across all or selected nodes in a cluster. They are ideal for system-level applications that require node-wide access to resources.