下面给你一套 CentOS 上 Filebeat 对接 Kafka 的常见做法(以 RPM 安装方式为例,适用于 ELK + Kafka 日志架构)。
假设:
kafka1:9092,kafka2:9092,kafka3:9092app-logs# 以 8.x 为例
sudo rpm -ivh https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.13.0-x86_64.rpm
启动并开机自启:
systemctl enable filebeat
systemctl start filebeat
vi /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/app/*.log
output.kafka:
hosts: ["kafka1:9092", "kafka2:9092", "kafka3:9092"]
topic: "app-logs"
partition.round_robin:
reachable_only: true
required_acks: 1
compression: gzip
max_message_bytes: 1000000
paths:
- /var/log/app/*.log
支持通配符、多目录。
| 参数 | 说明 |
|---|---|
| hosts | Kafka broker 地址 |
| topic | 发送到哪个 topic |
| required_acks | 0 / 1 / -1(推荐 1 或 -1) |
| compression | none / gzip / snappy |
| partition.round_robin | 轮询分区 |
output.kafka:
hosts: ["kafka1:9092"]
topic: "%{[fields.log_topic]}"
input 中:
fields:
log_topic: app-logs
filebeat test config
filebeat test output
systemctl restart filebeat
systemctl status filebeat
journalctl -u filebeat -f
kafka-console-consumer.sh \
--bootstrap-server kafka1:9092 \
--topic app-logs \
--from-beginning
能看到 JSON 日志即成功。
9092Kafka 默认可自动创建(取决于 auto.create.topics.enable)
Filebeat 默认发送 JSON,含:
@timestampmessagehostfields如果你需要:
可以直接告诉我你的 Kafka 版本和安全方式。