在CentOS上优化Filebeat日志存储可以通过以下几个方面来实现:
首先,确保你的Filebeat配置文件(通常是/etc/filebeat/filebeat.yml)已经正确设置。以下是一些关键配置项:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
ignore_older: 7d
processors:
- add_cloud_metadata: ~
output.elasticsearch:
hosts: ["localhost:9200"]
index: "filebeat-%{+yyyy.MM.dd}"
降低Filebeat的日志级别可以减少磁盘I/O和CPU的使用。你可以在filebeat.yml中设置日志级别:
logging.level: info
确保你的日志文件使用了日志轮转(logrotate),这样可以避免日志文件过大。你可以创建一个/etc/logrotate.d/filebeat文件,并添加以下内容:
/var/log/filebeat/*.log {
daily
missingok
rotate 7
compress
notifempty
create 0640 root root
}
优化Elasticsearch的索引设置可以提高查询性能和存储效率。你可以通过调整索引模板来实现这一点。以下是一个示例索引模板:
PUT _template/filebeat
{
"index_patterns": ["filebeat-*"],
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"refresh_interval": "30s"
},
"mappings": {
"_source": {
"enabled": true
}
}
}
使用监控工具(如Prometheus和Grafana)来监控Filebeat和Elasticsearch的资源使用情况。根据监控数据,你可以调整Filebeat的配置参数,例如增加或减少processors的数量,调整queue.mem.events和queue.mem.flush.min_events等参数。
定期清理Elasticsearch中的旧数据可以释放存储空间。你可以使用Elasticsearch的Curator工具来自动化这个过程。以下是一个简单的Curator配置示例:
client:
hosts: localhost
port: 9200
url_prefix:
use_ssl: False
certificate:
client_cert:
client_key:
ssl_no_validate: False
http_auth:
timeout: 30
master_only: False
logging:
loglevel: INFO
logfile:
logformat: default
blacklist: []
action:
delete_indices: true
ignore_empty_list: true
timeout_override:
continue_if_exception: False
disable_action: false
filters:
- filtertype: pattern
kind: prefix
value: filebeat-
negate: false
clients:
- host: localhost
port: 9200
url_prefix:
use_ssl: False
certificate:
client_cert:
client_key:
ssl_no_validate: False
http_auth:
timeout: 30
master_only: False
然后运行Curator来删除旧索引:
curator --config /etc/curator.yml delete_indices.yml
通过以上步骤,你可以有效地优化Filebeat在CentOS上的日志存储。