CentOS 上 Filebeat 日志传输配置指南
一 安装与目录
- 使用 YUM 安装 Filebeat(以 7.x 为例,其他版本将版本号替换为你的实际版本):
- 导入 GPG 并安装:sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
- 创建仓库文件:sudo tee /etc/yum.repos.d/elastic.repo << ‘EOF’
[elastic-7.x]
name=Elastic 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
EOF
- 安装:sudo yum install -y filebeat
- 常用目录与命令:
- 配置文件:/etc/filebeat/filebeat.yml
- 服务管理:sudo systemctl start|stop|restart|enable|status filebeat
- 日志查看:sudo journalctl -u filebeat -f
- 数据存储(可选自定义):默认 /var/lib/filebeat,可在 filebeat.yml 中用 path.data 调整
二 最小可用配置
- 编辑配置文件:sudo vim /etc/filebeat/filebeat.yml
- 示例 1 输出到 Elasticsearch(本机):
- filebeat.inputs:
- type: log
enabled: true
paths:
- output.elasticsearch:
hosts: [“localhost:9200”]
index: “filebeat-%{[agent.version]}-%{+yyyy.MM.dd}”
- 示例 2 输出到 Logstash(推荐用于加工与转发):
- filebeat.inputs:
- type: log
enabled: true
paths:
- output.logstash:
hosts: [“your_logstash_host:5044”]
- 说明:
- 将 your_logstash_host 替换为实际的 Logstash 地址(可为远程主机)。
- 若 Elasticsearch 启用了安全认证,见下一节补充配置
三 安全与远程传输配置
- 启用 TLS/HTTPS 与认证(Elasticsearch 输出):
- output.elasticsearch:
- hosts: [“your_es_host:9200”]
- protocol: “https”
- ssl.verification_mode: “full” # 生产建议 full
- ssl.certificate_authorities: [“/path/to/ca.crt”]
- ssl.certificate: “/path/to/client.crt”
- ssl.key: “/path/to/client.key”
- username: “elastic”
- password: “your_password”
- 自签名证书测试场景(不推荐生产):
- ssl.verification_mode: “none”
- 远程日志收集思路:
- 在被采集的远程主机上各自安装 Filebeat,配置 output.elasticsearch 或 output.logstash 指向你的中央集群/Logstash;不要把远程日志文件直接通过 NFS/SMB 挂载到采集机再读取,避免文件句柄与一致性问题
四 启动与验证
- 语法检查与启动:
- sudo filebeat test config -e
- sudo systemctl restart filebeat && sudo systemctl enable filebeat
- 运行状态与日志:
- sudo systemctl status filebeat
- sudo journalctl -u filebeat -f
- 验证数据到达:
- Elasticsearch:curl -X GET “localhost:9200/_cat/indices?v” | grep filebeat
- Kibana:在 Dev Tools 执行 GET /_cat/indices?v 查看索引是否生成
- 网络与防火墙(目标为远程 ES/Logstash 时):
- 放行端口:sudo firewall-cmd --permanent --zone=public --add-port=9200/tcp(ES);或 5044/tcp(Logstash)
- 使配置生效:sudo firewall-cmd --reload
- 云主机需同时配置安全组入站规则
五 常用增强配置
- 添加自定义字段与元数据:
- processors:
- add_host_metadata: {}
- add_cloud_metadata: {}
- add_fields:
fields:
env: “prod”
app: “myapp”
- 解析 JSON 日志:
- processors:
- decode_json_fields:
fields: [“message”]
target: “”
overwrite_keys: true
- 使用模块与 Kibana 仪表板(可选):
- 启用模块:sudo filebeat modules enable system
- 若有独立 Kibana:setup.kibana: host: “your_kibana_host:5601”
- 初始化仪表板与模板:sudo filebeat setup(首次接入或变更索引模板时使用)