A DaemonSet is a type of controller object that ensures that a specific pod runs on each node in the cluster. DaemonSets are useful for deploying system daemons or other background tasks that need to run on every node.
Here's how DaemonSets work and some key points to understand:
One Pod per Node: A DaemonSet guarantees that there is exactly one instance of a specified pod running on each node in the Kubernetes cluster. If new nodes are added to the cluster, the DaemonSet automatically schedules pods onto those nodes.
DaemonSet Controller: The Kubernetes control plane includes a DaemonSet controller that continuously monitors the cluster's state. When a DaemonSet is created or updated, the controller ensures that the desired number of pods is running on each node.
Node Selector and Affinity: DaemonSets can be configured to run on specific nodes using node selectors or node affinity rules. This allows you to control which nodes the DaemonSet's pods are scheduled on based on labels assigned to nodes.
Updating DaemonSets: When you update a DaemonSet (e.g., by changing the pod template), Kubernetes will automatically roll out the changes to all nodes. It follows a rolling update strategy by default, ensuring that there is no downtime during the update process.
Pod Eviction: DaemonSets also handle pod eviction gracefully. If a node becomes unhealthy or is removed from the cluster, the DaemonSet controller ensures that the pod running on that node is rescheduled onto a healthy node.
Use Cases: DaemonSets are commonly used for deploying cluster-level services or agents, such as monitoring agents (e.g., Prometheus Node Exporter), logging agents (e.g., Fluentd), or networking plugins (e.g., CNI plugins like Calico or Flannel).
Here's a basic example of a DaemonSet YAML manifest:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: example-daemonset
spec:
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
containers:
- name: example-container
image: example-image:tag
This DaemonSet definition ensures that one pod with the label app: example runs on each node in the cluster, using the specified container image.
Overall, DaemonSets are a powerful tool in Kubernetes for deploying and managing background tasks or system-level services across a cluster of nodes.