方案概述
ELK是Elasticsearch、Logstash和Kibana的簡稱,它們組合起來提供了業(yè)界最常用的 日志分析 和可視化工具。
Elasticsearch是一個基于Lucene的開源、分布式、RESTful搜索和分析引擎。
Logstash是一個開源的、服務(wù)器端的數(shù)據(jù)處理管道,能夠同時從多個來源實(shí)時接收、轉(zhuǎn)換并將數(shù)據(jù)發(fā)送到用戶選擇的“存儲庫”。通常用于日志的收集、過濾和轉(zhuǎn)發(fā)。
Kibana是一個開源的分析和可視化平臺,用于數(shù)據(jù)的可視化、儀表盤創(chuàng)建和搜索查詢。通常與Elasticsearch一起使用。
華為 云日志服務(wù)LTS 在功能豐富度、成本、性能方面優(yōu)于開源ELK方案,具體對比可以參考 云日志 服務(wù) LTS 對比自建ELK Stack有什么優(yōu)勢?。本文提供最佳實(shí)踐,使用自定義Python腳本和LTS采集器ICAgent,協(xié)助用戶將日志從Elasticsearch(簡稱ES) 遷移 到LTS中。
當(dāng)前華為云支持ECS機(jī)器通過安裝ICAgent來采集日志文件,因此可以基于該功能實(shí)現(xiàn)Elasticsearch日志導(dǎo)入云日志服務(wù)。
Elasticsearch數(shù)據(jù)先通過python腳本將數(shù)據(jù)落盤到ECS,然后通過LTS服務(wù)的日志接入功能,將落盤的日志文件采集到LTS服務(wù)。

將自建ELK日志導(dǎo)入云日志服務(wù)LTS
- 登錄云日志服務(wù)控制臺。
- 請參考安裝ICAgent在ECS主機(jī)安裝ICAgent。
- 配置ECS日志接入云日志服務(wù),請參考ECS接入。
- 腳本執(zhí)行前期準(zhǔn)備。以下示例僅供參考,請以實(shí)際信息填寫為準(zhǔn)。
- 首次使用python,需要安裝python環(huán)境。
- 首次使用ES時需要安裝對應(yīng)ES版本的python數(shù)據(jù)包,本次方案測試使用的elasticsearch為7.10.1版本。
pip install elasticsearch==7.10.1
- 方案測試使用的ES為華為云CSS服務(wù)創(chuàng)建的ES。
- 執(zhí)行用來構(gòu)造索引數(shù)據(jù)的python腳本,如果索引已經(jīng)有數(shù)據(jù),忽略這一步,直接執(zhí)行6。
python腳本需執(zhí)行在ECS機(jī)器,腳本命名為xxx.py格式,構(gòu)造數(shù)據(jù)請參考如下示例:
以下斜體字段需按照實(shí)際情況進(jìn)行修改,參考示例是插入1000條數(shù)據(jù),內(nèi)容為:This is a test log,Hello world!!!\n;
- index:要創(chuàng)建的索引名稱,參考示例為: test。
- 鏈接ES:ES的訪問url,參考示例為:http://127.0.0.1:9200。
from elasticsearch import Elasticsearch def creadIndex(index): mappings = { "properties": { "content": { "type": "text" } } } es.indices.create(index=index, mappings=mappings) def reportLog(index): i = 0 while i < 1000: i = i + 1 body = {"content": "This is a test log,Hello world!!!\n"} es.index(index=index,body=body) if __name__ == '__main__': #索引名稱 index = 'test' #鏈接ES es = Elasticsearch("http://127.0.0.1:9200") creadIndex(index) reportLog(index)
- 構(gòu)建python讀寫腳本,用來將ES數(shù)據(jù)寫入磁盤,輸出文件路徑需與配置日志接入規(guī)則一致。
腳本需執(zhí)行在ecs機(jī)器,命名xxx.py格式,寫入磁盤數(shù)據(jù)腳本請參考如下示例:
以下斜體字段需按照實(shí)際情況進(jìn)行修改。
- index:字段為索引名,參考示例為: test。
- pathFile :為數(shù)據(jù)寫入磁盤絕對路徑,參考示例為: /tmp/test.log。
- scroll_size :為索引滾動查詢大小,參考示例為:100。
- 鏈接ES:ES的訪問url,參考示例為:http://127.0.0.1:9200。
from elasticsearch import Elasticsearch def writeLog(res, pathFile): data = res.get('hits').get('hits') i = 0 while i < len(data): log = data[i].get('_source').get('content') file = open(pathFile, 'a', encoding='UTF-8') file.writelines(log) i = i + 1 file.flush() file.close() if __name__ == '__main__': #索引名稱 index = 'test' #輸出文件路徑 pathFile = '/tmp/' + index + '.log' #滾動查詢一次滾動大小,默認(rèn)為100 scroll_size = 100 #鏈接ES es = Elasticsearch("http://127.0.0.1:9200") init = True while 1: if (init == True): res = es.search(index=index, scroll="1m", body={"size": scroll_size}) init =False else: scroll_id = res.get("_scroll_id") res = es.scroll(scroll="1m", scroll_id=scroll_id) if not res.get('hits').get('hits'): break writeLog(res, pathFile)
- 執(zhí)行命令前,請確保python已安裝成功,在ECS執(zhí)行如下命令,將ES索引數(shù)據(jù)寫入磁盤。
python xxx.py
- 查看數(shù)據(jù)是否成功查詢及寫入磁盤。
參考示例demo寫入磁盤路徑為:/tmp/test.log,操作時需要填寫實(shí)際使用的路徑,執(zhí)行如下命令可以查看數(shù)據(jù)寫入磁盤情況。
tail -f /tmp/test.log
- 登錄云日志服務(wù)控制臺,在“日志管理”頁面,單擊日志流名稱進(jìn)入詳情頁面,在“日志搜索”頁簽查看到日志數(shù)據(jù),即代表采集成功。