基于docker的 EFK 日志分析系统

发布于 2021-09-09 18:34 阅读 1255

EFK 不是一个软件,而是一套解决方案。EFK 是三个开源软件的缩写,Elasticsearch,Fluentd,Kibana。其中 ELasticsearch 负责日志分析和存储,Fluentd 负责日志收集,Kibana 负责界面展示。它们之间互相配合使用,完美衔接,高效的满足了很多场合的应用,是目前主流的一种日志分析系统解决方案。 本文主要基于Fluentd实时读取日志文件的方式获取日志内容,将日志发送至Elasticsearch,并通过Kibana展示。

ee.JPG

WechatIMG2066.png

启动服务

docker-compose -f docker-compose.yml up -d

验证服务是否已正常启动

注意,es和kibana容器启动后,里面的服务还需要等待片刻

# es
http://localhost:9200
# kibana后台
http://localhost:5601

获取nginx 日志,将日志发送至Elasticsearch,并通过Kibana展示

#配置文件
fluentd/fluent.conf
#登录fluent容器,并向access.log添加数据
cat /var/log/nginx/access2.log > /var/log/nginx/access.log

如果没意外,就会在kibana后台(Management->Stack Management->数据->索引管理)看到fluentd.td.nginx.access开头的索引 aaa.JPG 下一步,创建索引模式(Management->Stack Management->Kibana->索引模式) 微信图片_20210909182709.png 最后,点击Discover(Analytics->Discover)就可以看到日志数据了 www.JPG

向es,Fluentd推数据

php向es推数据

http://localhost/es.php

详见:/php/www/es.php

php向Fluentd推数据

Fluentd再将数据转发到es,最终在Kibana中展示

http://localhost/fluentd.php

详见:/php/www/fluentd.php

通过url向Fluentd推数据

Fluentd再将数据转发到es,最终在Kibana中展示

vi fluentd/fluent.conf

<source>
  @type forward
  port 24224
  bind 0.0.0.0
</source>
# http://<ip>:9880/debug.test?json={"hehe":"uu"}
<source>
  @type http
  port 9880
</source>

<match debug.test>
  @type elasticsearch
  host elasticsearch
  port 9200
  index_name fluentd.${tag}.%Y%m%d
  <buffer tag,time>
    timekey 1m
  </buffer>
</match>

http://localhost:9880/debug.test?json={"haha":"heihei"}

WechatIMG2065.png

代码

https://github.com/chudaozhe/efk

参考

https://www.cnblogs.com/zongxiang/p/14745073.html

https://www.cnblogs.com/sanduzxcvbnm/p/13932087.html

https://docs.fluentd.org/configuration/config-file

https://docs.fluentd.org/input

https://docs.fluentd.org/output

广而告之,我的新作品《语音助手》上架Google Play了,欢迎下载体验