Fluentd:記錄彙總器,兩個目標
這個範例將一個 Fluentd 記錄彙總器,也就是記錄管線的接收端,從一個由開發者撰寫的 Aggregate 編譯為兩種實現:
- 用於本機開發的 Docker Compose,以及
- 合成式 helm Recipe,直接在 Kubernetes 上執行官方上游的
fluent/fluentd映像檔(沒有外部 chart)。
合成式 helm Recipe 在建置時不擷取任何東西;helm/fluentd-chart 則改為將 Fluent 專案自己的 chart 渲染為同一個彙總器,供能連到 chart 存放庫的建置使用。
同一個 aggregate.yaml 驅動兩者。每種實現只有 Manifest 所選的 Recipe 層會改變。
彙總器角色
記錄管線有兩端。轉送器(例如 Fluent Bit)收集記錄,並將它們往下游傳送。彙總器是另一端的接收端:它在 forward 輸入通訊協定上監聽、接收上游轉送器傳來的記錄,並將它們路由到它的輸出。這裡的 Fluentd 扮演彙總器的角色。
因為彙總器是接收而不是轉送,所以它不宣告下游的 uses 插槽。它改為透過 provides 匯出其 forward 端點的主機與連接埠,讓轉送器能在 forward 插槽上繫結它,並將記錄傳送給它。forward 端點在連接埠 24224 上監聽,這是慣用的 Fluentd forward 連接埠。
檔案
| 檔案 | 角色 |
|---|---|
aggregate.yaml | 開發者意圖:一個在 forward 輸入上監聽的 aggregator 元件(core/observability/fluentd)。 |
manifest-compose.yaml | 將 Fluentd 型別對應到 Compose Recipe(compose/fluentd)。 |
manifest-helm-synth.yaml | 將 Fluentd 型別對應到合成式 helm Recipe(helm/fluentd)。 |
release-compose.yaml / release-helm-synth.yaml | 兩種實現的操作員記錄。 |
release-helm-synth-buffer.yaml | 啟用持久緩衝的合成 helm 實現(已設定 infra.storage.size)。 |
因為 Compose 與合成式 helm Recipe 都帶有名稱 fluentd,所以 Manifests 以提供者範圍將它們參照為 compose/fluentd 與 helm/fluentd。
它示範的內容
- 一個撰寫介面,多個目標。 單一 Aggregate 是堆疊是什麼的事實來源。每個 Manifest 說明如何實現它。
- 字串管道(String Pipe)的接收端。 彙總器透過
provides匯出其 forward 端點:host(彙總器的服務)與forward通訊協定上的port(24224)。繫結這個元件的轉送器會將這些字串傳遞到自己的 forward 輸出;編譯器路由它們而不解讀它們。 - Workload 合成(Kubernetes)。 合成式 helm Recipe 宣告一個 Workload,也就是執行官方
fluent/fluentd映像檔的 Deployment 加上 ClusterIP Service,由 helm 提供者轉譯為 Kubernetes 資訊清單,不使用外部 chart。渲染出的資源集合正好就是該 Workload 所宣告的內容。 - 由開發者控制的路由。 開發者的輸出比對條件會選擇哪些收到的記錄被路由到彙總器的輸出。它預設為
**,會路由每一筆收到的記錄。 - 預設為暫時性緩衝,依要求提供持久性。 彙總器在記憶體中緩衝,所以預設渲染不帶有持久性磁碟區。設定
infra.storage.size(見release-helm-synth-buffer.yaml)會以掛載在/fluentd/log的持久性磁碟區支撐緩衝;工作負載仍是單一複本的 Deployment。 - 可重現的映像檔。
infra.engine_version固定 Fluentd 映像檔標籤,所以渲染出的工作負載參照固定的映像檔,合成式 Recipe 也不渲染浮動標籤。
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example fluentd-aggregator它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd fluentd-aggregator建置
此工作區沒有 .aggrega-config.yaml,因此標準程式庫實體從 hub 登錄庫 hub.aggrega.ai 解析,不需要目錄旗標;--auto-pull 會在建置期間寫入全新複製的副本所缺少的鎖定檔:
# Local development (Docker Compose)aggrega build --release release-compose.yaml --auto-pull
# Kubernetes (synthesized helm Recipe, official image)aggrega build --release release-helm-synth.yaml --auto-pull
# Kubernetes with a durable buffer volumeaggrega build --release release-helm-synth-buffer.yaml --auto-pull渲染出的成品位於 dist/ 之下。
搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,不要自己建立任何資料夾。如果工作區資料夾中還沒有 `fluentd-aggregator` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `fluentd-aggregator`,且不指定 `dir`:它會將範例寫入一個新的 `fluentd-aggregator` 資料夾。接著依下列順序為每個 Release 呼叫一次 `build` 工具:`release-compose.yaml`、`release-helm-synth.yaml`、`release-helm-synth-buffer.yaml`,每次都將 `workspace_root` 設為 `fluentd-aggregator`、`release` 設為該檔案、`auto_pull` 設為 true,並將 `write` 設為 true。告訴我每次建置寫出了哪些檔案;它們位於 `fluentd-aggregator/dist/` 之下。forward 端點是什麼樣子
在 Compose 上,彙總器會寫出 fluent.conf,其中有一個繫結到 forward 連接埠的 forward 輸入來源,並將相符的記錄路由到標準輸出:
<source> @type forward bind 0.0.0.0 port 24224</source><match **> @type stdout</match>在 Kubernetes 上,合成式 helm Recipe 會在啟動時寫出相同的 fluent.conf,而 ClusterIP Service 會公開 forward 連接埠:
spec: type: ClusterIP [… omitted: the selector matching the aggregator's Pods] ports: - name: forward port: 24224 targetPort: forwardDeployment 參照固定的映像檔,並在容器上公開 forward 連接埠:
image: fluent/fluentd:v1.19.3-debian-2.1[… omitted: the start-up command that writes the fluent.conf shown above]ports: - name: forward containerPort: 24224設定 infra.storage.size 時,Recipe 還會渲染一個 PersistentVolumeClaim 並將它掛載在 /fluentd/log,支撐檔案緩衝,使緩衝的記錄在重新啟動後仍然存在。
執行它(Compose)
aggrega build --release release-compose.yaml --auto-pulldocker compose -f dist/app/compose/docker-compose.yaml up -d彙總器在 localhost:24224 上監聽被轉送的記錄。以任何指向該連接埠的 Fluentd 或 Fluent Bit 轉送器傳送一筆測試記錄,收到的記錄就會出現在彙總器的標準輸出。以 docker compose -f dist/app/compose/docker-compose.yaml down 拆除。