优化 CentOS 上的 Filebeat 性能,主要目标是提升吞吐量、降低延迟、减少资源消耗。下面从系统层、Filebeat 配置层、日志采集方式、输出层几个维度给你一套实战优化方案。
Filebeat 会为每个日志文件打开一个 fd,很容易达到默认限制。
# 查看当前限制
ulimit -n
# 永久修改
vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
重新登录或重启生效。
SELinux 会影响 Filebeat 对日志文件的读取。
getenforce
setenforce 0
永久关闭:
vi /etc/selinux/config
SELINUX=disabled
vi /etc/sysctl.conf
fs.inotify.max_user_watches = 524288
fs.inotify.max_user_instances = 1024
net.core.somaxconn = 1024
sysctl -p
编辑:
vi /etc/filebeat/filebeat.yml
✅ 关闭无用模块
filebeat modules list
filebeat modules disable *
✅ 关闭 unused fields
processors:
- drop_fields:
fields: ["agent", "ecs", "host", "log", "input"]
scan_frequency: 30s # 默认 10s,可适当增大
harvester_buffer_size: 32kb # 默认 16kb
避免频繁写磁盘:
registry:
flush: 30s
harvester_limit: 10
❌ 不推荐:
paths:
- /var/log/*
✅ 推荐:
paths:
- /var/log/nginx/access.log
- /var/log/app/*.log
如果日志是 JSON:
json.keys_under_root: true
json.add_error_key: true
ignore_older: 48h
output.elasticsearch:
hosts: ["http://es1:9200"]
worker: 4
bulk_max_size: 2048
compression_level: 3
参数说明:
| 参数 | 建议 |
|---|---|
| worker | CPU 核数 |
| bulk_max_size | 1024–4096 |
| compression_level | 1–5 |
output.logstash:
hosts: ["logstash:5044"]
worker: 4
bulk_max_size: 2048
pipelining: 2
✅ 默认内存队列(通用):
queue.mem:
events: 4096
flush.min_events: 512
flush.timeout: 1s
✅ 高可靠场景(磁盘队列):
queue.disk:
path: /var/lib/filebeat/diskqueue
max_size_in_bytes: 1GB
monitoring.enabled: true
filebeat.harvester.runningfilebeat.events.totalfilebeat.events.droppedpublish.eventsfilebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/app/*.log
ignore_older: 24h
scan_frequency: 30s
harvester_buffer_size: 32kb
harvester_limit: 10
queue.mem:
events: 4096
flush.min_events: 512
flush.timeout: 1s
output.elasticsearch:
hosts: ["http://localhost:9200"]
worker: 4
bulk_max_size: 2048
如果你愿意,可以告诉我:
我可以给你定制一份 Filebeat 性能优化配置。