fluentdをインストールする

Posted by Tatsuyano on Mon, Oct 20, 2014
In
Tags middleware, fluentd

blogのアクセス数がほんの少し増えてきたので、fluentd(フルーエントディ) をインストールしてみます。 今回、fluentd をインストールする環境は、以下になります。

  • AmazonLinux
  • nginx
  • ruby 2.1.0 (anyenv)

fluentd を導入すると何ができるか

fluentd はログなどを取得し、取得したデータを整形して elasticsearch + kibana などに渡すことで、データを簡単に可視化することができます。

fluentdをgem経由でインストールする

まずは公式どおりにgemでインストールしてみます。

注意
こちらの記事を参考に、yum経由でインストールも行いました。

$ gem install fluentd --no-ri --no-rdoc

メモリが割り当てられない?というエラーが出ました。
ERROR:  Error installing fluentd:
        ERROR: Failed to build gem native extension.

    $HOME/.anyenv/envs/rbenv/versions/2.1.0/bin/ruby extconf.rb
    Cannot allocate memory - $HOME/.anyenv/envs/rbenv/versions/2.1.0/bin/ruby extconf.rb 2>&1

Gem files will remain installed in $HOME/.anyenv/envs/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/yajl-ruby-1.2.1 for inspection.
Results logged to $HOME/.anyenv/envs/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/extensions/x86_64-linux/2.1.0-static/yajl-ruby-1.2.1/gem_make.out

試しに`aws-sdk`をインストールしてから、再度インストールしてみたらうまくいきました。
$ gem install aws-sdk
$ gem install fluentd --no-ri --no-rdoc

$ source $HOME/.zshrc
$ fluentd --version
fluentd 0.10.55

#### セットアップ 次はセットアップを行います。 指定した`fluent`のホームディレクトリ直下に`fluent.conf`というファイルが生成されます。
$ fluentd --setup app/fluent
Installed app/fluent/fluent.conf.

$ tree app/fluent
app/fluent
├── fluent.conf
└── plugin

#### 起動方法 試しに公式どおりに起動してみます。 `-vv`でトレースモード、`&`でバッググラウンドで起動します。 起動後にjsonを`fluent-cat`に渡して、`debug`タグの設定(stdout)どおり出力します。
$ fluentd -c app/fluent/fluent.conf -vv &
[1] 22209

$ echo '{"json":"message"}' | fluent-cat debug.test

2014-10-20 05:48:01 +0000 [trace]: plugin/in_forward.rb:189:initialize: accepted fluent socket from '127.0.0.1:44260': object_id=69925032642500
2014-10-20 05:48:01 +0000 debug.test: {"json":"message"}
2014-10-20 05:48:01 +0000 [trace]: plugin/in_forward.rb:245:on_close: closed fluent socket object_id=69925032642500

### Nginxのログをfluentdに流す `fluent.conf`には、ログの入力方法``と、出力方法``を設定することができます。 app/fluent/fluent.conf
# access_log
<source>
  type tail
  path /var/log/nginx/access.log
  format ltsv
  time_key time_local
  time_format %d/%b/%Y:%H:%M:%S %z
  pos_file /var/tmp/nginx_access_log.pos
  tag nginx.access
 </source>
 <match nginx.access>
  type elasticsearch
  host localhost
  port 9200
  type_name access_log
  logstash_format true
  logstash_prefix nginx_access
  logstash_dateformat %Y%m
  buffer_type memory
  buffer_chunk_limit 10m
  buffer_queue_limit 10
  flush_interval 1s
  retry_limit 16
  retry_wait 1s
  </match>
  
 # error_log
 <source>
  type tail
  path /var/log/nginx/error.log
  format /^(? <time>[^ ]+ [^ ]+) \[(? <log_level>.*)\] (? <pid>\d*).(? <tid>[^:]*): (? <message>.*)$/
  pos_file /var/tmp/nginx_error_log.pos
  tag nginx.error
 </source>
 <match nginx.error>
  type elasticsearch
  host localhost
  port 9200
  type_name error_log
  logstash_format true
  logstash_prefix nginx_error
  logstash_dateformat %Y%m
  buffer_type memory
  buffer_chunk_limit 10m
  buffer_queue_limit 10
  flush_interval 1s
  retry_limit 16
  retry_wait 1s
 </match>

今回、fluentd を自分の$HOME以下にインストールしているので、 pathに設定してあるログのパーミッションに注意してください。

また、formatをltsvに変更しているので、Nginx のログフォーマットも ltsv に合わせてください。

Nginxのログをltsvに変更する

/etc/nginx/nginx.conf

log_format  ltsv  "remote_addr:$remote_addr\t"
                  "remote_user:$remote_user\t"
                  "time_local:$time_local\t"
                  "request:$request\t"
                  "status:$status\t"
                  "body_bytes_sent:$body_bytes_sent\t"
                  "http_refer:$http_referer\t"
                  "http_user_agent:$http_user_agent";

access_log  /var/log/nginx/access.log  ltsv;

現在生成されている`access.log`を削除し、Nginx の再起動、及び fluentd を(バックエンドで)起動します。
$ sudo rm /var/log/nginx/access.log
$ sudo /etc/init.d/nginx restart

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

/tmp/nginx.logが更新されていれば成功です。

yum経由でのインストール

トレジャーデータ社が用意したyumリポジトリを使い、td-agent(fluentd) をインストールします。

$ sudo bash

cat > /etc/yum.repos.d/treasuredata.repo

[treasuredata] name=TreasureData baseurl=http://packages.treasure-data.com/redhat/$basearch gpgcheck=0

$ sudo yum -y install td-agent $ sudo chkconfig td-agent on


fluentd プラグインインストール

anyenvで管理していないrubyをupdateするのは少し気が引けます。。

$ sudo /usr/lib64/fluent/ruby/bin/fluent-gem update
$ sudo /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-config-expander
$ sudo /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-elasticsearch

configファイル

td-agentのconfigファイルは/etc/td-agent/td-agent.confにあるので、 以前作ったconfigファイルで上書きします。

すでにposファイルが生成されている場合は、削除してください。

$ sudo cp $HOME/app/fluentd/fluent.conf /etc/td-agent/td-agent.conf
$ sudo rm /var/tmp/nginx_access_log.pos
$ sudo rm /var/tmp/nginx_error_log

起動、停止

$ sudo /etc/init.d/td-agent start
$ sudo /etc/init.d/td-agent stop

参考記事