我们在生产系统中大量使用ELK堆栈进行日志聚合和监视。
我们每天生成的日志大小约为100GB。由于我们不打算将日志文件保存在ELK中超过一个月,因此删除这些索引以释放磁盘空间变得很重要。
幸运的是,默认情况下,Logstash每天都会创建一个新索引。
因此,我们可以要求Curator删除x天前且遵循特定命名模式的索引。
安装Curator
简单易用pip install elasticsearch-curator,可在您的计算机上安装Curator。我更喜欢将其安装在Elasticsearch机器本身上。
配置Curator
创建一个curator.yml包含以下内容的文件。
---
client:
hosts:
- 127.0.0.1
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: ['elasticsearch', 'urllib3']
现在,我们需要定义一个动作。即Curator将做什么。有很多动作可供选择。查看文档以获取更多信息
别名
分配
关
集群路由
创建索引
删除指数
删除快照
打开
强制合并
复制品
恢复
快照
对于此讨论,我们将使用它Delete Indices作为操作,因为这是我们想要做的。
以下是示例操作文件delete_indices.yml,该文件将删除10天以上的logstash索引。
---
actions:
1:
action: delete_indices
description: >-
Delete indices older than 45 days (based on index name), for logstash-
prefixed indices. Ignore the error if the filter does not result in an
actionable list of indices (ignore_empty_list) and exit cleanly.
options:
ignore_empty_list: True
timeout_override:
continue_if_exception: False
disable_action: False
filters:
- filtertype: pattern
kind: prefix
value: logstash-
exclude:
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count: 10
exclude:
要运行此操作,只需使用以下命令
curator ./delete_index.yml --config ./curator.yml --dry-run
2017-04-09 17:27:46,075 INFO Preparing Action ID: 1, "delete_indices"
2017-04-09 17:27:46,080 INFO Trying Action ID: 1, "delete_indices": Delete indices older than 45 days (based on index name), for logstash- prefixed indices. Ignore the error if the filter does not result in an actionable list of indices (ignore_empty_list) and exit cleanly.
2017-04-09 17:27:46,538 INFO DRY-RUN MODE. No changes will be made.
2017-04-09 17:27:46,538 INFO (CLOSED) indices may be shown that may not be acted on by action "delete_indices".
2017-04-09 17:27:46,538 INFO Action ID: 1, "delete_indices" completed.
2017-04-09 17:27:46,538 INFO Job completed.
该--dry-run模式实际上不会删除索引。它可以用来测试动作的输出。
如果您想将其安排在一个cron中,可以使用 crontab -e
00 8 * * * root curator /path/delete_index.yml --config /path/curator.yml
上述配置将每天早上8点清除早于10天的索引。