DaemonSets in Kubernetes
DaemonSets are a type of Kubernetes controller that ensures a copy of a pod runs on all or specified nodes in a cluster. They are useful for deploying system daemons or services that need to run on every node.
With a DaemonSet, as nodes are added to the cluster, pods are automatically added to them. As nodes are removed from the cluster, the pods created by the DaemonSet are garbage collected. Deleting a DaemonSet will clean up all the pods that it created.
Some common use cases for DaemonSets are running a cluster storage daemon, log collection daemon, or node monitoring daemon on every node.
To use a DaemonSet, you define a pod template and a selector to match the pods the DaemonSet should manage. For example, a DaemonSet might create fluentd pods on each node for log collection.
The DaemonSet controller handles scheduling the pods across nodes and recreating them if they fail. DaemonSet also supports rolling updates to roll out a new pod template gradually.
You can use node selectors to control which nodes the DaemonSet schedules pods onto. Readiness checks can ensure the pods are ready before scaling up. Tolerations allow scheduling onto tainted nodes. Host networking and host mount volumes give the DaemonSet pods access to the node.
DaemonSets provide a simple Kubernetes native way to ensure your daemon runs on all or specified nodes in a cluster. The controller handles replicating and scheduling the pods for you.
Usage
Some common use cases for DaemonSets:
Example
Here is an example DaemonSet definition:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd
spec:
selector:
matchLabels:
name: fluentd
template:
metadata:
labels:
name: fluentd
spec:
containers:
- name: fluentd
image: fluent/fluentd
This will create a fluentd pod on each node in the cluster.
The spec.selector matches pods with a name: fluentd label. The spec.template defines the pod to create for each node.
To create:
kubectl apply -f fluentd-daemonset.yaml
DaemonSets also support rolling updates to rollout pod template changes gradually.
Tips
Kubectl CLI Commands
Here are some common kubectl CLI commands for working with DaemonSets:
Create a DaemonSet:
领英推荐
kubectl apply -f daemonset.yaml
List DaemonSets:
kubectl get daemonsets
Describe a DaemonSet:
kubectl describe daemonset my-daemonset
Delete a DaemonSet:
kubectl delete daemonset my-daemonset
Rollout an update to a DaemonSet:
kubectl set image daemonset my-daemonset mycontainer=newimage
Check rollout status:
kubectl rollout status daemonset my-daemonset
See pods created by a DaemonSet:
?kubectl get pods -l name=mydaemonset
Drain a node to remove DaemonSet pods:
?kubectl drain my-node --ignore-daemonsets
Cordon a node to stop scheduling new pods:
kubectl cordon my-node
Use kubectl apply/get/describe/delete to manage DaemonSets, kubectl set image and rollout to update them, and get pods/drain/cordon to manage DaemonSet pods.
Conclusion
DaemonSets are a powerful Kubernetes feature that ensures your pods run on all or specified nodes in a cluster. They are perfect for deploying daemons and services that need to run everywhere.
Now you know how to use YAML definitions to specify a DaemonSet. The controller will handle scheduling pods and cleaning them up when nodes are added or removed.
You also learned how to roll out updates, check status, and manage DaemonSet pods using kubectl. Useful techniques like node selectors, pod readiness, and tolerations help run Daemons reliably.
With this knowledge, you can deploy important cluster services using DaemonSets. They will run consistently regardless of changes to nodes. DaemonSets simplify the cluster-wide deployment and lifecycle management of your critical daemon workloads.
?
?