一、安装Filebeat
sudo yum install epel-release
sudo yum install filebeat
filebeat-7.14.0-x86_64.rpm),然后通过rpm命令安装:wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.14.0-x86_64.rpm
sudo rpm -vi filebeat-7.14.0-x86_64.rpm
二、配置Filebeat核心参数
Filebeat的主配置文件位于/etc/filebeat/filebeat.yml,需重点修改以下部分:
log类型的输入,指定要监控的日志文件路径(支持通配符*)。例如,监控/var/log/messages、/var/log/secure及Nginx日志:filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/messages
- /var/log/secure
- /var/log/nginx/*.log
exclude_files: ['\.gz$'] # 排除.gz压缩文件
localhost:9200),并设置动态索引名称(按日期分割):output.elasticsearch:
hosts: ["localhost:9200"]
index: "filebeat-%{+yyyy.MM.dd}"
5044):output.logstash:
hosts: ["localhost:5044"]
fields为日志事件添加业务标签(如app_id):filebeat.inputs:
- type: log
enabled: true
paths: ["/var/log/app/*.log"]
fields:
app_id: "my_web_app"
env: "production"
fields_under_root: true # 将字段提升至事件顶层
filebeat.inputs:
- type: log
enabled: true
paths: ["/var/log/java/*.log"]
multiline:
pattern: '^\d{4}-\d{2}-\d{2}' # 匹配日志行首的日期格式
negate: true # 反向匹配(非日期开头的行视为多行延续)
match: after # 将延续行追加至上一行之后
三、启动与启用Filebeat服务
sudo systemctl start filebeat
sudo systemctl enable filebeat
sudo systemctl status filebeat
四、验证日志传输
sudo tail -f /var/log/filebeat/filebeat
filebeat-*索引(表示日志已成功接收):curl -X GET "localhost:9200/_cat/indices?v"
GET /filebeat-*/_search
{
"query": {
"match_all": {}
}
}
五、常见问题排查
sudo chmod -R 644 /var/log/*.log)或以root用户运行Filebeat(不推荐)。filebeat.yml后,可通过filebeat test config -e命令测试配置是否正确。sudo systemctl status elasticsearch),并确认防火墙是否放行9200端口。