Fluentd不使用运行时的时区向Elasticsearch写入日志记录,怎么解决?

一、环境版本信息

Fluentd 1.1.0
Elasticsearch 6.3.0
fluent-plugin-elasticsearch 2.4.1

二、报错现象描述

以时区设置为Asia/Shanghai为例:
虽然Fluentd所在的运行时已经设置正确,但是上报到Elasticsearch对应索引中的日志记录,与实际相比还是相差八个小时。更直接的现象是,你发现明明应该归档在今天对应的索引中的日志,却归档在了昨天对应的索引中。再有就是你会发现,无论你怎么修改运行时的时区,Fluentd向Elasticsearch写入日志记录都始终使用UTC时区。

三、解决方法

把utc_index的值设置为false,允许Fluentd使用本地运行时设置的时区。

官方文档的原文描述如下:(详见参考资料部分的链接)
By default, the records inserted into index logstash-YYMMDD with UTC (Coordinated Universal Time). This option allows to use local time if you describe utc_index to false.

配置修改示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<match **>
@id elasticsearch
@type elasticsearch
@log_level info
include_tag_key true
host elasticsearch-ingest.logging
port 9200
logstash_format true
utc_index false
slow_flush_log_threshold 60.0
<buffer>
@type file
path /var/log/fluentd-buffers/kubernetes.system.buffer
flush_mode interval
retry_type exponential_backoff
flush_thread_count 2
flush_interval 5s
retry_forever
retry_max_interval 30
chunk_limit_size 6M
queue_limit_length 256
overflow_action block
</buffer>
</match>

四、参考资料

https://github.com/uken/fluent-plugin-elasticsearch/tree/v2.4.1#utc_index