现在才开始试用Fluentd,Apache + Elasticsearch + Kibana,这是什么好吃的? 第5部分

“现在试试 Fluentd 第五部(完结篇),虽然有点晚”

经历了很多曲折,终于能够以相同的水平成功安装了(不知道是第几次安装了?)。哇哦,幸好之前安装的版本有bug,我通过解决bug学到了很多东西。现在我终于能够站在起跑线上了!接下来,让我们进入正题——”Apache + fluentd + Elasticsearch + Kibana”(真不知道是第几次了?)这是完结篇了。

使用Fluentd + Elasticsearch + Kibana搭建数据集计服务器。

apache.png

文件描述符,内核参数的设置

我会准备两个虚拟机。这只是为了将服务端和分析端的实例分离并明确,所以如果需要可以忽略这部分(只是个人爱好问题)。不过,由于看起来 fluentd 使用了很多文件描述符,所以我会将默认值 1024 调大一些。

$ sudo vi /etc/security/limits.conf
root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 65536

$ ulimit -n (システムを再起動)
$ sudo vi /etc/sysctl.conf
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240    65535

$ sysctl -w

安装 fluentd

我选择安装降级版本的td-agent 0.10.45,原因是最新版本还不够稳定。

$ sudo su
# curl -L http://toolbelt.treasuredata.com/sh/install-redhat.sh | sh
$ sudo yum remove td-agent-1.1.20-0.x86_64
$ sudo yum install td-agent-1.1.19-0.x86_64
$ td-agent --version
td-agent 0.10.45

所以,如果安装了yum的fastestmirror的插件或者进行更新,那么之前所做的降级操作就会被覆盖,为了避免这种情况,在/etc/yum.conf文件中添加以下内容。

exclude=td-*

收信端(集收端)的设定

导入elasticsearch插件

在接收端(也称为收集器端)引入fluentd的elasticsearch插件时,安装gem包时需要安装libcurl-devel和gcc。

# yum install libcurl-devel gcc
# /usr/lib64/fluent/ruby/bin/gem install fluent-plugin-elasticsearch

安装 Elasticsearch

我将添加外部存储库(EPEL)。另外,由于 Elasticsearch 需要 Java,我也会安装它(个人觉得安装 Java 真的很麻烦哈哈)。另外,顺便安装形态解析器的 Kuromoji 插件也是个不错的选择。

# rpm --import http://packages.elasticsearch.org/GPG-KEY-elasticsearch
# vi /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-1.1]
name=Elasticsearch repository for 1.1.x packages
baseurl=http://packages.elasticsearch.org/elasticsearch/1.1/centos
gpgcheck=1
gpgkey=http://packages.elasticsearch.org/GPG-KEY-elasticsearch
enabled=1

# yum install elasticsearch java-1.7.0-openjdk
# chkconfig --add elasticsearch
# chkconfig elasticsearch on
# service elasticsearch start

然后,检查动作。

$ curl -X GET http://localhost:9200/
{
  "status" : 200,
  "name" : "Wilbur Day",
  "version" : {
    "number" : "1.1.2",
    "build_hash" : "e511f7b28b77c4d99175905fac65bffbf4c80cf7",
    "build_timestamp" : "2014-05-22T12:27:39Z",
    "build_snapshot" : false,
    "lucene_version" : "4.7"
  },
  "tagline" : "You Know, for Search"
}

安装Apache

這裡都照常運行,如果調整一下ServerName就不會有警告,所以我會先處理這個。只是啟動kibana所以用nginx也可以,這裡也只是一個喜好的問題。

# yum install httpd
# /etc/init.d/httpd start
# chkconfig httpd on

Kibana的安装

在2014年7月4日(最新版本为3.1.0)安装。

# curl -sL https://download.elasticsearch.org/kibana/kibana/kibana-3.1.0.tar.gz | sudo tar zxf - -C /var/www/html
# mv /var/www/html/kibana-3.1.0 /var/www/html/kibana
# /etc/init.d/httpd restart

访问

kibana.png

Fluentd 的定义

為了確認傳送的內容,在開始運用之前,最好使用 type stdout 命令指定輸出為 /var/log/td-agent/td-agent.log,並在確認內容並微調的同時進行理解和定義。

<source>
  type forward
  port 24224
</source>


<match apache.**>
  type copy

  <store>
    type stdout
  </store>

  <store>
    type elasticsearch
    host elasticsearch IPアドレス
    port 9200
    type_name access_log_2
    logstash_format true
    logstash_prefix apache_access
    logstash_dateformat %Y%m
  </store>

 </match>

发送端(代理方)

流畅存储(Fluentd)的定义

使用Fluentd的Apache2插件,将解码的内容传输到已安装Elasticsearch的服务器。在这里,我们还指定了输出类型为stdout。在开始操作之前,最好查看/var/log/td-agent/td-agent.log并进行相应配置。

<source>
  type tail
  path /var/log/httpd/access_log
  tag apache.combined
  pos_file /var/log/td-agent/httpd-access.log.pos
  format apache2
</source>

<match apache.**>
  type copy

  <store>
    type stdout
  </store>

  <store>
    type forward
    buffer_chunk_limit 256m
    buffer_queue_limit 128
    flush_interval 5s
    <server>
      host elasticsearch IPアドレス
      port 24224
    </server>
  </store>
</match>

基本上是一个用于数据可视化和分析的开源工具。

当Fluentd的设置让数据积累到Elasticsearch时,我们才能准备好可视化数据。顺便提一下,如果数据没有存储起来,就无法在Kibana中进行分析,这样就不够有趣。所以,我们需要使用wget或curl这样的工具来调用服务端,暂时存储一些数据。

如何使用Kibana

kibana2.png

使用fluentd + Elasticsearch + Kibana开始进行日志分析(设置部分)

下次

计划将Fluentd配置为多阶段结构并实现冗余配置,这样做是为了探索和学习。

开始使用Fluentd进行日志管理 【整理转发设置】

广告
将在 10 秒后关闭
bannerAds