Lesson 2.4: DaemonSets and StatefulSets
A DaemonSet is a Kubernetes controller that ensures a copy of a specific Pod runs on all or some nodes in a cluster. It is commonly used for system-level services that need to run on every node, such as:
- Logging agents (e.g., Fluentd, Logstash)
- Monitoring agents (e.g., Prometheus Node Exporter)
- Network plugins (e.g., Calico, Weave)
- Storage daemons (e.g., GlusterFS, Ceph)
Key Features of DaemonSets
- One Pod Per Node:
- A DaemonSet ensures that each node in the cluster runs exactly one instance of the specified Pod.
- If a new node is added to the cluster, the DaemonSet automatically schedules a Pod on that node.
- If a node is removed, the DaemonSet deletes the Pod from that node.
- Node Selectors:
- You can use node selectors or affinity/anti-affinity rules to control which nodes the DaemonSet should run on.
- For example, you can run a DaemonSet only on nodes with a specific label (e.g., disk=ssd).
- Taints and Tolerations:
- DaemonSets can work with tainted nodes by adding tolerations to the Pod spec.
- This allows DaemonSet Pods to run on nodes that are otherwise restricted.
- Rolling Updates:
- DaemonSets support rolling updates, allowing you to update the Pod template in a controlled manner.
- Automatic Scaling:
- DaemonSets automatically scale with the cluster. When nodes are added or removed, the DaemonSet adjusts the number of Pods accordingly.
How DaemonSets Work
- Pod Template:
- A DaemonSet defines a Pod template, similar to a Deployment or ReplicaSet.
- This template is used to create Pods on each node.
- Node Selection:
- The DaemonSet controller identifies nodes that match the specified criteria (e.g., node labels, taints,and tolerations).
- It then creates or deletes Pods on those nodes as needed.
- Pod Management:
- The DaemonSet ensures that the desired number of Pods (one per node) is always running.
- If a Pod is deleted, the DaemonSet recreates it.
[root@master daemonset]# cat ds.yml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx-deploy
labels:
env: demo
spec:
template:
metadata:
name: nginx
labels:
env: demo
spec:
containers:
- name: nginx
image: nginx:1.27.4-alpine
selector:
matchLabels:
env: demo
[root@master daemonset]# kubectl apply -f ds.yml
daemonset.apps/nginx-deploy created
[root@master ~]# kubectl get ds
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
nginx-deploy 2 2 2 2 2 <none> 13m
[root@master daemonset]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
cka-cluster2-control-plane Ready control-plane 26h v1.29.14
cka-cluster2-worker Ready <none> 26h v1.29.14
cka-cluster2-worker2 Ready <none> 26h v1.29.14
[root@master daemonset]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deploy-4lnck 1/1 Running 0 16s
nginx-deploy-cc2tw 1/1 Running 0 16s
[root@master daemonset]# kubectl describe pod nginx-deploy-4lnck | grep Node:
Node: cka-cluster2-worker2/172.18.0.5
[root@master daemonset]# kubectl describe pod nginx-deploy-cc2tw | grep Node:
Node: cka-cluster2-worker/172.18.0.4