前言
前文解決了鏈路的基本部署和配置調整,見專題 集中式日志分析平臺 - ELK Stack。在實際的生產過程中,我們需要把多個不同來源的日志按照不同的 index 寫入 ES,這樣方便針對不同的數據進行匯總分析。本文介紹如何根據 type 和 document_type 進行日志分離,達到我們所期望的效果,整個配置步驟我們是從數據流的產生到傳輸到落地這個角度去闡述。
本文針對的是整合了 Kafka 作為緩沖消息隊列的架構:
filebeat -> kafka -> logstash -> elasticsearch -> kibana
Filebeat 端配置
Filebeat 官方文檔中描述了一個配置,可以對數據流按照 input_type 級別的打標,通過 document_type 關鍵字申明:
filebeat.prospectors:
- input_type: log
paths:
- /home/admin/soft/filebeat-5.2.1-linux-x86_64/logs/filebeat
document_type: fb_log
- input_type: log
paths:
- /home/admin/soft/elasticsearch-5.2.1/logs/elasticsearch.log
document_type: es_log
比如上面的配置表述,prospectors 具有 2 個采集輸入,分別是采集 filebeat 日志和 elasticsearch 日志,filebeat 日志打標為 fb_log,elasticsearch 日志打標為 es_log。
然后,我們的輸出是 kafka,我們可以通過宏去通配推送數據到不同的 kafka topic :
output.kafka:
hosts: ["172.16.134.3:9092"]
topic: '%{[type]}'
partition.round_robin:
reachable_only: false
required_acks: 1
compression: gzip
max_message_bytes: 1000000
這樣配置,我們的 filebeat 日志和 elasticsearch 日志會推送到不同的 kafka topic,分別是 fb_log 和 es_log,配置完成后記得重啟。
Logstash 端配置
和 filebeat 對應的, logstash 的 input / filter / output 配置段,也有類似的打標方式:
input {
kafka {
bootstrap_servers => "${BOOTSTRAP_SERVERS}"
consumer_threads => 3
topics => "fb_log"
type => "fb_log"
}
kafka {
bootstrap_servers => "${BOOTSTRAP_SERVERS}"
consumer_threads => 3
topics => "es_log"
type => "es_log"
}
}
output {
if [type] == "fb_log" {
elasticsearch {
hosts => "${HOSTS}"
manage_template => false
index => "fb_log-%{+YYYY.MM.dd}"
document_type => "fb_log"
}
}
if [type] == "es_log" {
elasticsearch {
hosts => "${HOSTS}"
manage_template => false
index => "es_log-%{+YYYY.MM.dd}"
document_type => "es_log"
}
}
}
配置完成后重啟。
和之前的文檔類似,我們在 Kibana Discover 中,需要添加剝離后的 Index,比如按照 fb_log-*
去搜索。
小結
本文簡述了,在 ELK 生產環境中,如何進行按照日志來源區別的 ES index 配置。