在 Debian 下对 Filebeat 进行调优,通常从 系统资源、Filebeat 配置、采集策略、输出目标 四个方面入手。下面给你一套实战向的优化建议,适合生产环境。
Filebeat 需要大量 fd 来监听日志文件,建议调大:
# 查看当前限制
ulimit -n
# 永久修改
sudo vim /etc/security/limits.conf
添加:
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
并确认:
sudo systemctl edit filebeat
添加:
[Service]
LimitNOFILE=65535
减少系统负载,避免 CPU steal。
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
scan_frequency: 10s # 默认 10s,可降低
harvester_buffer_size: 512k
max_bytes: 10MB # 单条日志最大大小
close_timeout: 5m # 防止 harvester 长时间占用
clean_inactive: 72h
clean_removed: true
filebeat.registry:
path: /var/lib/filebeat/registry
flush: 5s
避免频繁写磁盘:
filebeat.registry.flush: 5s
output.elasticsearch:
hosts: ["http://es1:9200"]
bulk_max_size: 2048
worker: 4
flush_interval: 1s
compression_level: 3
推荐参数解释:
| 参数 | 说明 |
|---|---|
| bulk_max_size | 批量提交大小 |
| worker | 并发 worker |
| flush_interval | 刷新间隔 |
| compression_level | gzip 压缩 |
output.logstash:
hosts: ["localhost:5044"]
worker: 4
bulk_max_size: 2048
pipelining: 2
❌ 不建议在 Filebeat 做复杂处理
✅ 只做:
例如:
multiline.pattern: '^\d{4}-\d{2}-\d{2}'
multiline.negate: true
multiline.match: after
filebeat modules disable *
只启用需要的模块。
sudo systemctl edit filebeat
[Service]
CPUQuota=50%
MemoryLimit=512M
processors:
- drop_fields:
fields: ["agent.ephemeral_id", "agent.hostname"]
systemctl status filebeat
journalctl -u filebeat -f
logging.level: debug
⚠️ 生产环境不要长期开启
filebeat.inputs:
- type: log
paths:
- /var/log/app/*.log
scan_frequency: 10s
harvester_buffer_size: 512k
max_bytes: 10MB
queue.mem:
events: 4096
flush.min_events: 512
flush.timeout: 1s
output.elasticsearch:
hosts: ["http://localhost:9200"]
bulk_max_size: 2048
worker: 4
| 问题 | 原因 | 解决 |
|---|---|---|
| CPU 高 | 太多 harvester | 限制文件数 |
| 内存高 | registry 大 | 清理 inactive |
| 不采集 | inode 变更 | 重启应用 |
| 延迟高 | bulk 太小 | 增大 bulk_max_size |
如果你愿意,可以告诉我:
我可以帮你给出 精确的一套配置。