温馨提示×

Filebeat怎么连接CentOS的ES

小樊
53
2026-08-03 21:35:42
栏目: 智能运维

下面分 常见场景 给你说明 Filebeat 如何连接 CentOS 上的 Elasticsearch(ES),从 最简单生产推荐 的方式都有。


一、前提说明

  • CentOS:运行 Elasticsearch 的服务器
  • Filebeat:可以运行在 同一台机器,也可以运行在 其他机器
  • 关键点:
    • ES 的 IP + 端口(默认 9200)
    • ES 是否开启 安全认证(xpack)
    • 网络是否通(防火墙 / 云安全组)

二、最简单方式(无安全认证,同机或内网)

1️⃣ 确认 ES 正常运行

curl http://localhost:9200

返回类似:

{
  "name" : "centos-node",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "8.11.0"
  }
}

✅ 说明 ES 正常


2️⃣ 安装 Filebeat(CentOS)

sudo yum install -y filebeat

或手动下载:

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.11.0-x86_64.rpm
sudo rpm -ivh filebeat-8.11.0-x86_64.rpm

3️⃣ 修改 Filebeat 配置

编辑配置文件:

sudo vi /etc/filebeat/filebeat.yml

示例(无认证)

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

output.elasticsearch:
  hosts: ["http://localhost:9200"]

✅ 如果 ES 在 另一台机器

output.elasticsearch:
  hosts: ["http://192.168.1.10:9200"]

4️⃣ 启动 Filebeat

sudo systemctl enable filebeat
sudo systemctl start filebeat

查看状态:

systemctl status filebeat

5️⃣ 验证数据是否写入 ES

curl http://localhost:9200/_cat/indices?v

你应该能看到 filebeat-* 索引。


三、ES 开启安全认证(8.x 默认开启)

1️⃣ 确认 ES 有用户名密码

通常:

  • 用户名:elastic
  • 密码:安装时生成或 elasticsearch-reset-password

2️⃣ Filebeat 配置(带认证)

output.elasticsearch:
  hosts: ["https://localhost:9200"]
  username: "elastic"
  password: "你的密码"
  ssl:
    enabled: true
    verification_mode: none   # 测试用,生产建议用证书

⚠️ 生产环境不要用 verification_mode: none


四、ES 监听地址问题(非常常见)

默认 ES 只监听 127.0.0.1

如果 Filebeat 在 另一台机器,需要修改:

sudo vi /etc/elasticsearch/elasticsearch.yml
network.host: 0.0.0.0
http.port: 9200

然后重启 ES:

sudo systemctl restart elasticsearch

五、防火墙放行 9200(CentOS 7/8)

sudo firewall-cmd --add-port=9200/tcp --permanent
sudo firewall-cmd --reload

六、推荐架构(生产)

Filebeat → Logstash → Elasticsearch

Filebeat 配置示例(到 Logstash)

output.logstash:
  hosts: ["localhost:5044"]

七、排错常用命令

Filebeat 日志

journalctl -u filebeat -f

或:

tail -f /var/log/filebeat/filebeat

测试 ES 是否可达

curl -u elastic:密码 http://es_ip:9200

八、总结(速查表)

场景 配置重点
同机无认证 http://localhost:9200
跨机器 ES network.host: 0.0.0.0
有认证 username/password
HTTPS ssl.enabled: true
防火墙 放行 9200

如果你愿意,可以直接告诉我:

  • ✅ ES 版本(7.x / 8.x)
  • ✅ 是否开启安全认证
  • ✅ Filebeat 和 ES 是否同一台机器

我可以给你 完全可用的配置文件

0