fluentdをsystemdで管理する

6日目が少々反則的な内容となってしまいましたが、きにせず7日目行きたいと思います。今日は仕事からの帰りが遅かったので書き始めから日付を超えているというミラクル。もう、時間を気にせず書けます。

と、前置きはこれくらいにして本題に入りたいと思います。いままでのガッツリな感じ(?)とは異なり、時間の関係でサラッとしたネタです。参考にさせていただいたのはRHELやSystemdやDockerの説明では知らない人はいないであろう中井さんの下記サイトです。

enakai00.hatenablog.com


ログ収集をする場合にはデファクトスタンダードとなっているであろうfluentdをCentOS7などのSystemdなディストリビューションで起動/停止の管理をしたい場合の設定方法です。といっても、ひとまず動かしてみたレベルなので、問題もあるかもしれません。

通常はtd-agentをRPMからインストールしますし、まだCentOS7でもCentOS6の時のようにinitスクリプトで管理することもできますし、パッケージに付属しているのもinitスクリプトとなっています。ただ、私、結構変わり者でして、gem install fluentdとして使うのが好きだったりします(ツッコミたい気持ちは抑えてください)。

というわけで、rbenvでシステム全体にインストールしたRubyを使って、fluentdをSystemdの管理下で動かしてみたいと思います。

Fluentdのインストールと初期設定

jemallocは入れなくてもいいとも言われていますが、とりあえず入れる場合の手順です。入れない場合は、関連する部分を削っていただければ。jemallocはepelリポジトリが必要なので、先にepel-releaseをインストールしてリポジトリ情報を追加してから。
※パス周りがいろいろあるので、rootで作業しちゃいます。あしからず。

インストールと起動直前まで設定はこんな感じ。

# yum install epel-release -y
# yum install jemalloc -y 
# gem install fluentd --no-document
# useradd -M -s /bin/false fluentd
# mkdir /var/log/fluentd
# chown fluentd. /var/log/fluentd
# vim /etc/fluent/fluent.conf
# vim /etc/sysconfig/fluentd

/etc/fluent/fluent.conf

<source>
  type forward
</source>

@include conf.d/*.conf

<match fluent.**>
  type file
  path /var/log/fluentd/fluentd_internal.log
</match>

<match **>
  type file
  path /var/log/fluentd/else
  compress gz
</match>

/etc/sysconfig/fluentd

LD_PRELOAD=/usr/lib64/libjemalloc.so.1
FLUENTD_USER=fluentd
FLUENTD_GROUP=fluentd
FLUENTD_LOGFILE=/var/log/fluentd/fluentd.log

これが今日のメインともいうべきファイル。SystemdのUnitファイルと言うんですかね、設定ファイルはRPMでインストールしたものは /usr/lib/systemd/system/hoge.service に配置され、それを上書きをしたり自前で作る場合は /etc/systemd/system/hoge.service に配置します。

/etc/systemd/system/fluentd.service

[Unit]
Description=Fluentd daemon
After=network.service

[Service]
EnvironmentFile=/etc/sysconfig/fluentd
LimitNOFILE=65536
ExecStart=/usr/local/rbenv/versions/2.2.3/bin/fluentd --user ${FLUENTD_USER} --group ${FLUENTD_GROUP} --log ${FLUENTD_LOGFILE}
ExecStop=/bin/kill -INT ${MAINPID}
ExecReload=/bin/kill -HUP ${MAINPID}
Restart=always

[Install]
WantedBy=multi-user.target

それぞれの説明

  • Unit
    • Description: Unitの説明
    • After: このサービスの後に起動されるように
  • Service
    • EnvironmentFile: 環境変数を定義したファイル
    • LimitNOFILE: ulimit -fで指定するようにmax open fileを指定
    • ExecStart: 起動コマンド
    • ExecStop: 停止コマンド($MAINPIDでsystemdが管理しているpidを取得できる)
    • ExecReload: Reloadコマンド(同上)
    • Restart: プロセスの再起動条件(respawnと同じような感じ)
  • Install (enable/disableに関連する設定)
    • WantedBy: enable時にこのUnitのtargetディレクトリ(multi-user.target.wants)にsymlinkが作成される
      • multi-user.targetはこれまでの run-level 3に相当します。(run-level 5はgraphcal.target)

これにて起動してみましょう。

# systemctl enable fluentd
ln -s '/etc/systemd/system/fluentd.service' '/etc/systemd/system/multi-user.target.wants/fluentd.service'
# systemctl start fluentd
 systemctl status fluentd
fluentd.service - Fluentd daemon
   Loaded: loaded (/etc/systemd/system/fluentd.service; enabled)
   Active: active (running) since Mon 2015-12-07 16:30:42 UTC; 5s ago
 Main PID: 28406 (fluentd)
   CGroup: /system.slice/fluentd.service
           ├─28406 /usr/local/rbenv/versions/2.2.3/bin/ruby /usr/local/rbenv/versions/2.2.3/bin/fluentd --user fluentd --group fluentd --log /var/log/fluentd/fluentd.log
           └─28408 /usr/local/rbenv/versions/2.2.3/bin/ruby /usr/local/rbenv/versions/2.2.3/bin/fluentd --user fluentd --group fluentd --log /var/log/fluentd/fluentd.log

Dec 07 16:30:42 ip-10-11-0-199.localdomain systemd[1]: Starting Fluentd daemon...
Dec 07 16:30:42 ip-10-11-0-199.localdomain systemd[1]: Started Fluentd daemon.

無事起動しました。それでは、ちょっと設定ファイルを変えて、reloadしてみましょう。

# systemctl reload fluentd
# cat /vat/log/fluentd/fluentd.log
(一部抜粋)
2015-12-07 16:37:59 +0000 [info]: restarting
2015-12-07 16:37:59 +0000 [info]: reading config file path="/etc/fluent/fluent.conf"
2015-12-07 16:37:59 +0000 [info]: shutting down fluentd
2015-12-07 16:37:59 +0000 [info]: shutting down input type="forward" plugin_id="object:3fb66434acd8"
2015-12-07 16:37:59 +0000 [info]: shutting down input type="debug_agent" plugin_id="object:3fb66434a10c"
2015-12-07 16:37:59 +0000 [info]: shutting down output type="file" plugin_id="object:3fb66628a300"
2015-12-07 16:37:59 +0000 [info]: shutting down output type="stdout" plugin_id="object:3fb664310cb8"
2015-12-07 16:37:59 +0000 [info]: shutting down output type="file" plugin_id="object:3fb6662a89f4"
2015-12-07 16:37:59 +0000 [info]: process finished code=0
2015-12-07 16:37:59 +0000 [error]: fluentd main process died unexpectedly. restarting.
2015-12-07 16:37:59 +0000 [info]: starting fluentd-0.12.17
2015-12-07 16:37:59 +0000 [info]: gem 'fluentd' version '0.12.17'
2015-12-07 16:37:59 +0000 [info]: adding match pattern="debug.**" type="stdout"
2015-12-07 16:37:59 +0000 [info]: adding match pattern="fluent.**" type="file"
2015-12-07 16:37:59 +0000 [info]: adding match pattern="**" type="file"
2015-12-07 16:37:59 +0000 [info]: adding source type="forward"
2015-12-07 16:37:59 +0000 [info]: adding source type="debug_agent"
2015-12-07 16:37:59 +0000 [info]: using configuration file: <ROOT>
  <source>
    type forward
  </source>
  <source>
    type debug_agent
    port 24230
  </source>
  <match debug.**>
    type stdout
  </match>
  <match fluent.**>
    type file
    path /var/log/fluentd/fluentd_internal.log
    buffer_path /var/log/fluentd/fluentd_internal.log.*
  </match>
  <match **>
    type file
    path /var/log/fluentd/else
    compress gz
    buffer_path /var/log/fluentd/else.*
  </match>
</ROOT>
2015-12-07 16:37:59 +0000 [info]: listening fluent socket on 0.0.0.0:24224
2015-12-07 16:37:59 +0000 [info]: listening dRuby uri="druby://0.0.0.0:24230" object="Engine"
# systemctl status fluentd
(一部抜粋)
  Process: 28701 ExecReload=/bin/kill -HUP ${MAINPID} (code=exited, status=0/SUCCESS)

よさ気ですね。次に強制停止してみましょう。

# kill -9 28406
# systemctl status fluentd
fluentd.service - Fluentd daemon
   Loaded: loaded (/etc/systemd/system/fluentd.service; enabled)
   Active: active (running) since Mon 2015-12-07 16:45:32 UTC; 1s ago
  Process: 11982 ExecStop=/bin/kill -INT ${MAINPID} (code=exited, status=1/FAILURE)
 Main PID: 11986 (fluentd)
   CGroup: /system.slice/fluentd.service
           ├─11986 /usr/local/rbenv/versions/2.2.3/bin/ruby /usr/local/rbenv/versions/2.2.3/bin/fluentd --user fluentd --group fluentd --log /var/log/fluentd/fluentd.log
           └─11991 /usr/local/rbenv/versions/2.2.3/bin/ruby /usr/local/rbenv/versions/2.2.3/bin/fluentd --user fluentd --group fluentd --log /var/log/fluentd/fluentd.log

Dec 07 16:45:32 ip-10-11-0-199.localdomain systemd[1]: fluentd.service holdoff time over, scheduling restart.
Dec 07 16:45:32 ip-10-11-0-199.localdomain systemd[1]: Stopping Fluentd daemon...
Dec 07 16:45:32 ip-10-11-0-199.localdomain systemd[1]: Starting Fluentd daemon...
Dec 07 16:45:32 ip-10-11-0-199.localdomain systemd[1]: Started Fluentd daemon.

ちゃんと再起動されています。最後に正しく停止してみましょう。

# systemctl stop fluentd
# systemctl status fluentd
fluentd.service - Fluentd daemon
   Loaded: loaded (/etc/systemd/system/fluentd.service; enabled)
   Active: inactive (dead) since Mon 2015-12-07 16:48:50 UTC; 2s ago
  Process: 12004 ExecStop=/bin/kill -INT ${MAINPID} (code=exited, status=0/SUCCESS)
  Process: 11986 ExecStart=/usr/local/rbenv/versions/2.2.3/bin/fluentd --user ${FLUENTD_USER} --group ${FLUENTD_GROUP} --log ${FLUENTD_LOGFILE} (code=exited, status=0/SUCCESS)
 Main PID: 11986 (code=exited, status=0/SUCCESS)

Dec 07 16:45:32 ip-10-11-0-199.localdomain systemd[1]: fluentd.service holdoff time over, scheduling restart.
Dec 07 16:45:32 ip-10-11-0-199.localdomain systemd[1]: Stopping Fluentd daemon...
Dec 07 16:45:32 ip-10-11-0-199.localdomain systemd[1]: Starting Fluentd daemon...
Dec 07 16:45:32 ip-10-11-0-199.localdomain systemd[1]: Started Fluentd daemon.
Dec 07 16:48:49 ip-10-11-0-199.localdomain systemd[1]: Stopping Fluentd daemon...
Dec 07 16:48:50 ip-10-11-0-199.localdomain systemd[1]: Stopped Fluentd daemon.
# ps aux|grep [f]luentd
#

fluentdのログも見てましょう。

# cat /var/log/fluentd/fluentd.log
2015-12-07 16:48:49 +0000 [info]: shutting down fluentd
2015-12-07 16:48:49 +0000 [info]: shutting down input type="forward" plugin_id="object:3f92e82725a8"
2015-12-07 16:48:49 +0000 [info]: shutting down input type="debug_agent" plugin_id="object:3f92e634d4d4"
2015-12-07 16:48:50 +0000 [info]: shutting down output type="file" plugin_id="object:3f92e6312b7c"
2015-12-07 16:48:50 +0000 [info]: shutting down output type="file" plugin_id="object:3f92e63385fc"
2015-12-07 16:48:50 +0000 [info]: shutting down output type="stdout" plugin_id="object:3f92e63081b8"
2015-12-07 16:48:50 +0000 [info]: process finished code=0

はい、ちゃんと停止されましたね。めでたしめでたし。

おわりに

まだ設定をして軽く動作を確認して見た程度ですが、うまく行っていそうな感じではあります。以前からも今後も長くお世話になるfluentd、これから仲良くしていかなければならないSystemd、どちらもちゃんと使えるようになりたいですね。

これにて7日目の更新は終了です。それでは、また明日!