1. 安装Filebeat
sudo apt update,确保系统包信息是最新的。wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -,用于验证Elastic软件包的完整性。echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list,将Elastic的APT源添加到系统源列表(可根据需求替换为其他版本,如6.x)。sudo apt update,同步Elastic源的最新包信息。sudo apt install filebeat -y,自动安装Filebeat及其依赖。2. 配置Filebeat
nano)打开默认配置文件/etc/filebeat/filebeat.yml,命令为sudo nano /etc/filebeat/filebeat.yml。filebeat.inputs部分添加要采集的日志路径,例如收集系统日志和Apache日志:filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/syslog
- /var/log/apache2/*.log
若需忽略旧文件或特定行,可添加ignore_older: 72h(忽略72小时以上的文件)或exclude_lines: ['ERROR', 'WARN'](排除包含“ERROR”“WARN”的行)。output.elasticsearch:
hosts: ["localhost:9200"]
index: "filebeat-%{[agent.version]}-%{+yyyy.MM.dd}"
output.logstash:
hosts: ["localhost:5044"]
system、nginx、mysql),可快速配置常见日志类型的采集和处理。例如启用system模块:sudo filebeat modules enable system
启用后会自动生成对应的输入配置。3. 启动并启用Filebeat服务
sudo systemctl start filebeat,立即启动Filebeat进程。sudo systemctl enable filebeat,确保系统重启后Filebeat自动启动。4. 验证部署
sudo systemctl status filebeat,若显示“active (running)”则表示服务已正常运行。sudo journalctl -u filebeat -f实时查看Filebeat的运行日志,排查可能的错误(如无法连接到Elasticsearch)。curl -X GET "localhost:9200/_cat/indices?v"命令查看是否存在filebeat-*索引;若输出到Logstash,可查看Logstash日志(通常位于/var/log/logstash/logstash-plain.log)确认是否接收到事件。