DaemonSet 確保全部(或者一些)Node 上運行一個 Pod 的副本。當有 Node 加入集羣時,也會爲他們新增一個 Pod 。當有 Node 從集羣移除時,這些 Pod 也會被回收。刪除 DaemonSet 將會刪除它創建的所有 Pod。 使用 DaemonSet 的一些典型用法:
- 運行集羣存儲 daemon,例如在每個 Node 上運行 glusterd、ceph。
- 在每個 Node 上運行日誌收集 daemon,例如filebeat、logstash。
- 在每個 Node 上運行監控 daemon,例如 Prometheus Node Exporter、collectd、Datadog 代理、Zabbix Agent。
創建DaemonSet
DaemonSet的描述文件和Deployment非常相似,只需要修改Kind,並去掉副本數量的配置即可
[root@k8s-01 ~]# cat nginx-daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx-daemonset
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.13.12
ports:
- containerPort: 80
使用kubectl創建DaemonSet
[root@k8s-01 ~]# kubectl create -f nginx-daemonset.yaml
daemonset.apps/nginx-daemonset created
查看Pod在Node上的分佈
[root@k8s-01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-daemonset-5x66f 1/1 Running 0 2m13s 172.30.88.2 k8s-03 <none> <none>
nginx-daemonset-v62tc 1/1 Running 0 2m13s 172.30.64.3 k8s-01 <none> <none>
nginx-daemonset-vmrjh 1/1 Running 0 2m13s 172.30.224.3 k8s-02 <none> <none>
[root@k8s-01 ~]# kubectl get daemonset
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
nginx-daemonset 3 3 3 3 3 <none> 3m