在 CentOS 上使用 Filebeat 收集日志,一般流程如下(以 Elastic Stack / ELK 为例,也适用于直接输出到 Kafka、Logstash、ES 等)。
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
sudo cat > /etc/yum.repos.d/filebeat.repo <<EOF
[filebeat]
name=Elasticsearch 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
sudo yum install filebeat -y
如果是 7.x,把
8.x改成7.x
配置文件位置:
/etc/filebeat/filebeat.yml
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/messages
- /var/log/secure
- /var/log/*.log
output.elasticsearch:
hosts: ["http://localhost:9200"]
username: "elastic"
password: "your_password"
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /app/logs/app/*.log
fields:
app: myapp
fields_under_root: true
output.logstash:
hosts: ["logstash:5044"]
| 类型 | 说明 |
|---|---|
filestream |
新版本推荐(7.14+) |
log |
老版本常用 |
- type: log
paths:
- /var/log/nginx/access.log
systemctl enable filebeat
systemctl start filebeat
systemctl status filebeat
查看日志:
journalctl -u filebeat -f
curl -u elastic:password http://localhost:9200/_cat/indices
filebeat-*multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
multiline.negate: true
multiline.match: after
paths:
- /var/lib/docker/containers/*/*.log
output.kafka:
hosts: ["kafka:9092"]
topic: filebeat-logs
chmod 644 /var/log/messages
usermod -aG adm filebeat
enabled: truefilebeat test configfilebeat test output如果你有 具体日志路径 / 输出目标(ES / Kafka / Logstash),我可以帮你写一份完整配置。