前言
由于logstash內(nèi)存占用較大,靈活性相對(duì)沒那么好,ELK正在被EFK逐步替代.其中本文所講的EFK是Elasticsearch+Fluentd+Kfka,實(shí)際上K應(yīng)該是Kibana用于日志的展示,這一塊不做演示,本文只講述數(shù)據(jù)的采集流程.
前提
架構(gòu)
數(shù)據(jù)采集流程
數(shù)據(jù)的產(chǎn)生使用cadvisor采集容器的監(jiān)控?cái)?shù)據(jù)并將數(shù)據(jù)傳輸?shù)終afka.
數(shù)據(jù)的傳輸鏈路是這樣: Cadvisor->Kafka->Fluentd->elasticsearch

每一個(gè)服務(wù)都可以橫向擴(kuò)展,添加服務(wù)到日志系統(tǒng)中.
配置文件
docker-compose.yml
version: "3.7"
services:
elasticsearch:
image: elasticsearch:7.5.1
environment:
- discovery.type=single-node #使用單機(jī)模式啟動(dòng)
ports:
- 9200:9200
cadvisor:
image: google/cadvisor
command: -storage_driver=kafka -storage_driver_kafka_broker_list=192.168.1.60:9092(kafka服務(wù)IP:PORT) -storage_driver_kafka_topic=kafeidou
depends_on:
- elasticsearch
fluentd:
image: lypgcs/fluentd-es-kafka:v1.3.2
volumes:
- ./:/etc/fluent
- /var/log/fluentd:/var/log/fluentd
其中:
- cadvisor產(chǎn)生的數(shù)據(jù)會(huì)傳輸?shù)?92.168.1.60這臺(tái)機(jī)器的kafka服務(wù),topic為kafeidou
- elasticsearch指定為單機(jī)模式啟動(dòng)(
discovery.type=single-node環(huán)境變量),單機(jī)模式啟動(dòng)是為了方便實(shí)驗(yàn)整體效果
fluent.conf
#<source>
# type http
# port 8888
#</source>
<source>
@type kafka
brokers 192.168.1.60:9092
format json
<topic>
topic kafeidou
</topic>
</source>
<match **>
@type copy
# <store>
# @type stdout
# </store>
<store>
@type elasticsearch
host 192.168.1.60
port 9200
logstash_format true
#target_index_key machine_name
logstash_prefix kafeidou
logstash_dateformat %Y.%m.%d
flush_interval 10s
</store>
</match>
其中:
type為copy的插件是為了能夠?qū)luentd接收到的數(shù)據(jù)復(fù)制一份,是為了方便調(diào)試,將數(shù)據(jù)打印在控制臺(tái)或者存儲(chǔ)到文件中,這個(gè)配置文件默認(rèn)關(guān)閉了,只提供必要的es輸出插件.
需要時(shí)可以將@type stdout這一塊打開,調(diào)試是否接收到數(shù)據(jù).輸入源也配置了一個(gè)http的輸入配置,默認(rèn)關(guān)閉,也是用于調(diào)試,往fluentd放入數(shù)據(jù).
可以在linux上執(zhí)行下面這條命令:
curl -i -X POST -d 'json={"action":"write","user":"kafeidou"}' http://localhost:8888/mytag
- target_index_key參數(shù),這個(gè)參數(shù)是將數(shù)據(jù)中的某個(gè)字段對(duì)應(yīng)的值作為es的索引,例如這個(gè)配置文件用的是machine_name這個(gè)字段內(nèi)的值作為es的索引.
開始部署
在包含docker-compose.yml文件和fluent.conf文件的目錄下執(zhí)行:
docker-compose up -d
在查看所有容器都正常工作之后可以查看一下elasticsearch是否生成了預(yù)期中的數(shù)據(jù)作為驗(yàn)證,這里使用查看es的索引是否有生成以及數(shù)據(jù)數(shù)量來驗(yàn)證:
-bash: -: 未找到命令
[root@master kafka]# curl http://192.168.1.60:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open 55a4a25feff6 Fz_5v3suRSasX_Olsp-4tA 1 1 1 0 4kb 4kb
也可以直接在瀏覽器輸入http://192.168.1.60:9200/_cat/indices?v查看結(jié)果,會(huì)更方便.
可以看到我這里是用了machine_name這個(gè)字段作為索引值,查詢的結(jié)果是生成了一個(gè)叫55a4a25feff6的索引數(shù)據(jù),生成了1條數(shù)據(jù)(docs.count)
到目前為止kafka->fluentd->es這樣一個(gè)日志收集流程就搭建完成了.
當(dāng)然了,架構(gòu)不是固定的.也可以使用fluentd->kafka->es這樣的方式進(jìn)行收集數(shù)據(jù).這里不做演示了,無非是修改一下fluentd.conf配置文件,將es和kafka相關(guān)的配置做一下對(duì)應(yīng)的位置調(diào)換就可以了.
鼓勵(lì)多看官方文檔,在github或fluentd官網(wǎng)上都可以查找到fluentd-es插件和fluentd-kafka插件.
始發(fā)于 四顆咖啡豆 ,轉(zhuǎn)載請(qǐng)聲明出處.
關(guān)注公眾號(hào)->[四顆咖啡豆] 獲取最新內(nèi)容
四顆咖啡豆
