好久没写博客啦,关键没成品可分享的。

标题很高端的赶脚有木有,其实就是简单的ansible+fluentd+es+kibana。

本篇的内容主要是自动化部署日志收集系统fluentd

简单介绍下各个工具:

fluentd是一个日志收集系统,它的特点在于其各部分均是可定制化的,你可以通过简单的配置,将日志收集到不同的地方。目前开源社区已经贡献了下面一些存储插件:es,hdfs,mongodb,redis,mysql等等。

ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。支持通过HTTP使用JSON进行数据索引。

Kibana 是一个为 LogstashElasticSearch 提供的日志分析的 Web 接口。可使用它对日志进行高效的搜索、可视化、分析等各种操作。

ansible是一个模型驱动的配置管理器,支持多节点发布、远程任务执行。默认使用 SSH 进行远程连接。无需在被管理节点上安装附加软件,可使用各种编程语言进行扩展.


架构图如下(画的不好,请见谅):

ansible自动化部署分布式日志收集系统_fluentd;es;kibana;lo

流程如下:

  1. It continuously “tails” the access log file.

  2. It parses the incoming log entries into meaningful fields (such as ip, path, etc.) and buffers them.

  3. It writes the buffered data to es periodically.

4. 通过Kibana进行展示,搜索,查看等操作;

---------------------

接下来简单说说td-agent.conf的设置:详细语法还是看fluentd官方文档更清楚,这里就不解释了

1. 收集server端本地日志

<source>
  type tail
  format syslog
  path /var/log/messages
  pos_file /var/log/td-agent/messages.pos
  tag system.nginx.message
</source>
<match *.nginx.*>
  index_name adminpack
  type_name nginx
  type elasticsearch
  include_tag_key true
  tag_key @log_name
  host es-server
  port 9200
  logstash_format true
  flush_interval 5s
</match>

简单来说就是一个source源文件,对应一个match存储


2. 收集远端clientd客户端服务器日志

首先server服务器端必须开启个接收端口

<source>
  type forward
  port 24224
  bind 0.0.0.0
</source>

和对应的match,这样才知道存到哪里


<match *.nginx.*>
  index_name adminpack
  type_name nginx
  type elasticsearch
  include_tag_key true
  tag_key @log_name
  host es-server
  port 9200
  logstash_format true
  flush_interval 5s
</match>

然后client客户端设置forward,同样的也是要设置source源文件

以下就是个典型的客户端配置文件

<source>
  type tail
  format apache2
  path /var/log/nginx/access.log
  pos_file /var/log/td-agent/nginx.access.pos
  tag mysql.nginx.access
</source>
<source>
  type tail
  format syslog
  path /var/log/messages
  pos_file /var/log/td-agent/messages.pos
  tag system.nginx.message
</source>
<match *.nginx.*>
  # output type
  type forward
  send_timeout 10s
  recover_wait 5s
  heartbeat_interval 1s
  phi_threshold 8
  hard_timeout 10s
  # primary host
  <server>
    name collector
    host 192.168.200.216
    port 24224
    weight 60
  </server>
  # Failed
  <secondary>
    type file
    path /var/log/fluent/forware-failed
  </secondary>
  # Buffer Parameters
  buffer_type memory
  flush_interval 3s
</match>

这样就可以了,很简单,灵活

最后看看kibana的效果图

ansible自动化部署分布式日志收集系统_fluentd;es;kibana;lo_02

我们之前是采用fluentd+mongodb的形式,线上跑了快一年,效果不错,这个比较适合二次开发,挺好。fluentd有收集Mysql慢日志的插件哦,这个很不错的,方便查找,结合下我们的监控系统做分析。

那为什么不用更常见的logstash+es+kibana类?

主要是线上当初选择的就是fluentd,一改动得全都改,人懒得改了,还有对fluentd很满意,不想换了。

这个项目在github上面有,是小鬼子写的,我fork了稍微改了下,大家可以试试

地址:https://github.com/vTNT/ansible-elasticsearch