在 Debian 上用 Filebeat 收集日志,一般流程如下(以 Elastic Stack / ELK 为例,也可以单独使用 Filebeat 输出到 Elasticsearch、Logstash、Kafka 等)。
# 1. 安装依赖
sudo apt update
sudo apt install curl gpg -y
# 2. 导入 Elastic GPG 密钥
curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic.gpg
# 3. 添加 Elastic 仓库
echo "deb [signed-by=/usr/share/keyrings/elastic.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" \
| sudo tee /etc/apt/sources.list.d/elastic-8.x.list
# 4. 安装 Filebeat
sudo apt update
sudo apt install filebeat -y
如果是 7.x,把
8.x改成7.x
配置文件路径:
/etc/filebeat/filebeat.yml
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/syslog
- /var/log/auth.log
或(老版本写法,仍可用):
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts: ["localhost:9200"]
# 如果启用了安全认证
# username: "elastic"
# password: "your_password"
output.logstash:
hosts: ["localhost:5044"]
Filebeat 自带很多常用日志模块:
filebeat modules list
启用模块(如 nginx):
filebeat modules enable nginx
配置模块:
/etc/filebeat/modules.d/nginx.yml
filebeat test config
filebeat test output
sudo systemctl enable filebeat
sudo systemctl start filebeat
systemctl status filebeat
filebeat.inputs:
- type: log
enabled: true
paths:
- /opt/myapp/logs/*.log
fields:
app: myapp
fields_under_root: true
filebeat.inputs:
- type: log
paths:
- /var/log/nginx/access.log
fields:
service: nginx
- type: log
paths:
- /var/log/mysql/error.log
fields:
service: mysql
journalctl -u filebeat -f
检查:
enabled: trueFilebeat 使用注册表(registry)记录读取位置:
/var/lib/filebeat/registry
不要随意删除。
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/syslog
output.elasticsearch:
hosts: ["localhost:9200"]
如果你愿意,我可以帮你:
告诉我你的 具体使用场景即可。