Curator是一个用来管理Elasticsearch索引的工具,使用它可以管理需要删除或保留的索引数据。 当Elasticsearch作为ELK、EFK等日志收集方案的日志存储时,删除过期数据以释放存储空间显的格外重要,使用Curator可以删除旧的索引并优化系统。
1.Curator的功能
使用Curator可以完成以下功能:
- 为别名(Alias)添加或移除索引
- 创建索引
- 删除索引
- 关闭索引
- 删除快照
- 打开已经关闭的索引
- 更改分片路由配置
- 强制合并索引
- 重建索引(包括从远程的集群)
- 更改索引每个分片的副本数量
- 为索引创建快照
- 从快照还原
- rollover indices(当某个别名指向的实际索引过大的时候,自动将别名指向下一个实际索引)
2.安装Curator
安装Curator十分简单,可以通过python pip工具来完成。
pip install elasticsearch-curator需要注意不同版本的curator兼容不同版本的ES,具体参考这里Compatibility Matrix。
curator --version
curator, version 5.4.13.配置和命令行语法
先来看一下curator的配置文件curator.yml,主要用来指定curator作为ES客户端的连接和日志配置:
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的命令行语法:
curator --help
Usage: curator [OPTIONS] ACTION_FILE
Curator for Elasticsearch indices.
See http://elastic.co/guide/en/elasticsearch/client/curator/current
Options:
--config PATH Path to configuration file. Default: ~/.curator/curator.yml
--dry-run Do not perform any changes.
--version Show the version and exit.
--help Show this message and exit.从命令行语法可以使用curator进行操作,需要给定ACTION_FILE,关于ACTION_FILE文件的格式参考
Actions,这里不再展开。
后边将具体实践。
4.curator实践
4.1 定期清理索引
在ELK或EFK场景下,可以使用Curator实现索引的定期清理。
下面是清理ELK场景下,ES日志索引的ACTION_FILE deleteLogs.yml:
---
# Remember, leave a key empty if there is no value. None will be a string,
# not a Python "NoneType"
#
# Also remember that all examples have 'disable_action' set to True. If you
# want to use this action as a template, be sure to set this to False after
# copying it.
actions:
1:
action: delete_indices
description: >-
Delete indices older than 120 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
disable_action: False
filters:
- filtertype: pattern
kind: prefix
value: logstash-
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count: 120执行命令curator --config curator.yml deleteLogs.yml完成清理工作。
日志的清理工作一般可以简单配置成系统的Cron或在Kubernetes中以CronJob的形式调度执行。