在 CentOS 下,将 Filebeat 采集的日志输出到 Kafka 是非常常见的架构(Filebeat → Kafka → Logstash/消费者)。下面给你一个完整、可落地的配置示例和步骤说明。
192.168.1.10:9092)filebeat-logs)# 导入 Elastic GPG Key
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
# 创建 yum 源
cat > /etc/yum.repos.d/filebeat.repo <<EOF
[elastic-8.x]
name=Elastic repository for 8.x packages
baseurl=https://artifacts.elastic.co/packages/8.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
# 安装
yum install filebeat -y
/etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
- /opt/app/logs/*.log
# ===================== Kafka Output =====================
output.kafka:
enabled: true
hosts: ["192.168.1.10:9092"]
topic: "filebeat-logs"
partition.round_robin:
reachable_only: true
required_acks: 1
compression: gzip
max_message_bytes: 1048576
# ===================== 可选 =====================
processors:
- add_host_metadata:
when.not.contains:
tags: "no_host_metadata"
| 参数 | 说明 |
|---|---|
hosts |
Kafka broker 地址 |
topic |
Kafka topic(支持变量,如 %{[fields.log_topic]}) |
required_acks |
0 / 1 / -1(all),生产环境建议 1 或 -1 |
compression |
压缩方式:gzip / snappy / lz4 |
partition.round_robin |
轮询写入分区 |
max_message_bytes |
单条消息最大大小 |
output.kafka:
hosts: ["192.168.1.10:9092"]
topic: '%{[fields.log_topic]}'
filebeat.inputs:
- type: log
paths:
- /var/log/app.log
fields:
log_topic: app-logs
fields_under_root: false
filebeat test config
filebeat test output
systemctl enable filebeat
systemctl start filebeat
systemctl status filebeat
journalctl -u filebeat -f
# 消费 topic 数据
kafka-console-consumer.sh \
--bootstrap-server 192.168.1.10:9092 \
--topic filebeat-logs \
--from-beginning
你应该能看到 JSON 格式的日志数据。
9092 端口advertised.listeners 配置是否正确Exiting: error loading config
→ YAML 缩进错误(不能用 Tab)
max_message_bytes: 2097152
Filebeat → Kafka → Logstash → Elasticsearch
↓
Flink / 自研消费
如果你愿意,我可以帮你:
直接告诉我你的 Kafka 版本和使用场景即可。