我知道现在有点晚了,但是我想总结一下关于fluentd的内容(第三部分)

这次我们将作为前一次应用的延伸,完成对Rails应用程序日志的聚合,并在Kibana进行查看。

前提 tí)

我会使用以下的物品。

    • Rails 4.1

 

    • ruby 2.1.5

 

    • fluentd

 

    • elasticsearch 1.4.4

 

    kibana 4

设定

轨道

我将使用以下的宝石。

添加宝石

gem 'act-fluent-logger-rails'
gem 'lograge'

由于在Fluentd的官网上提到,如果想要收集Rails应用程序的日志,请使用这个gem,所以我们选择了它。在Gemfile中写下后,只需运行bundle install来安装它。

在config/application.rb中添加日志定义。

请在config/application.rb中添加以下代码。

  config.log_level = :info
  config.logger = ActFluentLoggerRails::Logger.new
  config.lograge.enabled = true
  config.lograge.formatter = Lograge::Formatters::Json.new

我們在這裡將日誌以 JSON 格式輸出,而在正式環境中打算使用 Logstash 格式。

添加 config/fluent-logger.yml

创建一个包含定义连接配置的 YAML 文件,并将其放置在 config 目录中以供 fluentd 使用。

development:
  fluent_host:   '127.0.0.1'
  fluent_port:   24224
  tag:           'rails.log'
  messages_type: 'string'

test:
  fluent_host:   '127.0.0.1'
  fluent_port:   24224
  tag:           'rails.log'
  messages_type: 'string'

production:
  fluent_host:   '127.0.0.1'
  fluent_port:   24224
  tag:           'rails.log'
  messages_type: 'string'

本次是针对本地设置的,但在实际生产环境中,这里将变为预先配置好的fluentd服务器的IP地址和指定端口。

以上就是Rails端的设置完成了!

Fluentd 流畅的

接下来是Fluentd。这是Treasure data开发的日志收集工具。它可以处理各种格式的日志,并且是用Ruby编写的。它还提供了许多插件,如果需要的话,你也可以自己添加功能。这次我们将使用fluent-elasticsearch插件。

安装Fluentd

按照本机的页面的说明做,你就能做到。

gem install fluentd --no-ri --no-rdoc
fluentd --setup ./fluent

当执行到这一步时,将在fluent目录下创建一个名为fluent.conf的文件,然后按照以下内容进行编辑。

<source>
  type forward
  port 24224
  tag rails.log
</source>

<match rails.*>
  host localhost
  port 9201
  index_name fluentd
  type_name fluentd
  type elasticsearch
</match>

首先在source标签中编写接受fluentd处理的日志定义。本次的设置是通过fluentd在默认端口24224上接收rails的日志。接下来,在match标签中定义如何处理接收到的日志。本次设置是将日志存储在本地运行的elasticsearch中。

安装Elasticsearch插件。

输入以下命令安装elasticsearch插件。

gem install fluent-plugin-elasticsearch

在中文中,通过以下方式使用fluentd启动。使用-c选项指定之前创建的conf文件。

fluentd -c ./fluent/fluent.conf -vv &

弹性搜索

接下来是elasticsearch。安装elasticsearch需要Java。Java可以从oracle下载并安装。至于elasticsearch,可以从官方网站下载,或者如果你使用mac的话,也可以使用Homebrew来安装。这次我只是在本地的mac上做了测试,所以直接使用brew install elasticsearch进行了安装。

安装插件

为了支持日语,我们将添加elasticsearch-analysis-kuromoji插件。

plugin --install elasticsearch/elasticsearch-analysis-kuromoji/2.3.0

※内容待办:由于启动时出现错误,稍后进行调查。

为了能够使用浏览器确认elasticsearch的配置和数据是否已导入,我们还需要安装elasticsearch-head。

plugin --install mobz/elasticsearch-head

只需一种选择的汉语语义版本: 启动elasticsearch并打开localhost:9200/_plugin/head,即可查看elasticsearch的内容。

スクリーンショット 2015-03-01 0.26.23.png

Kibana4 可视化工具

我会从这里下载Kibana4。下载完成后,解压并打开config/kibana.yml,在顶部的elasticsearch_url字段中填写Elasticsearch的连接URL。

# Kibana is served by a back end server. This controls which port to use.
port: 5601

# The host to bind the server to.
host: "0.0.0.0"

# The Elasticsearch instance to use for all your queries.
elasticsearch_url: "http://localhost:9201"

# preserve_elasticsearch_host true will send the hostname specified in `elasticsearch`. If you set it to false,
# then the host you use to connect to *this* Kibana instance will be sent.
elasticsearch_preserve_host: true

# Kibana uses an index in Elasticsearch to store saved searches, visualizations
# and dashboards. It will create a new index if it doesn't already exist.
kibana_index: ".kibana"

# If your Elasticsearch is protected with basic auth, this is the user credentials
# used by the Kibana server to perform maintence on the kibana_index at statup. Your Kibana
# users will still need to authenticate with Elasticsearch (which is proxied thorugh
# the Kibana server)
# kibana_elasticsearch_username: user
# kibana_elasticsearch_password: pass
.....
以下省略
..

在写完之后,只需启动(bin/kibana)就可以查看存储在elasticsearch中的日志数据!!!!

スクリーンショット 2015-03-01 0.35.00.png

然后我发现仪表板的设置相比Kibana3变得更加困难,所以我稍微做了一些调查。首先我启动了并访问了localhost:5601。

    1. 将”Index contains time-based events”的复选框取消选中

 

    1. 在”Index name or pattern”中输入”fluentd”,然后点击”Create”按钮

 

    可能正在加载elasticsearch的设置,请点击左上角的”discover”按钮

如果你做到这一步,屏幕应该会变成像上面描述的那样。

我打算下次更深入地调查和写关于elasticsearch周边设置的内容。

bannerAds