sudo yum update -y确保系统软件包为最新版本,避免兼容性问题。yum-utils(用于管理仓库)和wget(用于下载文件),命令:sudo yum install -y yum-utils wget。sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch。/etc/yum.repos.d/elastic.repo文件,添加以下内容(以7.x版本为例,可根据需求替换版本号):[elasticsearch]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
```。
sudo yum install -y filebeat,系统会自动下载并安装最新版本的Filebeat。filebeat-7.14.0-x86_64.rpm),命令:wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.14.0-x86_64.rpm。rpm命令安装,命令:sudo rpm -ivh filebeat-7.14.0-x86_64.rpm。/etc/filebeat/filebeat.yml,使用文本编辑器(如vi或nano)打开:sudo vi /etc/filebeat/filebeat.yml。filebeat.inputs section中定义要监控的日志文件,例如监控/var/log/*.log(所有系统日志):filebeat.inputs:
- type: log # 输入类型为日志
enabled: true # 启用该输入
paths: # 监控的日志文件路径
- /var/log/*.log
exclude_files: ['\.gz$'] # 排除.gz压缩文件(可选)
```。
localhost:9200):output.elasticsearch:
hosts: ["localhost:9200"] # Elasticsearch地址
index: "filebeat-%{+yyyy.MM.dd}" # 动态生成日期索引(如filebeat-2025.10.28)
```。
output.elasticsearch:
hosts: ["localhost:9200"]
username: "elastic" # Elasticsearch用户名(默认为elastic)
password: "your_password" # Elasticsearch密码
```。
setup.kibana:
host: "localhost:5601" # Kibana地址
```。
sudo systemctl start filebeat启动服务。sudo systemctl enable filebeat,确保系统重启后Filebeat自动启动。sudo systemctl status filebeat,确认服务运行状态(显示“active (running)”即为正常)。sudo tail -f /var/log/filebeat/filebeat,检查是否有错误信息或日志采集记录。_cat/indices API查看是否创建了Filebeat索引(如filebeat-2025.10.28),命令:curl -XGET 'localhost:9200/_cat/indices?v'。http://<服务器IP>:5601,进入Kibana的“Stack Management”→“Index Patterns”,检查是否能看到Filebeat索引。filestream输入类型:Filebeat 7.0及以上版本推荐使用filestream替代老旧的log输入,提升大文件处理效率和稳定性,配置示例如下:filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/*.log
parsers:
- ndjson: # 根据日志格式选择解析器(如ndjson、grok等)
```。
bulk_max_size(批量发送大小,默认为50MB),增大至200MB以提高传输效率;output.elasticsearch.compression: gzip),减少网络带宽占用;max_procs(最大CPU核心数),避免占用过多系统资源。logrotate工具管理Filebeat自身日志(/var/log/filebeat/filebeat),防止日志文件过大,示例配置:/var/log/filebeat/filebeat {
daily
rotate 7
compress
missingok
notifempty
}
```。