Beats概述
Beats是Elastic公司开发的轻量级数据采集器系列,用于将数据从各种来源发送到Elasticsearch或Logstash。相比Logstash,Beats更轻量级,资源占用更少,适合在生产环境中部署。
Beats系列
# Beats系列工具
beats_tools = {
"Filebeat": "日志文件采集",
"Metricbeat": "系统和应用指标采集",
"Packetbeat": "网络数据包分析",
"Heartbeat": "服务可用性监控",
"Auditbeat": "审计数据采集",
"Functionbeat": "无服务器函数数据采集",
"Journalbeat": "systemd日志采集",
"Winlogbeat": "Windows事件日志采集"
}
Filebeat配置
1. 基础配置
# filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/app/*.log
fields:
log_type: application
fields_under_root: false
output.elasticsearch:
hosts: ["http://localhost:9200"]
index: "filebeat-%{+yyyy.MM.dd}"
setup.template.settings:
index.number_of_shards: 1
index.codec: best_compression
2. 多输入配置
# Filebeat多输入配置
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/app/*.log
fields:
log_type: application
multiline.pattern: '^\d{4}-\d{2}-\d{2}'
multiline.negate: true
multiline.match: after
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
fields:
log_type: nginx_access
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
fields:
log_type: nginx_error
output.elasticsearch:
hosts: ["http://localhost:9200"]
index: "%{[fields.log_type]}-%{+yyyy.MM.dd}"
3. 输出到Logstash
# Filebeat输出到Logstash
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/app/*.log
output.logstash:
hosts: ["localhost:5044"]
# Logstash配置
input {
beats {
port => 5044
}
}
output {
elasticsearch {
hosts => ["http://localhost:9200"]
index => "logs-%{+YYYY.MM.dd}"
}
}
Metricbeat监控
1. 系统监控
# metricbeat.yml
metricbeat.modules:
- module: system
metricsets:
- cpu
- load
- memory
- network
- process
- process_summary
- socket_summary
- filesystem
- fsstat
enabled: true
period: 10s
output.elasticsearch:
hosts: ["http://localhost:9200"]
index: "metricbeat-%{+yyyy.MM.dd}"
2. 应用监控
# Metricbeat应用监控
metricbeat.modules:
- module: apache
metricsets:
- status
period: 10s
hosts: ["http://localhost/server-status"]
- module: nginx
metricsets:
- stubstatus
period: 10s
hosts: ["http://localhost/nginx_status"]
- module: mysql
metricsets:
- status
- galera_status
period: 10s
hosts: ["tcp(127.0.0.1:3306)/"]
3. Docker监控
# Metricbeat Docker监控
metricbeat.modules:
- module: docker
metricsets:
- container
- cpu
- diskio
- healthcheck
- info
- memory
- network
hosts: ["unix:///var/run/docker.sock"]
period: 10s
Packetbeat网络分析
1. 基础配置
# packetbeat.yml
packetbeat.interfaces.device: any
packetbeat.protocols:
- type: http
ports: [80, 8080, 8000, 5000, 8002, 8081, 9200]
- type: mysql
ports: [3306]
- type: redis
ports: [6379]
output.elasticsearch:
hosts: ["http://localhost:9200"]
index: "packetbeat-%{+yyyy.MM.dd}"
2. 网络流量分析
# Packetbeat网络流量分析
packetbeat.interfaces.device: any
packetbeat.protocols:
- type: http
ports: [80, 443]
send_request: true
send_response: true
hide_keywords: ["password", "passwd", "pwd"]
- type: dns
ports: [53]
output.elasticsearch:
hosts: ["http://localhost:9200"]
index: "packetbeat-%{+yyyy.MM.dd}"
Heartbeat监控
1. 服务可用性监控
# heartbeat.yml
heartbeat.monitors:
- type: http
urls:
- http://localhost:8080/health
- http://localhost:8080/api/status
schedule: '@every 10s'
timeout: 5s
- type: tcp
hosts:
- localhost:3306
schedule: '@every 30s'
timeout: 5s
output.elasticsearch:
hosts: ["http://localhost:9200"]
index: "heartbeat-%{+yyyy.MM.dd}"
Beats最佳实践
1. 性能优化
# Beats性能优化
性能优化建议:
批量大小: "增加批量大小减少网络请求"
压缩: "启用压缩减少网络带宽"
索引模板: "使用索引模板优化存储"
多实例: "使用多个Beats实例处理大量数据"
2. 配置管理
# Beats配置管理
配置管理建议:
版本控制: "使用Git管理配置文件"
环境分离: "开发、测试、生产环境分离"
配置模板: "使用配置模板减少重复"
监控配置: "监控配置变更"
3. 安全配置
# Beats安全配置
安全配置:
SSL/TLS: "启用SSL/TLS加密传输"
认证: "使用用户名密码或API密钥"
权限控制: "限制Beats访问权限"
日志审计: "记录Beats操作日志"
实际应用案例
1. 日志采集架构
# 日志采集架构
架构设计:
应用层: "Filebeat采集应用日志"
系统层: "Filebeat采集系统日志"
网络层: "Packetbeat分析网络流量"
监控层: "Metricbeat监控系统指标"
数据流向:
Filebeat → Logstash → Elasticsearch
Metricbeat → Elasticsearch
Packetbeat → Elasticsearch
2. 监控仪表板
# 监控仪表板配置
仪表板组件:
系统指标:
- CPU使用率
- 内存使用率
- 磁盘IO
- 网络流量
应用指标:
- 请求量
- 响应时间
- 错误率
服务可用性:
- HTTP健康检查
- TCP连接检查
总结
Beats轻量级数据采集器的关键要点:
- Beats概述:Beats系列工具、适用场景
- Filebeat:日志文件采集、多输入配置、输出配置
- Metricbeat:系统监控、应用监控、Docker监控
- Packetbeat:网络数据包分析、流量分析
- Heartbeat:服务可用性监控
- 最佳实践:性能优化、配置管理、安全配置
- 实际应用:日志采集架构、监控仪表板
掌握Beats工具,可以构建轻量级、高效的数据采集系统,为ELK技术栈提供可靠的数据源。
转载请注明:周志洋的博客 » Beats轻量级数据采集器


