一、安装前准备
sudo yum update -y确保CentOS系统为最新版本,避免兼容性问题。sudo yum install -y yum-utils安装包管理辅助工具,便于后续操作。二、下载并解压Filebeat
linux-x86_64),例如:wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.12.0-linux-x86_64.tar.gz。tar命令解压文件到/opt目录(便于系统管理):sudo tar -xzf filebeat-8.12.0-linux-x86_64.tar.gz -C /opt/。三、配置Filebeat
cd /opt/filebeat-8.12.0-linux-x86_64。vi)修改filebeat.yml,核心配置项如下:
log类型输入,指定要监控的日志路径(如/var/log/*.log表示监控/var/log下所有.log文件):filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
localhost:9200,远程实例替换为对应IP/域名):output.elasticsearch:
hosts: ["localhost:9200"]
setup.kibana部分,指定Kibana地址:setup.kibana:
host: "localhost:5601"
output.elasticsearch:
index: "filebeat-%{[agent.version]}-%{+yyyy.MM.dd}"
setup.template:
name: "filebeat"
pattern: "filebeat-*"
settings:
index.number_of_shards: 3
index.codec: best_compression
```。
四、设置开机自启动
vi创建/etc/systemd/system/filebeat.service文件,内容如下:[Unit]
Description=Filebeat
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
ExecStart=/opt/filebeat-8.12.0-linux-x86_64/filebeat -e -c /opt/filebeat-8.12.0-linux-x86_64/filebeat.yml
Restart=always
[Install]
WantedBy=multi-user.target
注:
ExecStart路径需与解压后的Filebeat目录一致。
sudo systemctl daemon-reload使新服务文件生效。sudo systemctl enable filebeatsudo systemctl start filebeat。五、验证部署
sudo systemctl status filebeat,若显示“active (running)”则表示服务启动成功。sudo tail -f /var/log/filebeat/filebeat查看Filebeat运行日志,确认无报错。GET _cat/indices?v,若看到以filebeat-开头的索引(如filebeat-8.12.0-2025.11.09),则说明Filebeat已成功将日志发送到Elasticsearch。六、常见问题解决
filebeat test config -e命令测试配置文件语法是否正确。/opt/filebeat-*/目录及filebeat.yml文件的权限正确(建议chmod -R 755 /opt/filebeat-*)。*.log被压缩为*.log.gz),需在filebeat.inputs中添加exclude_files: ['\.gz$'],避免重复采集。