在 CentOS 上把 Filebeat 采集的日志发送到 Kafka,一般步骤如下(以 CentOS 7/8 + Filebeat 7.x/8.x 为例):
# 导入 Elastic GPG
sudo rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
# 添加 yum 源(以 8.x 为例)
sudo tee /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
sudo yum install filebeat -y
vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
# 可添加多个路径
# - /opt/app/logs/*.log
output.kafka:
# Kafka 集群地址
hosts: ["192.168.1.10:9092", "192.168.1.11:9092"]
# Kafka topic(可写死或用变量)
topic: filebeat-logs
# 可选
partition.round_robin:
reachable_only: true
required_acks: 1
compression: gzip
max_message_bytes: 1000000
✅ 说明
hosts:Kafka broker 地址topic:Kafka 主题(需提前创建或允许自动创建)required_acks:0 / 1 / -1(生产建议 1 或 -1)在 Kafka 服务器上创建 topic:
bin/kafka-topics.sh \
--create \
--topic filebeat-logs \
--bootstrap-server localhost:9092 \
--partitions 3 \
--replication-factor 1
如果 Kafka 开启了 自动创建 topic,可跳过。
# 测试配置
filebeat test config
filebeat test output
# 启动
systemctl enable filebeat
systemctl start filebeat
# 查看状态
systemctl status filebeat
bin/kafka-console-consumer.sh \
--topic filebeat-logs \
--bootstrap-server localhost:9092 \
--from-beginning
如果能看到 JSON 格式日志,说明成功 ✅
firewall-cmd --add-port=9092/tcp --permanentlisteners 配置是否对外暴露ls -l /var/log/xxx.log
output.kafka:
hosts: ["kafka:9092"]
topic: '%{[fields.log_topic]}'
并在 input 中:
fields:
log_topic: app-log
如果你有:
我可以直接给你一份可用配置模板。