在CentOS环境下部署Filebeat时,可以参考以下最佳实践:
下载Filebeat:
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.14.0-linux-x86_64.tar.gz
解压文件:
tar -xzvf filebeat-7.14.0-linux-x86_64.tar.gz
cd filebeat-7.14.0-linux-x86_64
配置Filebeat:
编辑filebeat.yml
文件,配置要监控的日志文件路径和输出目标(如Elasticsearch)。
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts:
- "elasticsearch:9200"
设置开机自启动:
创建filebeat.service
文件并放置在/lib/systemd/system/
目录下。
[Unit]
Description=Filebeat
Wants=network-online.target
After=network-online.target
[Service]
User=root
ExecStart=/usr/local/filebeat/bin/filebeat -e -c /usr/local/filebeat/filebeat.yml
Restart=always
[Install]
WantedBy=multi-user.target
启动并启用Filebeat:
systemctl daemon-reload
systemctl start filebeat
systemctl enable filebeat
多行日志处理:
multiline.pattern: '^\['
multiline.negate: true
multiline.match: after
multiline.max_lines: 10000
JSON日志处理:
json.keys_under_root: true
json.overwrite_keys: true
json.message_key: log
json.add_error_key: true
内存队列优化:
queue.type: persisted
queue.max_bytes: 1024mb
flush.min_events: 2048
flush.timeout: 1s
filebeat.yml
中启用监控,将指标发送到Elasticsearch。monitoring.enabled: true
monitoring.elasticsearch.hosts: ["elasticsearch:9200"]
monitoring.elasticsearch.backoff.max: 180
monitoring.elasticsearch.metrics.period: 180
monitoring.elasticsearch.state.period: 600
内存溢出:
close_inactive
参数关闭不活跃文件、优化multiline
配置、增大queue.max_bytes
值。数据发送缓慢:
通过以上步骤和配置,可以在CentOS环境下高效地部署和运行Filebeat,确保日志数据的实时收集和传输。