02 - DaemonSets

DaemonSet

DaemonSet 是 Kubernetes 中的一種資源類型,用來確保每個節點 (Node) 都執行「一個」特定的 Pod。這通常用於執行節點級別的系統服務,例如:

  • 日誌收集 (如 Fluentd、Filebeat)
  • 監控代理 (如 Prometheus Node Exporter)
  • 網路代理 (如 Cilium, Calico, Istio)


DaemonSet 的特性

✅ 確保所有符合條件的節點都會執行一個 Pod
✅ 當有新節點加入 (或移除) 叢集時,自動新增 (或刪除) Pod
支援節點選擇器 (Node Selector)、親和性 (Affinity) 及 污點與容忍度 (Taints & Tolerations),可精細控制 Pod 的調度策略
不需要手動擴展副本數 (replicas),它會根據節點數量自動調整

摘要:DaemonSet vs Deployment


何時使用 DaemonSet?

確保每個節點(Node)都需要運行一個同樣的 Pod (如 監控、日誌收集、網路代理)
✅ 當 Pod 需要存取主機資源 (如 hostPath 掛載 /var/log)
✅ 當 Pod 必須與節點一起運行,不受負載均衡影響

  • 如果你的應用是 水平擴展 (Scaling out) 的服務,應該選擇使用 Deployment
  • 如果你要在 每個節點上運行一個 Pod,那就選擇 DaemonSet


$ kubectl get daemonsets --all-namespaces             
$ kubectl get daemonsets -A                                   


Q: On how many nodes are the pods scheduled by the DaemonSet `kube-proxy`?

$ kubectl describe daemonset kube-proxy --namespace=kube-system        
$ kubectl describe daemonset kube-proxy
-n kube-system                         

$ kubectl get daemonset --namespace=kube-system                                 


Q: What is the image used by the POD deployed by the `kube-flannel-ds` DaemonSet?

$ kubectl get all -A                                                                            

$ kubectl describe daemonset kube-flannel-ds -n kube-flannel              
$ kubectl describe
ds kube-flannel-ds -n kube-flannel                          


Q: Deploy a DaemonSet for `FluentD` Logging.

  • Name: elasticsearch
  • Namespace: kube-system
  • Image: registry.k8s.io/fluentd-elasticsearch:1.20

Deployment 和 DaemonSet 的 YAML 格式很像,故使用下面指令產出檔案後,再進行修改即可(刪除 replicas、strategy 和 status,以及修改為 kind: DaemonSet)。

$ kubectl create deployment elasticsearch --image=registry.k8s.io/fluentd-elasticsearch:1.20 -n kube-system --dry-run=client -o yaml > fluentd.yaml            

Next, remove the replicas, strategy and status fields from the YAML file using a text editor. Also, change the kind from `Deployment` to `DaemonSet`.

```
apiVersion: apps/v1
kind: DaemonSet
metadata:
  creationTimestamp: null
  labels:
    app: elasticsearch
  name: elasticsearch
  namespace: kube-system
spec:
  selector:
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: elasticsearch
    spec:
      containers:
      - image: registry.k8s.io/fluentd-elasticsearch:1.20
        name: fluentd-elasticsearch
        resources: {}
```

下面指令,二擇一執行:

$ kubectl create -f fluentd.yaml                     
$ kubectl apply -f fluentd.yaml                       

留言