That's an excellent question, and it gets to the heart of what a DaemonSet does! Labby is happy to explain.
DaemonSets manage pods in a very specific and powerful way, different from other controllers like Deployments or ReplicaSets.
Here's how a DaemonSet manages pods:
-
Ensures One Pod Per Node (or selected nodes):
- The primary goal of a DaemonSet is to ensure that one (and only one) copy of a specific pod is running on each node in your Kubernetes cluster that matches certain criteria.
- It's like a rule that says, "For every house in the neighborhood, there must be one gardener."
-
Watches for Nodes:
- A DaemonSet controller constantly monitors the cluster for changes in the number of nodes.
- Node Added: When a new node joins the cluster, the DaemonSet automatically detects it and provisions a new pod onto that new node.
- Node Removed: If a node is removed from the cluster, the DaemonSet recognizes this and cleans up the pod that was running on it.
- Node Unhealthy: If a node becomes unhealthy or unresponsive, the DaemonSet doesn't try to move the pod to another node (unlike Deployments/ReplicaSets). It waits for the unhealthy node to either recover or be removed.
-
Uses a Pod Template:
- As we discussed, the DaemonSet does not create pods from scratch. Instead, it uses a
template(thespec.templatesection in your YAML) which acts as a blueprint or specification for the pods it needs to create. This template defines the container image, ports, labels, environment variables, etc.
- As we discussed, the DaemonSet does not create pods from scratch. Instead, it uses a
-
Uses Selectors for Management:
- The DaemonSet uses a
selector(e.g.,matchLabels: {app: myapp}) to identify which pods belong to it. This allows it to:- Track the pods it has created.
- Monitor their status.
- Ensure that the correct number (one per node) is maintained.
- The DaemonSet uses a
-
Handles Updates:
- If you update the DaemonSet's pod template (e.g., change the image version), the DaemonSet can perform a rolling update to replace the old pods with new ones on each node, one by one, according to its update strategy.
Common Use Cases for DaemonSets:
- Cluster Storage Daemons: Running a storage agent like Ceph or GlusterFS on every node.
- Log Collection Daemons: Running a log collector like Fluentd or Logstash on every node to gather logs.
- Node Monitoring Daemons: Running a Prometheus node exporter or other monitoring agents on every node.
- Network Plugins: Many CNI (Container Network Interface) plugins run as DaemonSets.
In summary, a DaemonSet is designed for applications that need to run continuously on each node to provide node-level utilities or services. It automates the lifecycle of these pods based on the presence and health of your cluster's nodes.
Does this explanation help you understand how DaemonSets manage pods?