1. 安装Filebeat
在Debian上安装Filebeat有多种方式,推荐使用APT包管理器(官方源,便于后续更新):
sudo apt update
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
sudo apt update && sudo apt install filebeat -y
2. 配置Filebeat
Filebeat的主配置文件位于/etc/filebeat/filebeat.yml,需修改以下核心部分:
log类型的输入,指定要监控的日志文件路径(支持通配符*):filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log # 监控/var/log目录下所有.log文件
- /var/log/syslog # 可选:单独添加系统日志文件
hosts地址):output.elasticsearch:
hosts: ["localhost:9200"] # Elasticsearch服务器地址
index: "filebeat-%{+yyyy.MM.dd}" # 索引名称格式(按天分割)
sudo filebeat modules enable nginx
启用后会自动生成/etc/filebeat/modules.d/nginx.yml配置文件,无需手动编写路径。3. 启动并启用Filebeat服务
配置完成后,启动Filebeat服务并设置为开机自启:
sudo systemctl start filebeat # 启动服务
sudo systemctl enable filebeat # 开机自启
4. 验证配置与运行状态
sudo systemctl status filebeat
若状态显示为active (running),则表示服务已启动。sudo journalctl -u filebeat -f # 实时查看Filebeat日志
filebeat.yml无语法错误sudo filebeat test config -e
若输出Config OK,则配置正确。5. 可选:安全增强配置
filebeat.yml中配置:output.elasticsearch:
hosts: ["localhost:9200"]
ssl.certificate_authorities: ["/etc/filebeat/filebeat.crt"] # CA证书路径
ssl.certificate: "/etc/filebeat/filebeat.crt" # 客户端证书路径
ssl.key: "/etc/filebeat/filebeat.key" # 客户端私钥路径
filebeat.yml中添加:output.elasticsearch:
hosts: ["localhost:9200"]
username: "elastic" # Elasticsearch用户名(默认为elastic)
password: "your_password" # Elasticsearch密码
6. 验证数据采集
curl -XGET 'localhost:9200/filebeat-*/_search?pretty' # 查询所有filebeat索引的日志
http://localhost:5601),创建filebeat-*索引模式,进入Discover页面即可查看采集到的日志数据。