ElasticSearch集群日志限制问题

本文是基于CentOS7的环境下使用rpm包安装进行说明。ELK的默认日志记录会增加不少,除ElasticSearch外,都会无限增加,长时间运行可能带来灾难性的后果(如:节点宕机)。这就是咱们今天要面对的主要问题。主要策略为限制日志总量:时间+size,天天rotate一个日志文件或者每当日志文件大小超过256M,rotate一个新的日志文件,而且最多保留7天以内的日志文件。linux

ElasticSearch

默认配置问题

ElasticSearch默认状况下会天天rolling一个文件,当到达2G的时候,才开始清除超出的部分,当一个文件只有几十K的时候,文件会一直累计下来。app

解决方案

经过修改log4j2.properties文件来解决。该文件在/etc/elasticsesarch目录下,默认配置有以下设置ide

...
appender.rolling.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize
appender.rolling.strategy.action.condition.nested_condition.exceeds = 2GB 
...

该配置,会保存2GB的日志,只有累计的日志大小超过2GB的时候,才会删除旧的日志文件。建议更改成工具

...
appender.rolling.strategy.action.condition.nested_condition.type = IfLastModified
appender.rolling.strategy.action.condition.nested_condition.age = 7D 
...

仅保留最近7天的日志。ui

Logstash

默认配置问题

Logstash会一直增加gc文件和不停增多的rolling日志文件,而且不会删除。this

解决方案

经过修改log4j2.properties文件(/etc/logstash目录下),增长配置:日志

...
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basepath = ${sys:ls.logs}
appender.rolling.strategy.action.condition.type = IfFileName
appender.rolling.strategy.action.condition.glob = ${sys:ls.logs}/logstash-${sys:ls.log.format}
appender.rolling.strategy.action.condition.nested_condition.type = IfLastModified
appender.rolling.strategy.action.condition.nested_condition.age = 7D 
...

Kibana

默认配置问题

日志输出到kibana.out文件当中,这个文件会变得愈来愈大。code

解决方案

在kibana的配置文件中,只有如下几个选项:orm

logging.dest:
Default: stdout Enables you specify a file where Kibana stores log output.

logging.quiet:
Default: false Set the value of this setting to true to suppress all logging output other than error messages.

logging.silent:
Default: false Set the value of this setting to true to suppress all logging output.

logging.verbose:
Default: false Set the value of this setting to true to log all events, including system usage information and all requests. Supported on Elastic Cloud Enterprise.

logging.timezone
Default: UTC Set to the canonical timezone id (e.g. US/Pacific) to log events using that timezone. A list of timezones can be referenced at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones.

咱们能够指定输出的日志文件与日志内容,可是却不能够配置日志的rotate。这时,咱们须要使用logrotate,这个linux默认安装的工具。
首先,咱们要在配置文件里面指定生成pid文件:ip

pid.file: "pid.log"

而后,修改/etc/logrotate.conf:

/var/log/kibana {
    missingok
    notifempty
    shareds
    daily
    rotate 7
    copytruncate
    /bin/kill -HUP $(cat /usr/share/kibana/pid.log 2>/dev/null) 2>/dev/null
    end
}
相关文章
相关标签/搜索