分享

综合使用tail、forward、copy和stdout

 RealPython 2021-03-14

我们已经介绍了几个Fluentd的输入插件和输出插件,现在先通过一个例子进行阶段总结。

本示例使用到如下插件:

    in_tail, out_copy, out_stdout, out_forward, in_forward

out_copy和out_stdout参见本次推送的后两篇文章。

本示例包含两个节点:

node_forwarder

    使用in_tail收集nginx的access日志,将其输出到stdout,同时通过out_forward转发给节点node_aggregator。

node_aggregator

    使用in_forward接收节点node_forwarder转发的日志,将其输出到stdout。

这种部署方式采用的是官方建议的高可用架构

这两个节点位于同一服务器上,我们通过给td-agent指定不同的启动参数,来启动两个节点。

部署过程

首先,在服务器上部署nginx,并开启一个简单的http服务器。

nginx日志格式取默认值。

nginx日志文件路径为:/usr/local/openresty/nginx/logs/access.log

然后,在服务器上配置node_forwarder。

配置文件/etc/td-agent/node_forwarder.conf内容如下:

<source>  @type tail path /usr/local/openresty/nginx/logs/access.log pos_file /tmp/td-node_forwarder.pos tag td.nginx.access <parse> @type nginx </parse></source>
<match td.nginx.*> @type copy <store> @type stdout </store>  <store> @type forward <server> host 127.0.0.1     port 24224    </server> <secondary> @type file path /tmp/td-node_forwarder-failed.log </secondary>  </store></match>

启动node_forwarder:

td-agent -c /etc/td-agent/node_forwarder.conf --daemon /var/run/td-agent/node_forwarder.pid -o /tmp/td-node_forwarder.log

参数说明:

    -c:指定td-agent使用的配置文件

    --daemon:指定pid文件

    -o:指定运行日志文件路径

最后,在服务器上配置node_aggregator。

配置文件/etc/td-agent/node_aggregator.conf内容如下:

<source> @type forward</source>
<match td.nginx.*> @type stdout</match>

启动node_aggregator:

td-agent -c /etc/td-agent/node_aggregator.conf --daemon /var/run/td-agent/node_aggregator.pid -o /tmp/td-node_aggregator.log

测试过程

接下来,我们使用curl访问nginx:

curl http://127.0.0.1:8090/

得到如下日志:

127.0.0.1 - - [23/May/2020:18:43:09 +0800"GET / HTTP/1.1" 200 558 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"

然后分别查看这两个Fluentd节点的运行日志。

node_forwarder日志输出如下:

2020-05-23 18:43:09.000000000 +0800 td.nginx.access: {"remote":"127.0.0.1","host":"-","user":"-","method":"GET","path":"/","code":"200","size":"558","referer":"-","agent":"curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"}

node_aggregator日志输出如下:

2020-05-23 18:43:09.000000000 +0800 td.nginx.access: {"remote":"127.0.0.1","host":"-","user":"-","method":"GET","path":"/","code":"200","size":"558","referer":"-","agent":"curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"}

我们可以看到对nginx的访问被正确记录到了这两个节点中。

结语

通过本次测试示例,我们演示了in_tail、out_copy、out_stdout、out_forward 和 in_forward这几个插件的基本用法。

同时也使用到了parser插件用来解析nginx的默认日志。

这里通过组合out_copy和out_stdout给大家提供了一种调试Fluentd配置的思路,即:

在将日志发送到目的端的同时,将其写入本地标准输出(或运行日志),以观察当前节点是否正确处理了日志,从而缩小排查疑难的范围。

如果本文对您有帮助,不妨点下【在看】

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多