fluentdのforward
fluentdtd-agent間でログをやりとりするとき に使われるforwardについて。内部ではMessagePackを使っている。
forward input
http://docs.fluentd.org/articles/in_forward
受け取る側。
<source>
@type forward
port 24224
bind 0.0.0.0
</source>
<match **>
@type stdout
</match>
/etc/init.d/td-agent restart
してfluent-catで送ってみる。
$ echo '{"hoge": "fuga"}' | /opt/td-agent/embedded/bin/fluent-cat -h xx.xx.xx.xx test.tag
/var/log/td-agent/td-agent.log
に出力される。
test.tag: {"hoge":"fuga"}
forward output
http://docs.fluentd.org/articles/out_forward
http://docs.fluentd.org/articles/buffer-plugin-overview
送る側。
ポートはデフォルトで24224で、イベントの送信にTCPを、heartbeatにUDP(heartbeat_typeで変えられる)を使う。
flush_intervalははデフォルトで60秒。 確認のときは短くしておくと分かりやすい。 buffer_queueの一番上のチャンクがこの時間経過するか、サイズがbuffer_chunk_limitを超えると、一番下のチャンクが書き込まれ、新しいチャンクがpushされる。 chunkの数がbuffer_queue_limitに到達してしまうと新しいイベントは破棄されてしまうので リソースを圧迫(buffer_chunk_limit * buffer_queue_limit)しない程度に十分大きな数にしておき、 スパイクや障害時に備えておく。 buffer_typeはデフォルトがmemory。fileだとflush_at_shutdownのデフォルトがfalseなので注意。
<source>
@type forward
port 24224
bind 0.0.0.0
</source>
<match **>
@type forward
flush_interval 1s
buffer_type file
buffer_path /var/log/td-agent/forward-buf
flush_at_shutdown true
buffer_chunk_limit 256m
<server>
name log_server
host xx.xx.xx.xx
port 24224
</server>
</match>
冗長化
serverは複数書くことができ、 それぞれにweight(デフォルトは60)を設定したり、 standbyを付けることでActive-Standbyの構成にすることもできる。
<source>
@type forward
port 24224
bind 0.0.0.0
</source>
<match **>
@type forward
...
<server>
name log_server
host xx.xx.xx.xx
port 24224
weight 60
</server>
<server>
name log_server2
host yy.yy.yy.yy
port 24224
weight 60
</server>
</match>
片方のサーバーをtd-agentをstopしてstartしてみるとこんなログが出る。
detached forwarding server 'log_server2' host="yy.yy.yy.yy" port=24224 phi=16.06814271743242 phi_threshold=16
recovered forwarding server 'log_server2' host="yy.yy.yy.yy" port=24224
ちなみにtd-agentはrootで動かしている。
$ cat /etc/sysconfig/td-agent
TD_AGENT_USER=root
TD_AGENT_GROUP=root
fluentdのAggregatorをELBで負荷分散し、Blue/Green Deploymentする - sambaiz-net