本文是基於CentOS7的環境下使用rpm包安裝進行說明。ELK的默認日誌記錄會增加不少,除ElasticSearch外,都會無限增加,長時間運行可能帶來災難性的後果(如:節點宕機)。這就是咱們今天要面對的主要問題。主要策略爲限制日誌總量:時間+size,天天rotate一個日誌文件或者每當日誌文件大小超過256M,rotate一個新的日誌文件,而且最多保留7天以內的日誌文件。linux
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會一直增加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.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 }