温馨提示×

CentOS Filebeat如何对接ES

小樊
36
2026-07-20 08:16:21
栏目: 智能运维

下面以 CentOS 7/8 为例,说明 Filebeat 对接 Elasticsearch(ES) 的完整步骤,适用于常见场景(Filebeat → ES,不使用 Logstash)。


一、环境准备

假设:

  • OS:CentOS 7 / 8
  • Filebeat 版本:与 ES 版本一致(强烈建议)
  • ES 已部署并可访问(如 http://es-node:9200

二、安装 Filebeat(CentOS)

方式一:使用 RPM(推荐)

# 导入 Elastic GPG Key
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

# 创建 yum 源
sudo tee /etc/yum.repos.d/elastic.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

如果是 ES 7.x,把 8.x 改成 7.x

sudo yum install filebeat -y

三、配置 Filebeat 对接 ES

1️⃣ 主配置文件

编辑:

vim /etc/filebeat/filebeat.yml

最小可用配置示例:

filebeat.inputs:
  - type: log
    enabled: true
    paths:
      - /var/log/*.log

output.elasticsearch:
  hosts: ["http://192.168.1.10:9200"]
  username: "elastic"
  password: "your_password"

setup.kibana:
  host: "http://192.168.1.10:5601"

✅ 说明:

  • paths:要采集的日志路径
  • hosts:ES 地址
  • 如果 ES 没开启安全,可删除 username/password
  • setup.kibana 可选(用于自动创建索引模板)

2️⃣ 如果 ES 使用 HTTPS(常见)

output.elasticsearch:
  hosts: ["https://192.168.1.10:9200"]
  ssl.verification_mode: none

或:

  ssl.certificate_authorities: ["/etc/filebeat/ca.crt"]

四、初始化 Filebeat(重要)

filebeat setup

作用:

  • 自动创建 ES 索引模板
  • 创建 Kibana Dashboard(如需要)

⚠️ 只运行一次即可


五、启动并设置开机自启

systemctl enable filebeat
systemctl start filebeat
systemctl status filebeat

六、验证是否成功

1️⃣ 查看 ES 索引

curl -u elastic:password http://192.168.1.10:9200/_cat/indices?v

你会看到类似:

filebeat-8.11.0-2026.01.21-000001

2️⃣ 查看数据

curl -u elastic:password http://192.168.1.10:9200/filebeat-*/_search?pretty

或在 Kibana → Discover 中查看。


七、常见问题排查

❌ Filebeat 启动失败

journalctl -u filebeat -f

或:

filebeat test config
filebeat test output

❌ 没有数据

  • 确认日志文件有新增内容
  • 确认 enabled: true
  • 检查权限:
ls -l /var/log/*.log

八、常见扩展

多日志路径

paths:
  - /var/log/nginx/*.log
  - /var/log/app/*.log

添加自定义字段

fields:
  env: prod
fields_under_root: true

九、架构示意

日志文件
   ↓
Filebeat (CentOS)
   ↓
Elasticsearch
   ↓
Kibana

如果你需要:

  • Filebeat → Logstash → ES
  • 多节点 ES
  • 容器化部署
  • 日志解析(grok / dissect)

可以直接告诉我,我可以给你对应配置示例 ✅

0