在 Debian 上优化 Filebeat,通常可以从资源占用、采集性能、稳定性、日志处理效率几个方向入手。下面按“常见且实用”的方式给你一份优化清单。
不要只用 apt 默认版本(通常偏旧):
sudo apt remove filebeat
# 从 Elastic 官方源安装
官方源示例:
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo "deb https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
sudo apt update
sudo apt install filebeat
配置文件:/etc/filebeat/filebeat.yml
编辑:
sudo systemctl edit filebeat
示例:
[Service]
MemoryAccounting=yes
MemoryMax=300M
CPUQuota=50%
filebeat.inputs:
- type: log
paths:
- /var/log/*.log
harvester_buffer_size: 512KB # 默认 16KB,大日志可加大
close_inactive: 5m # 文件无活动多久关闭
clean_inactive: 72h
ignore_older: 24h
✅ 减少文件句柄占用
✅ 避免长期打开无变化文件
queue.mem:
events: 4096
flush.min_events: 1024
flush.timeout: 5s
output.elasticsearch:
hosts: ["http://es:9200"]
bulk_max_size: 2048
worker: 2
或 Logstash:
output.logstash:
hosts: ["logstash:5044"]
bulk_max_size: 2048
worker: 2
sudo filebeat modules disable *
只启用你需要的:
sudo filebeat modules enable system nginx
queue.disk(高可靠)queue.disk:
path: /var/lib/filebeat/diskqueue
max_size: 4GB
✅ 防止 ES 不可用时丢日志
✅ 降低内存压力
默认:
filebeat.registry.path: /var/lib/filebeat/registry
建议:
✅ 推荐:
dissectdrop_eventinclude_fields❌ 避免:
示例(丢弃 debug 日志):
processors:
- drop_event:
when:
regexp:
message: "^DEBUG"
multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
multiline.negate: true
multiline.match: after
monitoring.enabled: true
monitoring.elasticsearch:
hosts: ["http://es:9200"]
或:
logging.level: info
logging.to_files: true
# 文件句柄
ls -l /proc/$(pidof filebeat)/fd | wc -l
# CPU
top -p $(pidof filebeat)
# 队列堆积
filebeat test output
queue.disk:
path: /var/lib/filebeat/diskqueue
max_size: 4GB
filebeat.inputs:
- type: log
paths: ["/var/log/app/*.log"]
harvester_buffer_size: 512KB
close_inactive: 5m
ignore_older: 12h
output.elasticsearch:
hosts: ["http://es:9200"]
bulk_max_size: 2048
worker: 2
processors:
- drop_event:
when:
regexp:
message: "^DEBUG"
如果你愿意,可以告诉我:
我可以直接帮你写一份可落地的 filebeat.yml。