搭建Elasitc stack集群需要注意的日誌問題
搭建Elasitc stack集群時,我們往往把大部分注意力放在集群的搭建,索引的優化,分片的設置上等具體的調優參數上,很少有人會去關心Elasitc stack的日誌配置的問題,大概是覺得,日誌應該是一個公共的問題,默認的配置應該已經為我們處理好了。但很不幸,在不同的機器配置或者不同的運營策略下,如果採用默認的配置,會給我們帶來麻煩。
默認配置帶來的麻煩
以下例子是默認情況下,當Elasitc stack集群運行超過3個月之後的情況:
elasticsearch
elasticsearch默認情況下會每天rolling一個文件,當到達2G的時候,才開始清除超出的部分,當一個文件只有幾十K的時候,文件會一直累計下來。
logstash
一直增長的gc文件和不停增多的rolling日誌文件
kibana
默認日誌輸出到kibana.out文件當中,這個文件會變得越來越大
kafka
這裡提到kafka是因為在大部分的架構當中,我們都會用到kafka作為中間件數據緩衝區,因此不得不維護kafka集群。同樣,如果不做特定的配置,也會遇到日誌的問題:不停增多的rolling日誌文件
解決方案
因此,對於我們需要維護的這幾個組件,需要配置合理的日誌rotate策略。一個比較常用的策略就是時間+size,每天rotate一個日誌文件或者每當日誌文件大小超過256M,rotate一個新的日誌文件,並且最多保留7天之內的日誌文件。
elasticsearch
通過修改log4j2.properties文件來解決。該文件在/etc/elasticsesarch目錄下(或者config目錄)。
默認配置是:
appender.rolling.type = RollingFile
appender.rolling.name = rolling
appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %.-10000m%n
appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.log.gz
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size = 256MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.fileIndex = nomax
appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
appender.rolling.strategy.action.condition.type = IfFileName
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-*
appender.rolling.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize
appender.rolling.strategy.action.condition.nested_condition.exceeds = 2GB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
以上默認配置,會保存2GB的日誌,只有累計的日誌大小超過2GB的時候,才會刪除舊的日誌文件。
建議改為如下配置,僅保留最近7天的日誌
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
appender.rolling.strategy.action.condition.type = IfFileName
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-*
appender.rolling.strategy.action.condition.nested_condition.type = IfLastModified
appender.rolling.strategy.action.condition.nested_condition.age = 7D
1
2
3
4
5
6
7
logstash
與elasticsearch類似,通過修改log4j2.properties文件來解決。該文件在/etc/logstash目錄下(或者config目錄)。
默認配置是不會刪除歷史日誌的:
status = error
name = LogstashPropertiesConfig
appender.console.type = Console
appender.console.name = plain_console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c] %m%n
appender.json_console.type = Console
appender.json_console.name = json_console
appender.json_console.layout.type = JSONLayout
appender.json_console.layout.compact = true
appender.json_console.layout.eventEol = true
appender.rolling.type = RollingFile
appender.rolling.name = plain_rolling
appender.rolling.fileName = ${sys:ls.logs}/logstash-${sys:ls.log.format}.log
appender.rolling.filePattern = ${sys:ls.logs}/logstash-${sys:ls.log.format}-%d{yyyy-MM-dd}.log
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c] %-.10000m%n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
需手動加上:
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
1
2
3
4
5
6
7
kibana
在kibana的配置文件中,只有以下幾個選項:
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.
1
2
3
4
5
6
7
8
9
10
我們可以指定輸出的日誌文件與日誌內容,但是卻不可以配置日誌的rotate。這時,我們需要使用logrotate,這個linux默認安裝的工具。
首先,我們要在配置文件裡面指定生成pid文件:
pid.file: "pid.log"
1
然後,修改/etc/logrotate.conf:
/var/log/kibana {
missingok
notifempty
sharedscripts
daily
rotate 7
copytruncate
/bin/kill -HUP $(cat /usr/share/kibana/pid.log 2>/dev/null) 2>/dev/null
endscript
}
※天氣轉涼,程序員們趁機穿上了工作服,同事都看傻了
※9個絢麗多彩的HTML5進度條動畫賞析
TAG:程序員小新人學習 |