温馨提示×

centos如何编译安装filebeat

小樊
51
2026-08-18 20:50:55
栏目: 智能运维

在 CentOS 上编译安装 Filebeat,一般有两种方式:

  1. 官方推荐:直接用 RPM / 二进制(最简单,不建议编译)
  2. 从源码编译(适合定制或学习)

下面重点讲 从源码编译安装 Filebeat(基于 CentOS 7/8)。


一、准备环境(编译依赖)

1. 安装系统依赖

yum install -y git wget curl gcc make

2. 安装 Go 环境(必须)

Filebeat 是 Go 语言写的,需要 Go 1.20+(以 8.x 为例)。

下载 Go

wget https://go.dev/dl/go1.22.4.linux-amd64.tar.gz

解压并配置

tar -C /usr/local -xzf go1.22.4.linux-amd64.tar.gz

配置环境变量

编辑 /etc/profile 或 ~/.bashrc:

export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

生效:

source /etc/profile

验证:

go version

二、下载 Filebeat 源码

Elastic 官方仓库:

git clone https://github.com/elastic/beats.git
cd beats

切换到稳定版本(推荐)

git checkout v8.13.0

(版本号可到 https://github.com/elastic/beats/releases 查看)


三、编译 Filebeat

进入 filebeat 目录:

cd filebeat

编译

make

或:

go build -o filebeat .

编译完成后会生成:

./filebeat

四、安装到系统

1. 拷贝二进制文件

cp filebeat /usr/local/bin/
chmod +x /usr/local/bin/filebeat

2. 创建配置目录

mkdir -p /etc/filebeat
cp filebeat.yml /etc/filebeat/

3. 测试

filebeat version

五、配置 Filebeat(示例)

编辑 /etc/filebeat/filebeat.yml:

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

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

测试配置:

filebeat test config

六、配置 Systemd 服务(可选)

创建 /etc/systemd/system/filebeat.service:

[Unit]
Description=Filebeat
After=network.target

[Service]
ExecStart=/usr/local/bin/filebeat -c /etc/filebeat/filebeat.yml
Restart=always

[Install]
WantedBy=multi-user.target

启动:

systemctl daemon-reload
systemctl enable filebeat
systemctl start filebeat

七、常见问题

1. 编译失败(内存不足)

go build -ldflags "-s -w" -o filebeat .

或增加 swap。

2. CentOS 7 默认 GCC 太老

yum install centos-release-scl
yum install devtoolset-11
scl enable devtoolset-11 bash

3. 不建议生产环境编译

✅ 生产环境建议直接用官方 RPM:

rpm -ivh filebeat-8.x.rpm

八、总结

场景 建议
生产环境 ✅ RPM / 官方二进制
学习源码 ✅ 编译
定制功能 ✅ 编译
快速部署 ✅ 官方包

如果你愿意,我可以:

  • ✅ 给你 RPM 安装完整流程
  • ✅ 帮你 写一个 filebeat → ES → Kibana 示例
  • ✅ 帮你 排查编译错误

你现在用的是 CentOS 7 还是 8?

0 踩