DaemonSet 保證在每個 Node 上都運作一個容器副本,常用來部署一些叢集的日志、監控或者其他系統管理應用。下面以日志收集 fluentd 為例,看下如何使用阿裡雲容器服務控制台建立DaemonSet。
準備Kubernetes環境
在阿裡雲容器服務控制台中建立Kubernetes 叢集(1.11.5),3 master,3 worker
安裝fluentd
1、選擇應用->守護程序集->使用鏡像建立

填寫應用名稱,選擇部署叢集、命名空間,進入下一步
2、選擇鏡像并進行相應配置
注意:這裡挂載了配置項fluentd-conf,用來覆寫鏡像中的預設配置,需要提前建立出來,内容如下:
apiVersion: v1
kind: ConfigMap
metadata:
name: fluentd-conf
namespace: kube-system
data:
td-agent.conf: |
<match fluent.**>
type null
</match>
<source>
type tail
path /var/log/containers/*.log
pos_file /var/log/es-containers.log.pos
time_format %Y-%m-%dT%H:%M:%S.%NZ
tag kubernetes.*
format json
read_from_head true
</source>
<filter kubernetes.**>
type kubernetes_metadata
verify_ssl false
</filter>
否則會遇到pod 啟動問題
[error]: config error file="/etc/td-agent/td-agent.conf" error="Invalid Kubernetes API v1 endpoint https://172.21.0.1:443/api: SSL_connect returned=1 errno=0 state=error: certificate verify failed"
3、設定更新政策
可以在進階配置中選擇更新方式:
- 滾動更新(RollingUpdate):更新 DaemonSet 模版後,自動删除舊的 Pod 并建立新的 Pod
- 替換更新(OnDelete):更新模闆後,隻有手動删除了舊的 Pod 後才會建立新的 Pod
4、指定節點排程
隻選擇worker節點安裝。設定節點親和性如圖。
5、建立完成
點選建立,可以看到建立成功。
6、問題排查與更新
按着上述步驟可以看到在3個worker節點分别起了對應的pod,但pod并沒有成功啟動。選擇其中的一個容器,檢視一下日志發現如下錯誤:
config error file="/etc/td-agent/td-agent.conf" error="Exception encountered fetching metadata from Kubernetes API endpoint: pods is forbidden: User cannot list pods at the cluster scope"
Google後發現需要設定ClusterRole
apiVersion: v1
kind: ServiceAccount
metadata:
name: fluent-account
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: fluent-account
roleRef:
kind: ClusterRole
name: view
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
name: fluent-account
namespace: kube-system
建立成功後更新fluent-es 的yaml,編輯yaml,送出更新。
Pod啟動成功,日志已經可以正常采集了。
總結
使用阿裡雲容器服務控制台支援友善的建立DaemonSet,歡迎使用體驗。
https://cs.console.aliyun.com/