使用Fluentd + Elasticsearch + Kibana建立数据统计服务器

可以适用于各种类型的日志,并且能够很好地整理它们,使分析变得更加容易。是否能够熟练运用仍然成谜。

Fluentd是一个方便的工具,用于汇总日志。
Elasticsearch是一个搜索引擎。它可以对通过Fluentd收集的日志进行良好的处理。
Kibana可以以漂亮的方式查看和处理汇总数据,并且可以进行各种搜索,非常厉害。

安装Elasticsearch。

■官方网站
http://www.elasticsearch.org/overview/elkdownloads/

这次我们将从RPM版进行安装。

wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.2.1.noarch.rpm

rpm -ivh elasticsearch-1.2.1.noarch.rpm

chkconfig --add elasticsearch

我要手动安装Java,因为用yum安装的版本在Oracle上无法正常运行。

■如果你在谷歌上搜索“Java安装”,会出现Java官方网站。

安装RPM版本。

rpm -ivh jdk-8u5-linux-x64.rpm

修改设置文件

暂时先只需一台设备,所以只需要进行最基本的设置。

index.number_of_shards: 1
index.number_of_replicas: 0
path.data: /data
path.logs: /var/log/elasticsearch/logs

创建目标目录并授予权限

mkdir -p elasticsearch:elasticsearch /data/elasticsearch
chown -R elasticsearch:elasticsearch /data

服务启动

/etc/init.d/elasticsearch start
elasticsearch を起動中:                                    [  OK  ]

目前为止,Elasticsearch的准备工作已经完成。

虽然通过rpm安装的Java应该是没问题的,但是如果通过源码安装或者有多个Java同时运行时,如果不加以处理,可能会在yum update时自动更改JAVA_HOME,或者使命令的PATH倒退,导致各种故障,所以排除在外。


execlude=java*

安装Kibana

Kibana下载网站
http://www.elasticsearch.org/overview/kibana/installation/

使用wget下载并部署

wget https://download.elasticsearch.org/kibana/kibana/kibana-3.1.0.tar.gz

tar xvfz kibana-3.1.0.tar.gz

mv kibana-3.1.0 /opt/kibana

安装Fluentd

安装rpm版

curl -L http://toolbelt.treasuredata.com/sh/install-redhat.sh | sh

在进行安装之前

根据Fluentd的官方文档,建议修改操作系统的内核配置,因此进行配置更改。
http://docs.fluentd.org/ja/articles/before-install

安装ntpd

yum install ntpd
chkconfig ntpd on

增加文件描述符的最大值


root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 65536

网络相关的内核参数优化

net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240    65535

等一切都结束后重新启动

安装Fluentd-agent

这次我们将在与日志统计服务器不同的服务器上安装代理(agent),以便从远程发送数据。
上述提到的Fluentd安装部分我们将全部完成。

安装Elasticsearch插件。

由于在使用rpm安装fluentd时也会包含Ruby,因此需要对附带的Ruby进行gem install操作。

/usr/lib64/fluent/ruby/bin/gem install fluent-plugin-elasticsearch

错误

ERROR:  Error installing fluent-plugin-elasticsearch:
    ERROR: Failed to build gem native extension.

        /usr/lib64/fluent/ruby/bin/ruby extconf.rb
checking for curl-config... no
checking for main() in -lcurl... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more

解决

yum install libcurl-devel

再次出现错误。只是没有gcc而已…

yum install gcc

修改配置文件。尝试收集Apache日志。


<source>
  type tail
  format apache
  path /var/log/httpd/access_log.%Y%m%d
  tag apache.access
</source>

<match apache.access>
  type elasticsearch
  host 192.168.33.10
  port 9200
  type_name access_log_2
  logstash_format true
  logstash_prefix apache_access
  logstash_dateformat %Y%m
</match>

做完之后我想到了,Kibana那边其实不需要Fluentd。

bannerAds