在 CentOS 上使用 Filebeat 收集日志,一般流程如下(以 7.x / 8.x 版本为例,适用于 ELK/Elastic Stack 场景)。
# 导入 Elastic GPG 密钥
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
# 创建 yum 源
sudo tee /etc/yum.repos.d/filebeat.repo <<EOF
[elastic-8.x]
name=Elastic repository for 8.x packages
baseurl=https://artifacts.elastic.co/packages/8.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
如果你用的是 7.x,把
8.x改成7.x
# 安装
sudo yum install filebeat -y
配置文件路径:
/etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
- /var/log/nginx/access.log
output.elasticsearch:
hosts: ["http://localhost:9200"]
# 或输出到 Logstash
# output.logstash:
# hosts: ["localhost:5044"]
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/messages
- /var/log/secure
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
- /var/log/nginx/error.log
filebeat.inputs:
- type: log
enabled: true
paths:
- /opt/app/logs/app.log
multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
multiline.negate: true
multiline.match: after
output.elasticsearch:
hosts: ["http://192.168.1.10:9200"]
output.logstash:
hosts: ["localhost:5044"]
# 测试配置
filebeat test config
# 启动
sudo systemctl start filebeat
# 开机自启
sudo systemctl enable filebeat
# 查看状态
sudo systemctl status filebeat
# 查看日志
journalctl -u filebeat -f
# 或
tail -f /var/log/filebeat/filebeat
# 测试能否读取日志
filebeat test input
# 测试输出是否可达
filebeat test output
sudo chmod 644 /var/log/xxx.log
# 或
sudo usermod -aG root filebeat
getenforce
setenforce 0 # 临时关闭(不推荐生产)
日志文件
↓
Filebeat
↓
Elasticsearch / Logstash
↓
Kibana
如果你需要:
可以直接告诉我你的具体场景,我可以一步步给你定制 ✅