偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

EFK+DeepSeek 智能運維方案:技術(shù)架構(gòu)與實施步驟

原創(chuàng) 精選
人工智能
本文將圍繞 EFK(Fluent Bit + Fluentd + Elasticsearch + Kibana)結(jié)合 AI 大模型的日志智能運維方案展開。先利用 Fluent Bit 從指定位置獲取日志,依解析規(guī)則處理后推送給 Fluentd;Fluentd 監(jiān)聽日志,經(jīng)過濾器提取、解析并標(biāo)記異常,再輸出給 Elasticsearch 存儲,同時調(diào)用大模型處理服務(wù);Elasticsearch 存儲

作者 | 崔皓

審校 | 重樓

開篇

在日常 IT 運營場景里,EFK(Elasticsearch +Fluent Bit + Fluentd + Kibana )組合是日志管理與分析的經(jīng)典方案,被廣泛應(yīng)用于各類系統(tǒng)運維中。它憑借 Fluent Bit 輕量高效的日志采集能力,快速獲取多源日志;依托 Fluentd 靈活的日志處理機(jī)制,完成過濾、格式化等操作;借由 Elasticsearch 強(qiáng)大的分布式存儲與檢索特性,實現(xiàn)日志的高效存儲和快速查詢;再通過 Kibana 直觀的可視化界面,讓運維人員能清晰洞察日志數(shù)據(jù)背后的系統(tǒng)狀態(tài),以 “采集 - 處理 - 存儲 - 可視化” 的完整鏈路,助力運維團(tuán)隊及時發(fā)現(xiàn)系統(tǒng)問題,成為保障 IT 系統(tǒng)穩(wěn)定運行的有力工具。

然而,在實際運維過程中,面對日志里海量的錯誤異常信息,僅靠傳統(tǒng) EFK 方案仍存在短板。IT 運維人員受限于精力和時間,面對繁雜的異常日志,難以逐一對其深入拆解、分析根源,常常錯過最佳修復(fù)時機(jī),導(dǎo)致小故障演變成影響業(yè)務(wù)的大問題。為突破這一困境,我們嘗試引入 AI 運維模式,借助 AI 大模型對 EFK 采集到的異常信息進(jìn)行智能分析,讓機(jī)器替代人工完成繁瑣的異常診斷、根因定位等工作,以此提升運維效率與質(zhì)量。

如下圖所示,本文將圍繞 EFK(Fluent Bit + Fluentd + Elasticsearch + Kibana)結(jié)合 AI 大模型的日志智能運維方案展開。先利用 Fluent Bit 從指定位置獲取日志,依解析規(guī)則處理后推送給 Fluentd;Fluentd 監(jiān)聽日志,經(jīng)過濾器提取、解析并標(biāo)記異常,再輸出給 Elasticsearch 存儲,同時調(diào)用大模型處理服務(wù);Elasticsearch 存儲的數(shù)據(jù)可在 Kibana 可視化展示;大模型處理服務(wù)則對異常日志解析、分析并生成報表。以 CPU 使用率異常場景為例,演示從日志采集、處理到智能診斷的全流程。

安裝環(huán)境

工欲善其事必先利其器,在開始案例之前我們先把需要用到的應(yīng)用和環(huán)境安裝上,首先保證 docker 安裝完成,然后從文件夾到容器的安裝,按照如下流程進(jìn)行。

創(chuàng)建文件夾

準(zhǔn)備好 fluent-bit、fluentd 以及 web-logs 文件夾。分別用來放置容器的配置文件和對應(yīng)的日志文件。

請注意,我這里以/Users/cuihao/docker 為基礎(chǔ)目錄, 在這個目錄下創(chuàng)建文件夾和文件,大家可以按照自己的操作系統(tǒng)和目錄情況規(guī)劃目錄以及存放文件。

如下圖所示, 我們可以看到 fluent-bit、fluentd 以及 weblogs 三個目錄。稍后我們會分別在這三個目錄下面放置對應(yīng)應(yīng)用的配置文件,用來完成日志采集、分析、過濾、輸出等操作。

在完成文件夾的創(chuàng)建之后,接著在 fluent-bit 文件夾下創(chuàng)建 etc 目錄,后面會在 etc 下面創(chuàng)建 fluent-bit.conf 文件,用來配置日志采集的輸入和輸出信息。

完成 fluent-bit 文件夾創(chuàng)建之后,接著在 fluentd 文件夾下面創(chuàng)建 conf 文件夾,為 fluent.conf 的創(chuàng)建做好準(zhǔn)備。這里可以劇透一下,在fluent.conf 會有日志采集、過濾、標(biāo)記、調(diào)用智能報表等配置信息。

最后,就是保證創(chuàng)建一個 web-logs 目錄,下面的 metric_log 文件是我們用來模擬 CPU 使用率數(shù)據(jù)的日志文件。也是案例的起點, fluent-bit 會從這里采集數(shù)據(jù)。

創(chuàng)建 FluentD Docker 文件

完成目錄布局之后,我們大致知道完成案例大致需要的配置信息,接著在目錄/Users/cuihao/docker/EFK/fluentd/ 下創(chuàng)建 Dockerfile 文件。這個文件用來安裝 fluentd 的基礎(chǔ)鏡像以及對應(yīng)的插件。

# 使用的基礎(chǔ)鏡像
 FROM fluent/fluentd:edge-debian
 # 切換用戶為root方便接下來執(zhí)行安裝命令
 USER root
 # 安裝系統(tǒng)依賴
 RUN apt-get update && \
 apt-get install -y curl && \
 rm -rf /var/lib/apt/lists/*

 # 卸載可能存在的高版本 Elasticsearch gem
 RUN gem uninstall elasticsearch elasticsearch-api -a -x || true

 # 安裝指定版本 gem 和 Fluentd Elasticsearch 插件
 RUN gem install elasticsearch -v 8.17.1 --no-document && \
 gem install elasticsearch-api -v 8.17.1 --no-document && \
 gem install fluent-plugin-elasticsearch -v 5.4.3 --no-document
 USER fluent

上述 Dockerfile 文件以官方 fluentd 的 edge-debian 版本,在這個版本的基礎(chǔ)上 fluentd 還需要配置數(shù)據(jù)轉(zhuǎn)發(fā)或聚合操作,比如本例中需要轉(zhuǎn)發(fā)到ES,就需要安裝對應(yīng)的插件(fluent-plugin-elasticsearch)。這些插件就需要通過 Dockerfile 文件的方式安裝。

從文件中可以看到,首先切換到 root 用戶安裝系統(tǒng)依賴 curl,再清理可能存在的高版本 Elasticsearch 相關(guān)組件,隨后安裝 8.17.1 版本的 elasticsearch(注意這里使用的 ES 版本)、elasticsearch-api gem 包及 5.4.3 版本的 fluent-plugin-elasticsearch 插件,確保與目標(biāo) Elasticsearch 服務(wù)兼容,最后切換回 fluent 普通用戶以遵循最小權(quán)限原則,最終生成一個可直接用于 EFK 日志棧中收集并向 Elasticsearch 發(fā)送日志的定制化鏡像。

創(chuàng)建 EFK 組件docker-compose 文件

由于本案例需要安裝 fluent-bit、fluentd、elasticsearch、kibana 等應(yīng)用,為了方便安裝與調(diào)試,我們計劃使用 docker 方式對他們進(jìn)行安裝。于是 docker compose 的安裝方式就成了最佳選擇,它可以用于定義和管理多容器 Docker 應(yīng)用的 YAML 配置文件,能將多個關(guān)聯(lián)的容器(如應(yīng)用服務(wù)、數(shù)據(jù)庫、緩存等)的配置(鏡像、端口映射、數(shù)據(jù)卷、環(huán)境變量、依賴關(guān)系等)集中整合,通過 docker compose 命令一鍵實現(xiàn)多容器的創(chuàng)建、啟動、停止、重啟等操作。其核心益處在于簡化了多容器應(yīng)用的部署與管理流程,避免了手動逐個操作容器的繁瑣;通過統(tǒng)一配置文件確保了環(huán)境一致性,同時清晰的依賴關(guān)系定義保證了容器按正確順序啟動。

為了保證安裝的順利進(jìn)行我們選擇Fluentd官網(wǎng)的docker-compose 文件,并在其基礎(chǔ)上進(jìn)行修改,從而適應(yīng)安裝需求。

由于該文件內(nèi)容比較長,這里我們通過一張大圖將文件的內(nèi)容進(jìn)行描述,如下:

該文件用于搭建 EFK(Fluent Bit + Fluentd + Elasticsearch + Kibana)日志管理系統(tǒng)。通過定義 fluent-bit(輕量采集容器日志,依賴 fluentd 健康后啟動,配置掛載日志目錄與配置文件)、fluentd(基于自定義 Dockerfile 構(gòu)建,格式化日志,依賴 elasticsearch 健康后啟動,配置掛載、端口及健康檢查 )、elasticsearch(存儲日志,單節(jié)點模式、關(guān)閉安全功能,配置健康檢查與端口 )、kibana(可視化日志,依賴 elasticsearch 健康后啟動,映射 Web 端口 )四個服務(wù),利用 Docker Compose 實現(xiàn)多容器協(xié)同,讓日志從采集、處理、存儲到可視化全流程自動化部署與管理,各服務(wù)間通過健康檢查依賴保障啟動順序與運行狀態(tài),方便快速搭建日志分析環(huán)境。

文件內(nèi)容如下:

# 定義本文件中所有要啟動的服務(wù)(容器)
 services:
 # Fluent Bit:輕量收集容器日志
 # 【服務(wù)名】是的,'fluent-bit' 就是這個服務(wù)的名稱。它在 Docker Compose 網(wǎng)絡(luò)內(nèi)部被識別為此名。
 fluent-bit:
 # 使用的鏡像:從 Docker Hub 拉取最新的 Fluent Bit 官方鏡像
 image: fluent/fluent-bit:latest
 # 指定容器啟動后的名稱,通過 `docker ps` 等命令可以看到這個名字
 container_name: fluent-bit
 # 依賴關(guān)系:指定此服務(wù)的啟動依賴于另一個服務(wù) 'fluentd'
 depends_on:
 fluentd:
 # 條件:只有當(dāng) 'fluentd' 服務(wù)通過健康檢查(healthy)后,才會啟動 fluent-bit
 condition: service_healthy
 # 端口映射:將宿主機(jī)的端口映射到容器內(nèi)的端口
 # 格式 - "宿主機(jī)端口:容器端口"
 ports:
 - "2020:2020" # 將容器內(nèi)的 2020 端口(Fluent Bit 的 HTTP Server,常用于健康檢查或監(jiān)控)映射到宿主機(jī)的 2020 端口
 # 數(shù)據(jù)卷掛載:將宿主機(jī)的目錄或文件掛載到容器內(nèi),實現(xiàn)數(shù)據(jù)持久化或配置注入
 volumes:
 # 將宿主機(jī)的 '/Users/cuihao/docker/EFK/web-logs' 目錄掛載到容器內(nèi)的 '/var/log/' 目錄。
 # Fluent Bit 會監(jiān)控這個目錄下的日志文件變化,并收集新產(chǎn)生的日志。
 #/var/log/metric_log
 #/Users/cuihao/docker/EFK/web-logs
 - /Users/cuihao/docker/EFK/web-logs:/var/log/
 # 將宿主機(jī)的 Fluent Bit 主配置文件掛載到容器內(nèi),替代鏡像內(nèi)的默認(rèn)配置。
 # 這個文件定義了數(shù)據(jù)輸入(Input)、處理(Parser, Filter)和輸出(Output)的規(guī)則。
 - /Users/cuihao/docker/EFK/fluent-bit/etc/:/fluent-bit/etc/

 # Fluentd:格式化
 # 【服務(wù)名】'fluentd' 是這個服務(wù)的名稱。
 fluentd:
 container_name: fluent
 # 不是使用現(xiàn)成的鏡像,而是通過指定構(gòu)建上下文路徑(E:\EFK\fluentd)來構(gòu)建自定義鏡像。
 # 該路徑下應(yīng)該有一個名為 'Dockerfile' 的文件,默認(rèn)讀取 'Dockerfile' 文件。
 build: /Users/cuihao/docker/EFK/fluentd
 volumes:
 # 將宿主機(jī)上的 Fluentd 配置目錄掛載到容器內(nèi),使配置變更無需重新構(gòu)建鏡像。
 - /Users/cuihao/docker/EFK/fluentd/conf:/fluentd/etc
 depends_on:
 elasticsearch:
 condition: service_healthy
 ports:
 - "24224:24224" # Fluentd 默認(rèn)的 TCP 端口,用于接收來自 Fluent Bit 或其他客戶端轉(zhuǎn)發(fā)來的日志,這個端口會配置到fluent-bit的OUTPUT中
 - "24224:24224/udp" # Fluentd 默認(rèn)的 UDP 端口,用途同上
 - "24220:24220" # Fluentd 的健康檢查 API 端口
 # 健康檢查配置:Docker 會根據(jù)此規(guī)則判斷容器是否正常啟動
 healthcheck:
 test: ["CMD-SHELL", "curl -fs http://localhost:24220/api/plugins.json || exit 1"] # 檢查健康檢查端點是否返回成功
 interval: 5s # 每 5 秒檢查一次
 timeout: 3s # 每次檢查超時時間為 3 秒
 retries: 5 # 連續(xù)失敗 5 次才標(biāo)記為不健康
 start_period: 10s # 容器啟動后,等待 10 秒再進(jìn)行第一次健康檢查

 # Elasticsearch:存儲日志
 # 【服務(wù)名】'elasticsearch' 是這個服務(wù)的名稱。
 elasticsearch:
 image: docker.elastic.co/elasticsearch/elasticsearch:8.17.1 # 使用 Elastic 官方的 8.17.1 版本鏡像
 container_name: elasticsearch
 hostname: elasticsearch # 設(shè)置容器內(nèi)部的主機(jī)名,在集群中很有用
 environment:
 - discovery.type=single-node # 設(shè)置為單節(jié)點模式,適合開發(fā)和測試
 - xpack.security.enabled=false # 關(guān)閉 X-Pack 安全功能(用戶認(rèn)證、HTTPS等)。生產(chǎn)環(huán)境必須開啟,但測試時關(guān)閉更簡單。
 healthcheck:
 test: ["CMD", "curl", "-f", "http://localhost:9200/_cluster/health"] # 檢查 ES 集群健康狀態(tài) API
 interval: 10s
 retries: 5
 timeout: 5s
 ports:
 - "9200:9200" # 將 ES 的 HTTP REST API 端口映射到宿主機(jī),方便通過瀏覽器或命令訪問

 # Kibana:日志可視化
 # 【服務(wù)名】'kibana' 是這個服務(wù)的名稱。
 kibana:
 container_name: kibana
 image: docker.elastic.co/kibana/kibana:8.17.1 # Kibana 版本需要與 Elasticsearch 版本一致
 depends_on:
 elasticsearch:
 condition: service_healthy # 等待 Elasticsearch 健康后再啟動
 ports:
 - "5601:5601" # 將 Kibana 的 Web 界面端口映射到宿主機(jī),通過 http://localhost:5601 訪問

啟動 EFK 組件

有了前面的準(zhǔn)備, EFK 都通過 Docker 鏡像的方式進(jìn)行了定義,接著只需要執(zhí)行 docker compose 命令就可以安裝了。

執(zhí)行如下命令啟動容器安裝 EFK 組件:

docker compose -f /Users/cuihao/docker/EFK/docker-compose.yml up -d

如果出現(xiàn)如下錯誤:

=> ERROR [fluentd internal] load metadata for docker.io/fluent/fluentd:edge-debian 31.1s
 ------
 > [fluentd internal] load metadata for docker.io/fluent/fluentd:edge-debian:
 ------
 failed to solve: DeadlineExceeded: DeadlineExceeded: DeadlineExceeded: fluent/fluentd:edge-debian: failed to resolve source metadata for docker.io/fluent/fluentd:edge-debian: failed to authorize: DeadlineExceeded: failed to fetch anonymous token: Get "https://auth.docker.io/token?scope=repository%3Afluent%2Ffluentd%3Apull&service=registry.docker.io": dial tcp 75.126.124.162:443: i/o timeout

說明需要手動拉取 fluentd 的鏡像,執(zhí)行如下命令:

docker pull fluent/fluentd:edge-debian

再次執(zhí)行如下命令:

docker compose -f /Users/cuihao/docker/EFK/docker-compose.yml up -d

完成安裝之后,可以通過 docker desktop 看到容器服務(wù)正常運行。如下圖所示,

配置日志采集與分析

完成 EFK 的安裝之后,接下來就開始應(yīng)用之間的配置了。在配置之前,我們先回顧一下案例的整體思路,fluent-bit 是日志采集的第一步,它會從日志文件中采集日志的信息,這里需要定義日志目錄。如圖所示,綠色區(qū)域中我們需要配置“日志目錄”,同時還需要制定fluent-bit 采集之后需要將日志信息輸出到 fluentd 中,這里需要填入“fluentd”作為輸出的服務(wù)名。

配置 Fluent Bit

從上面的描述,我們清楚需要對 fluent-bit 的輸入和輸出進(jìn)行配置,接下來就是編寫配置文件了。在/Users/cuihao/docker/EFK/fluent-bit/etc 創(chuàng)建 fluent-bit.conf 文件如下:

##############################################
 # 輸入插件配置(收集日志)
 ##############################################

 # 輸入源1:采集cpu日志文件
 [INPUT]
 # 使用 tail 插件監(jiān)控文件變化
 Name tail
 # 自定義標(biāo)簽,標(biāo)識為系統(tǒng)指標(biāo)類日志
 Tag sys.metric 
 # 系統(tǒng)指標(biāo)日志文件路徑(可能是由其他工具生成的指標(biāo)數(shù)據(jù))
 Path /var/log/metric_log
 # 檢查文件變化的間隔時間(秒)
 Refresh_Interval 10 

 ##############################################
 # 輸出插件(轉(zhuǎn)發(fā)到 Fluentd)
 ##############################################
 [OUTPUT]
 # 使用 forward 插件將日志轉(zhuǎn)發(fā)到Fluentd聚合器
 Name forward
 # 匹配所有標(biāo)簽的日志(* 是通配符,表示所有輸入源)
 Match *
 # Fluentd 服務(wù)地址(使用Docker Compose服務(wù)名進(jìn)行服務(wù)發(fā)現(xiàn))
 Host fluentd
 # Fluentd 監(jiān)聽端口(forward插件的默認(rèn)端口)
 Port 24224
 # 網(wǎng)絡(luò)故障時的最大重試次數(shù),防止無限重試消耗資源
 Retry_Limit 10

 # 輸出源2:同時輸出到控制臺(用于調(diào)試和監(jiān)控)
 [OUTPUT]
 # 使用 stdout 插件在控制臺打印日志
 Name stdout
 # 匹配所有標(biāo)簽的日志
 Match *
 # 注意:生產(chǎn)環(huán)境通常應(yīng)注釋或移除此輸出,避免日志重復(fù)和性能開銷

由于文件中的配置信息都用了備注,比較容易理解就不逐一解釋了。需要注意的是配置文件中的 Host 對應(yīng)的 fluentd 是 docker 容器的服務(wù)名,由于這幾個應(yīng)用是通過 docker compose 安裝的能夠保證在相同的網(wǎng)絡(luò)內(nèi),所以可以通過服務(wù)名進(jìn)行訪問,通過下圖的方式查詢。

配置 Fluentd

完成 fluent bit 的配置之后,緊接著就需要對 fluentd 進(jìn)行配置了,它是整個配置環(huán)節(jié)的重頭戲,這里還是以案例整體大圖為例。將 fluentd 的部分進(jìn)行展開說明, 如下圖所示。Fluent Bit 從日志文件獲取日志并推送給 Fluentd,F(xiàn)luentd 經(jīng) 24224 端口監(jiān)聽接收。先由過濾器按規(guī)則提取日志(過濾指定 Tag、正則提取 JSON )、解析日志(自定義 JSON 解析器處理原始日志),若 CPU 使用率超 80% 則標(biāo)記異常;接著通過多路輸出,將日志存至 Elasticsearch、輸出到控制臺,還會對標(biāo)記異常的日志,調(diào)用大模型處理服務(wù)(process_log.py )進(jìn)一步分析。

在/Users/cuihao/docker/EFK/fluentd/conf 目錄下創(chuàng)建 fluent.conf 文件,內(nèi)容如下:

###############################
 # Fluentd 健康檢查(monitor_agent)
 ###############################
 <source>
 @type monitor_agent # 啟用監(jiān)控代理插件,用于收集Fluentd自身運行指標(biāo)
 bind 0.0.0.0 # 監(jiān)聽所有網(wǎng)絡(luò)接口
 port 24220 # 監(jiān)控服務(wù)端口,可通過此端口獲取Fluentd運行狀態(tài)信息
 </source>

 ###############################
 # Fluentd 日志輸入(forward)
 ###############################
 <source>
 @type forward # 啟用forward輸入插件,接收來自Fluent-bit或其他Fluentd節(jié)點的日志
 port 24224 # 轉(zhuǎn)發(fā)協(xié)議監(jiān)聽端口
 bind 0.0.0.0 # 監(jiān)聽所有網(wǎng)絡(luò)接口
 </source>

 ###############################
 # 處理 collectd 采集的CPU指標(biāo)數(shù)據(jù)
 ###############################
 # 第一步:提取log字段中write_log后的JSON部分(現(xiàn)有配置)
 <filter sys.metric> # 過濾系統(tǒng)指標(biāo)數(shù)據(jù)
 @type parser # 使用解析器插件
 key_name log # 解析log字段內(nèi)容
 reserve_data true # 保留所有原始字段
 remove_key_name_field false # 不移除原始的key_name字段
 <parse>
 @type regexp # 使用正則表達(dá)式解析
 # 正則表達(dá)式匹配collectd的CPU數(shù)據(jù)格式,使用命名捕獲組提取CPU信息
 expression /write_log values:(?:#012|\s)*(?<log>\[\{.*?\}\])/ # 非貪婪匹配確保只取一個數(shù)組

 time_key time # 指定時間字段
 time_type float # 時間格式為浮點數(shù)(Unix時間戳)
 </parse>
 </filter>

 # 第二步:將log字段的JSON字符串解析為JSON對象
 <filter sys.metric>
 @type parser
 key_name log # 解析第一步提取的log字段(此時是JSON字符串)
 reserve_data true # 保留所有字段
 remove_key_name_field false # 保留解析后的log字段
 <parse>
 @type json # 用JSON解析器處理
 json_parser json # 使用默認(rèn)JSON解析器
 array true # 強(qiáng)制將JSON數(shù)組字符串解析為數(shù)組對象
 </parse>
 </filter>

 # 第三步:標(biāo)記異常日志(增加詳細(xì)調(diào)試字段)
 <filter sys.metric>
 @type record_transformer
 enable_ruby true
 <record>
 # 直接判斷 values[0] 是否大于 80
 status ${(record["plugin"] == "cpu" && record["type"] == "percent" && record["values"].is_a?(Array) && !record["values"].empty? && record["values"][0].to_f > 80) ? "abnormal" : "normal"}

 debug_plugin ${record["plugin"].to_s}
 debug_type ${record["type"].to_s}
 debug_values_content ${record["values"].inspect}
 </record>
 </filter>

 ###############################
 # 輸出到 Elasticsearch 8、stdout 和 HTTP端點
 ###############################
 <match *.**> # 匹配所有標(biāo)簽的日志Tag
 @type copy # 復(fù)制插件,將日志同時發(fā)送到多個輸出目的地

 # 第一個輸出目標(biāo):Elasticsearch
 <store>
 @id es_output
 @type elasticsearch # 輸出到Elasticsearch
 host elasticsearch # ES主機(jī)地址(可以是主機(jī)名或IP)
 port 9200 # ES服務(wù)端口
 scheme http # 使用HTTP協(xié)議
 logstash_format true # 使用Logstash格式索引命名
 logstash_prefix fluentd-${tag} # 索引前綴加上標(biāo)簽名
 logstash_dateformat %Y%m%d # 索引日期格式年月日
 include_tag_key true # 在輸出中包含標(biāo)簽字段
 tag_key @log_name # 標(biāo)簽字段的鍵名
 flush_interval 1s # 刷新間隔1秒
 </store>

 <store>
 @id output
 @type stdout # 輸出到控制臺/stdout
 key status # 字段
 pattern ^normal$ # 正則匹配
 </store>

 <store>
 @type relabel # 使用 relabel 輸出插件進(jìn)行標(biāo)簽重路由
 @label @abnormal # 將所有匹配到的數(shù)據(jù)重新路由到 @abnormal 標(biāo)簽處理流程
 </store>
 </match>


 ###############################
 # 異常日志輸出到 HTTP端點
 ###############################
 <label @abnormal>
 # 這個 filter 塊會對所有進(jìn)入 @abnormal 標(biāo)簽的數(shù)據(jù)進(jìn)行過濾
 # 只有通過這個過濾器的數(shù)據(jù)才會繼續(xù)流向后面的 match 塊
 <filter **>
 @type grep # 使用 grep 過濾器
 <regexp>
 key status # 檢查每條記錄的 status 字段
 pattern ^abnormal$ # 只保留 status 值為 "abnormal" 的記錄
 </regexp>
 </filter>

 # 這個 match 塊會接收到經(jīng)過上面 filter 過濾后的數(shù)據(jù)
 <match **>
 @id http_output
 @type http # HTTP輸出插件
 endpoint http://host.docker.internal:5001/analyze-fluentd-log # 日志分析API端點,host.docker.internal是Docker特殊主機(jī)名,指向宿主機(jī)
 http_method post # 使用POST方法發(fā)送

 <format>
 @type json # 數(shù)據(jù)格式為JSON
 </format>

 <buffer>
 # 緩沖配置區(qū)塊:控制數(shù)據(jù)如何緩沖和重試發(fā)送
 # 用于提高網(wǎng)絡(luò)輸出的可靠性和性能,避免頻繁的小數(shù)據(jù)包發(fā)送
 
 # 緩沖刷新間隔:每2秒強(qiáng)制刷新一次緩沖區(qū)
 # - 即使緩沖區(qū)未滿,也會每2秒發(fā)送一次累積的數(shù)據(jù)
 # - 平衡實時性和網(wǎng)絡(luò)效率:太短會增加網(wǎng)絡(luò)請求,太長會降低實時性
 flush_interval 2s
 
 # 重試策略:使用指數(shù)退避算法
 # - 第一次重試等待:基礎(chǔ)等待時間
 # - 第二次重試等待:基礎(chǔ)時間 × 2
 # - 第三次重試等待:基礎(chǔ)時間 × 4,依此類推
 # - 避免網(wǎng)絡(luò)擁塞時的大量重試導(dǎo)致雪崩效應(yīng)
 retry_type exponential_backoff
 
 # 基礎(chǔ)重試等待時間:第一次重試前等待1秒
 # - 首次重試的初始等待間隔
 # - 后續(xù)重試會根據(jù)指數(shù)退避算法遞增
 retry_wait 1s
 
 # 最大重試間隔:單次重試最多等待30秒
 # - 防止指數(shù)增長后的等待時間過長
 # - 即使使用指數(shù)退避,也不會超過30秒的間隔
 retry_max_interval 30s
 
 # 總重試超時時間:10分鐘后放棄重試
 # - 從第一次失敗開始計算,10分鐘后停止重試
 # - 防止因為長期不可用的目標(biāo)服務(wù)導(dǎo)致無限重試
 # - 超時后數(shù)據(jù)可能會被丟棄(取決于配置)
 retry_timeout 10m
 </buffer>
 </match>
 </label>

生成智能報表

在完成配置 fluent bit 與 fluentd 的配置之后,我們來到生成智能報表的環(huán)節(jié), 這里需要根據(jù) fluentd 傳入的告警信息(CPU 使用率>80%),進(jìn)行分析并生成報表。

在/Users/cuihao/docker/EFK/目錄下創(chuàng)建process_log.py 文件, 寫入如下內(nèi)容:

from flask import Flask, request, jsonify, Response
 from openai import AsyncOpenAI
 import os
 import json
 import threading
 import asyncio
 from datetime import datetime
 from dotenv import load_dotenv

 # 加載環(huán)境變量
 load_dotenv()

 # 創(chuàng)建Flask應(yīng)用實例
 app = Flask(__name__)

 # 初始化客戶端
 DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
 if not DEEPSEEK_API_KEY:
 raise ValueError("未找到DEEPSEEK_API_KEY,請在.env文件中配置")

 client = AsyncOpenAI(
 api_key=DEEPSEEK_API_KEY,
 base_url="https://api.deepseek.com"
 )

 # 路徑配置
 current_dir = os.path.dirname(os.path.abspath(__file__))
 report_dir = os.path.join(current_dir, "report")
 OUTPUT_REPORT_PATH = os.path.join(report_dir, "incident_report.json")
 ERROR_INFO_PATH = os.path.join(current_dir, "api_error.log")
 OPERATION_LOG_PATH = os.path.join(current_dir, "api_operation.log") # 新增操作日志文件
 os.makedirs(report_dir, exist_ok=True)


 def write_operation_log(message, request_id=None):
 """記錄操作日志,包含時間戳和可選的請求ID"""
 timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] # 精確到毫秒
 request_id = request_id or "N/A"
 log_entry = f"[{timestamp}] [REQUEST={request_id}] OPERATION: {message}\n"
 try:
 with open(OPERATION_LOG_PATH, 'a', encoding='utf-8') as f:
 f.write(log_entry)
 print(log_entry.strip()) # 同時輸出到控制臺
 except Exception as e:
 print(f"[日志系統(tǒng)錯誤] 寫入操作日志失敗: {str(e)} | 原始消息: {message}")


 def write_error_log(error_message, request_id=None):
 """記錄錯誤日志,包含時間戳和請求ID"""
 timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
 request_id = request_id or "N/A"
 log_entry = f"[{timestamp}] [REQUEST={request_id}] ERROR: {error_message}\n"
 try:
 with open(ERROR_INFO_PATH, 'a', encoding='utf-8') as f:
 f.write(log_entry)
 print(log_entry.strip()) # 同時輸出到控制臺
 except Exception as e:
 print(f"[日志系統(tǒng)錯誤] 寫入錯誤日志失敗: {str(e)} | 原始錯誤: {error_message}")


 def append_to_json_file(data, timestamp, request_id):
 """將數(shù)據(jù)追加到JSON文件,帶日志記錄"""
 try:
 write_operation_log("開始寫入分析報告", request_id)

 data_with_timestamp = {
 "timestamp": timestamp,
 "request_id": request_id, # 新增請求ID便于追蹤
 **data
 }

 existing_data = []
 if os.path.exists(OUTPUT_REPORT_PATH):
 try:
 with open(OUTPUT_REPORT_PATH, 'r', encoding='utf-8') as f:
 existing_data = json.load(f)
 write_operation_log(f"成功讀取現(xiàn)有報告({len(existing_data)}條記錄)", request_id)
 except (json.JSONDecodeError, FileNotFoundError):
 error_msg = "報告文件損壞或不存在,將重新初始化"
 write_error_log(error_msg, request_id)
 existing_data = []

 existing_data.append(data_with_timestamp)

 with open(OUTPUT_REPORT_PATH, 'w', encoding='utf-8') as f:
 json.dump(existing_data, f, ensure_ascii=False, indent=2)

 write_operation_log("分析報告寫入成功", request_id)
 return True
 except Exception as e:
 error_msg = f"寫入JSON報告文件時出錯: {str(e)}"
 write_error_log(error_msg, request_id)
 return False


 async def process_llm_background(log_content, call_timestamp, request_id):
 """后臺處理LLM調(diào)用,帶詳細(xì)日志"""
 try:
 write_operation_log("開始后臺LLM處理流程", request_id)
 
 # 構(gòu)造提示詞
 write_operation_log("開始構(gòu)建提示詞", request_id)
 prompt = f"""
 你是一名系統(tǒng)運維專家。以下是異常日志,請生成根因分析報告:
 日志內(nèi)容:
 {log_content}

 請輸出格式:
 【標(biāo)題】
 ...
 【異常原因分析】
 ...
 【修復(fù)建議】
 ...
 """
 write_operation_log("提示詞構(gòu)建完成,準(zhǔn)備調(diào)用LLM", request_id)

 # 調(diào)用LLM
 write_operation_log("開始調(diào)用DeepSeek API", request_id)
 response = await client.chat.completions.create(
 model="deepseek-chat",
 messages=[
 {"role": "system", "content": "你是經(jīng)驗豐富的運維專家,負(fù)責(zé)分析日志并提供修復(fù)建議"},
 {"role": "user", "content": prompt}
 ],
 temperature=0.7,
 max_tokens=800,
 stream=False
 )
 write_operation_log("DeepSeek API調(diào)用成功", request_id)

 # 處理結(jié)果
 report_text = response.choices[0].message.content
 result_data = {
 "raw_log": log_content,
 "root_cause_report": report_text
 }
 write_operation_log("LLM返回結(jié)果解析完成", request_id)
 
 # 保存結(jié)果
 if append_to_json_file(result_data, call_timestamp, request_id):
 write_operation_log("分析結(jié)果已成功保存到報告文件", request_id)
 else:
 write_error_log("分析結(jié)果保存失敗", request_id)

 write_operation_log("后臺LLM處理流程完成", request_id)

 except Exception as e:
 error_msg = f"LLM調(diào)用失敗: {str(e)}"
 write_error_log(error_msg, request_id)


 def run_async_task(log_content, call_timestamp, request_id):
 """線程包裝器,帶日志"""
 try:
 write_operation_log("啟動異步任務(wù)處理線程", request_id)
 asyncio.run(process_llm_background(log_content, call_timestamp, request_id))
 except Exception as e:
 write_error_log(f"異步任務(wù)線程執(zhí)行失敗: {str(e)}", request_id)


 def build_log_content_from_records(records, request_id):
 """構(gòu)建日志內(nèi)容,帶日志記錄"""
 write_operation_log("開始構(gòu)建日志內(nèi)容", request_id)
 lines = []
 for i, record in enumerate(records or []):
 if isinstance(record, dict) and ("log" in record):
 value = record.get("log")
 if value is not None and str(value).strip() != "":
 lines.append(str(value))
 write_operation_log(f"成功提取第{i+1}條記錄的log字段", request_id)
 else:
 write_operation_log(f"第{i+1}條記錄的log字段為空,已忽略", request_id)
 else:
 write_operation_log(f"第{i+1}條記錄不包含log字段,已忽略", request_id)
 
 log_content = "\n".join(lines).strip()
 write_operation_log(f"日志內(nèi)容構(gòu)建完成,共包含{len(lines)}條有效記錄", request_id)
 return log_content


 @app.route('/analyze-fluentd-log', methods=['POST'])
 def analyze_fluentd_log():
 """主接口:帶詳細(xì)日志記錄的請求處理"""
 # 生成唯一請求ID,便于全流程追蹤
 request_id = datetime.now().strftime("%Y%m%d%H%M%S") + f"-{os.urandom(4).hex()}"
 write_operation_log("收到新的請求", request_id)

 try:
 # 1. 記錄請求基本信息
 call_timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
 write_operation_log(f"請求方法: {request.method}, 客戶端IP: {request.remote_addr}, Content-Type: {request.content_type}", request_id)

 # 2. 驗證請求類型
 if request.content_type != "application/x-ndjson":
 error_msg = f"不支持的Content-Type: {request.content_type},僅接受application/x-ndjson"
 write_error_log(error_msg, request_id)
 return jsonify({
 "error": error_msg,
 "request_id": request_id
 }), 415

 # 3. 解析NDJSON數(shù)據(jù)
 write_operation_log("開始解析NDJSON數(shù)據(jù)", request_id)
 ndjson_data = request.data.decode().splitlines()
 records = []
 for i, line in enumerate(ndjson_data):
 if line.strip():
 try:
 record = json.loads(line)
 records.append(record)
 write_operation_log(f"成功解析第{i+1}行NDJSON數(shù)據(jù)", request_id)
 except json.JSONDecodeError as e:
 error_msg = f"第{i+1}行NDJSON解析失敗: {str(e)}"
 write_error_log(error_msg, request_id)
 records.append({"message": line, "parse_error": str(e)})
 else:
 write_operation_log(f"第{i+1}行是空白行,已忽略", request_id)
 write_operation_log(f"NDJSON數(shù)據(jù)解析完成,共{len(records)}條記錄", request_id)

 # 4. 過濾有效日志記錄
 write_operation_log("開始過濾有效日志記錄", request_id)
 log_records = [r for r in records if isinstance(r, dict) and 'log' in r and str(r.get('log', '')).strip()]
 write_operation_log(f"有效日志記錄過濾完成: {len(log_records)}/{len(records)}", request_id)
 
 if not log_records:
 error_msg = "未找到有效日志記錄(缺少log字段)"
 write_error_log(error_msg, request_id)
 return Response(
 json.dumps({
 "status": "ignored",
 "reason": error_msg,
 "raw_records_count": len(records),
 "request_id": request_id
 }, ensure_ascii=False, indent=2),
 mimetype="application/json"
 )

 # 5. 構(gòu)建日志內(nèi)容
 log_content = build_log_content_from_records(log_records, request_id)
 if not log_content:
 error_msg = "日志內(nèi)容為空"
 write_error_log(error_msg, request_id)
 return jsonify({
 "error": error_msg,
 "request_id": request_id
 }), 400

 # 6. 參數(shù)驗證通過,準(zhǔn)備啟動后臺任務(wù)
 write_operation_log("參數(shù)驗證通過,準(zhǔn)備啟動后臺處理任務(wù)", request_id)

 # 7. 啟動后臺線程處理LLM調(diào)用
 thread = threading.Thread(
 target=run_async_task,
 args=(log_content, call_timestamp, request_id),
 daemnotallow=True
 )
 thread.start()
 write_operation_log("后臺處理線程已啟動", request_id)

 # 8. 立即返回響應(yīng)
 response_data = {
 "status": "accepted",
 "message": "請求已接收,正在后臺處理",
 "timestamp": call_timestamp,
 "log_records_count": len(log_records),
 "request_id": request_id # 返回請求ID便于追蹤
 }
 write_operation_log("請求處理完成,已返回響應(yīng)", request_id)

 return Response(
 json.dumps(response_data, ensure_ascii=False, indent=2),
 mimetype="application/json",
 status=202
 )

 except Exception as e:
 error_msg = f"參數(shù)驗證失敗: {str(e)}"
 write_error_log(error_msg, request_id)
 return jsonify({
 "error": error_msg,
 "request_id": request_id
 }), 400


 @app.route('/health', methods=['GET'])
 def health_check():
 """健康檢查接口,帶日志"""
 request_id = f"health-{datetime.now().strftime('%Y%m%d%H%M%S')}"
 write_operation_log("收到健康檢查請求", request_id)
 response = jsonify({
 "status": "healthy",
 "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
 "request_id": request_id
 })
 write_operation_log("健康檢查響應(yīng)已返回", request_id)
 return response


 if __name__ == '__main__':
 print("Flask API 啟動中...")
 print("環(huán)境變量配置:")
 print(f" - 從 .env 文件加載 DEEPSEEK_API_KEY: {'已配置' if DEEPSEEK_API_KEY else '未配置'}")
 print("日志文件路徑:")
 print(f" - 操作日志: {OPERATION_LOG_PATH}")
 print(f" - 錯誤日志: {ERROR_INFO_PATH}")
 print("可用的端點:")
 print(" - POST /analyze-fluentd-log - 接收日志并在后臺處理")
 print(" - GET /health - 服務(wù)健康檢查")
 app.run(host="0.0.0.0", port=5001, debug=True)

代碼是基于 Flask 框架的日志分析 API 服務(wù),用于接收并處理 EFK 體系中 Fluentd 推送的異常日志,結(jié)合 AI 大模型實現(xiàn)智能運維分析。代碼首先加載環(huán)境變量獲取 DeepSeek API 密鑰,初始化異步客戶端;通過定義日志記錄函數(shù),實現(xiàn)操作日志、錯誤日志的詳細(xì)追蹤,以及分析報告的 JSON 持久化存儲。核心接口/analyze-fluentd-log接收 NDJSON 格式的日志數(shù)據(jù),生成唯一請求 ID 用于全流程追蹤,解析日志后過濾出含有效log字段的記錄,構(gòu)建日志內(nèi)容并啟動后臺線程,異步調(diào)用 DeepSeek 大模型生成根因分析報告(含標(biāo)題、異常原因、修復(fù)建議),最終將結(jié)果寫入 JSON 文件。

測試

接下來需要將整個案例進(jìn)行測試,從日志文件生成到采集、分析、調(diào)用大模型。

日志生成與報表展示

由于需要生成測試的日志文件以及查看生成的智能日志,所以需要在 EFK 目錄下創(chuàng)建log_generator.py 和log_ui.py 分別完成上述功能。

import streamlit as st
 import json
 import re
 import os
 from log_generator import LogGenerator

 # 初始化日志生成器
 log_generator = LogGenerator()

 # ========================
 # 讀取 JSON 文件
 # ========================
 #file_path = r"report\incident_report.json"
 file_path = os.path.join("report", "incident_report.json")
 # 初始化reports變量
 reports = []

 # 檢查文件是否存在和讀取數(shù)據(jù)
 if os.path.exists(file_path) and os.path.getsize(file_path) > 0:
 try:
 with open(file_path, "r", encoding="utf-8") as f:
 reports = json.load(f)
 except json.JSONDecodeError as e:
 st.sidebar.error(f"JSON文件格式錯誤: {e}")
 except Exception as e:
 st.sidebar.error(f"讀取文件時發(fā)生錯誤: {e}")
 else:
 if not os.path.exists(file_path):
 st.sidebar.warning(f"?? 文件 {file_path} 不存在")
 else:
 st.sidebar.warning(f"?? 文件 {file_path} 為空")

 # ========================
 # 提取標(biāo)題(標(biāo)題在【標(biāo)題】后的下一行)
 # ========================
 def extract_title(report_text):
 # 去掉空行,逐行處理
 lines = [line.strip() for line in report_text.splitlines() if line.strip()]
 for i, line in enumerate(lines):
 if line.startswith("【標(biāo)題】"):
 # 找到【標(biāo)題】,取下一行作為真正的標(biāo)題
 if i + 1 < len(lines):
 return lines[i + 1].strip()
 else:
 return "未命名報告"
 return "未命名報告"

 # ========================
 # 按【字段】分段解析報告
 # 例如:分出 "異常原因分析"、"修復(fù)建議"、"異常日志"
 # ========================
 def split_sections(report_text):
 sections = {}
 # 用正則把【xxx】作為分隔符拆開
 parts = re.split(r"(【.*?】)", report_text)
 current_key = None
 for part in parts:
 if not part.strip():
 continue
 if part.startswith("【") and part.endswith("】"):
 # 當(dāng)前是一個小節(jié)標(biāo)題,例如【異常原因分析】
 current_key = part.strip("【】")
 sections[current_key] = ""
 else:
 # 當(dāng)前是小節(jié)內(nèi)容
 if current_key:
 sections[current_key] += part.strip() + "\n"
 return sections

 # ========================
 # 側(cè)邊欄:搜索功能
 # ========================
 st.sidebar.title("異常報告列表")

 # 如果沒有報告數(shù)據(jù),顯示提示信息
 if not reports:
 st.sidebar.info("?? 暫無報告數(shù)據(jù)")
 # 右側(cè)主內(nèi)容區(qū)域顯示友好提示
 st.title("?? 異常報告系統(tǒng)")
 st.info("""
 ## 歡迎使用異常報告系統(tǒng)!
 
 ### ?? 當(dāng)前狀態(tài)
 - 報告數(shù)據(jù)文件未找到或為空
 - 請確保 `report/incident_report.json` 文件存在且包含有效數(shù)據(jù)
 
 ### ?? 如何添加數(shù)據(jù)
 1. 在 `report` 文件夾中創(chuàng)建 `incident_report.json` 文件
 2. 按照以下格式添加報告數(shù)據(jù):
 ```json
 [
 {
 "timestamp": "2024-01-01 10:00:00",
 "raw_log": "日志內(nèi)容...",
 "root_cause_report": "【標(biāo)題】\n報告標(biāo)題\n【異常原因分析】\n分析內(nèi)容..."
 }
 ]
 ```
 
 ### ?? 功能說明
 - 支持按時間倒序查看報告
 - 支持搜索報告內(nèi)容
 - 自動解析報告結(jié)構(gòu)并展示
 """)
 st.stop()

 # 倒序排序:最新的報告排在最前面
 reports_sorted = sorted(reports, key=lambda x: x["timestamp"], reverse=True)

 # 添加搜索輸入框
 search_query = st.sidebar.text_input("?? 搜索報告內(nèi)容(標(biāo)題/日志/分析/建議)").strip()

 # 判斷一條報告是否匹配搜索條件
 def report_matches(report, query):
 if not query:
 return True
 query = query.lower()
 if query in extract_title(report["root_cause_report"]).lower():
 return True
 if query in report["raw_log"].lower():
 return True
 if query in report["root_cause_report"].lower():
 return True
 return False

 # 根據(jù)搜索條件過濾報告
 filtered_reports = [r for r in reports_sorted if report_matches(r, search_query)]

 # 構(gòu)建側(cè)邊欄顯示內(nèi)容(報告生成時間 | 標(biāo)題)
 sidebar_items = [
 f"{r['timestamp']} | {extract_title(r['root_cause_report'])}" for r in filtered_reports
 ]

 # 沒有結(jié)果時提示
 if not sidebar_items:
 st.sidebar.warning("未找到匹配的報告")
 st.stop()

 # 側(cè)邊欄選擇框
 selected_item = st.sidebar.radio("選擇報告", sidebar_items)

 # ========================
 # 側(cè)邊欄:測試區(qū)域
 # ========================
 st.sidebar.markdown("---")
 st.sidebar.subheader("?? 測試數(shù)據(jù)生成")

 # 日志類型選擇
 log_type = st.sidebar.radio(
 "選擇日志類型",
 ["CPU"],
 help="CPU: 生成CPU使用率日志",
 )

 # CPU使用率輸入(僅在選擇CPU類型時顯示)
 cpu_usage = None
 if log_type == "CPU":
 cpu_usage = st.sidebar.number_input(
 "CPU使用率 (%)",
 min_value=0.1,
 max_value=100.0,
 value=85.0,
 step=0.1,
 help="輸入0.1-100之間的CPU使用率值"
 )

 # 生成按鈕
 if st.sidebar.button(f"生成{log_type}日志", type="primary"):
 with st.spinner("正在生成日志..."):
 result = log_generator.generate_and_save_log(log_type.lower(), cpu_usage)
 
 if result['status'] == 'success':
 st.sidebar.success(result['message'])
 st.sidebar.info(f"日志文件: {result['log_file']}")
 if 'log_content_preview' in result:
 st.sidebar.code(result['log_content_preview'], language="text")
 else:
 st.sidebar.error(result['message'])

 # 根據(jù)選擇找到對應(yīng)的報告
 selected_index = sidebar_items.index(selected_item)
 selected_report = filtered_reports[selected_index]

 # 提取報告標(biāo)題
 report_title = extract_title(selected_report["root_cause_report"])

 # ========================
 # 右側(cè)內(nèi)容展示
 # ========================
 st.title(report_title)
 st.subheader(f"**報告生成時間**: {selected_report['timestamp']}")

 # ---- 原始日志(默認(rèn)折疊) ----
 with st.expander("原始日志", expanded=False):
 st.code(selected_report["raw_log"], language="text")

 # ---- 根因分析報告 ----
 sections = split_sections(selected_report["root_cause_report"])
 # 去掉【標(biāo)題】部分,避免重復(fù)展示
 sections.pop("標(biāo)題", None)


 for key, value in sections.items():
 st.subheader(key) # 小節(jié)標(biāo)題,例如 “異常原因分析” st.markdown(value.strip()) # 小節(jié)內(nèi)容

啟動 EFK 服務(wù)

按照下圖所示,啟動 EFK 服務(wù),如果已經(jīng)啟動,建議重啟一下,保證配置文件修改之后可以生效。

啟動分析服務(wù)

當(dāng)大模型(DeepSeek)接收到告警信息之后會生成對應(yīng)的報告。

執(zhí)行如下指令:

python process_log.py

看到如下內(nèi)容的時候, 說明該服務(wù)啟動了。

Flask API 啟動中...
 環(huán)境變量配置:
 - 從 .env 文件加載 DEEPSEEK_API_KEY: 已配置
 日志文件路徑:
 - 操作日志: /Users/cuihao/docker/EFK/api_operation.log
 - 錯誤日志: /Users/cuihao/docker/EFK/api_error.log
 可用的端點:
 - POST /analyze-fluentd-log - 接收日志并在后臺處理
 - GET /health - 服務(wù)健康檢查
 * Serving Flask app "process_log" (lazy loading)

生成測試數(shù)據(jù)

通過如下命令執(zhí)行啟動測試和報告查看界面。

streamlit run log_ui.py

如下圖所示,在打開的 Web UI 界面的左側(cè),輸入 CPU 使用率為 85.5%,并點擊“生成 CPU 日志”。

觀察結(jié)果

由于日志文件寫入了內(nèi)容, 日志采集服務(wù)、過濾服務(wù)以及處理服務(wù)都啟動了,接下來就可以觀察日志處理的過程了。通過在容器中執(zhí)行 Fluent-bit 的日志文件可以發(fā)現(xiàn),它已經(jīng)采集到了對應(yīng)的日志信息,如下:

通過是在 Fluent-D 中也可以看到接收到的日志信息,如下:

ES + Kibana 日志展示

這里只是展示日志內(nèi)容,包含了正常和異常兩類數(shù)據(jù),但是沒有對智能日志的顯示。

配置確認(rèn)

ES和Kibana的鏡像版本都是使用的8.17.1,版本保持要一致,ES和Kibana服務(wù)的配置在docker-compse中。

ES的數(shù)據(jù)來自fluentd轉(zhuǎn)發(fā),由于轉(zhuǎn)發(fā)功能依賴fluentd的插件fluent-plugin-elasticsearch,fluentd的dockerfile文件中配置有安裝插件的命令,在通過docker compose生成容器啟動時會執(zhí)行dockerfile中的命令安裝插件到fluentd容器中。

轉(zhuǎn)發(fā)到ES需要修改配置文件fluent.conf,通過服務(wù)名進(jìn)行轉(zhuǎn)發(fā)到ES。

KIbana鏡像中默認(rèn)配置連接到ES(ES默認(rèn)啟動的端口是9200,如果ES的端口需要手動修改,這里也需要改成相應(yīng)的端口)。

日志展示

我在 fluentd 中完成了到ES的轉(zhuǎn)發(fā)配置,并成功轉(zhuǎn)發(fā)才會生成索引。通過配置文件可以看出,Kibana的訪問端口為 5601。通過如下地址:http://localhost:5601/app/discover#/ 訪問,如果首次Kibana打開會提示創(chuàng)建數(shù)據(jù)視圖。

如下圖所示,在彈窗中配置名稱和匹配索引的規(guī)則。fluentd-*表示匹配所有fluentd-開頭的索引,在右邊列表會顯示匹配的索引。

視圖創(chuàng)建完成后會跳轉(zhuǎn)到展示頁面,默認(rèn)顯示最近15分鐘的數(shù)據(jù)。

修改時間范圍,這里切換到15天,右邊的Refresh按鈕會變成Update。

點擊Update,就可以看到數(shù)據(jù)列表。

作者介紹

崔皓,51CTO社區(qū)編輯,資深架構(gòu)師,擁有18年的軟件開發(fā)和架構(gòu)經(jīng)驗,10年分布式架構(gòu)經(jīng)驗。

責(zé)任編輯:姜華 來源: 51CTO內(nèi)容精選
相關(guān)推薦

2017-04-14 13:54:41

WOT2017架構(gòu)運維

2016-01-13 10:11:20

智能化運維運維自動化運維

2012-05-15 14:58:57

IT運維

2015-11-30 11:25:20

UPYUN

2018-12-14 11:04:56

數(shù)據(jù)庫運維智能

2009-06-09 16:24:11

攜程網(wǎng)運維管理SiteView

2012-08-15 14:58:01

運維架構(gòu)師

2018-03-27 16:23:53

運維AI智能

2020-06-30 09:35:25

智能運維云架構(gòu)IT運營

2017-10-13 13:14:35

互聯(lián)網(wǎng)

2022-10-20 17:37:46

運維智能管理平臺

2025-08-29 08:58:02

2009-03-18 10:00:27

電子政務(wù)運維管理廣通信達(dá)

2023-04-12 14:24:03

人工智能機(jī)器學(xué)習(xí)

2025-08-18 08:06:15

2020-01-21 21:48:58

運維架構(gòu)技術(shù)

2009-04-15 09:36:21

運維

2017-02-27 18:50:42

運維持續(xù)交付
點贊
收藏

51CTO技術(shù)棧公眾號