Curator是一个用来管理Elasticsearch索引的工具,使用它可以管理需要删除或保留的索引数据。 当Elasticsearch作为ELK、EFK等日志收集方案的日志存储时,删除过期数据以释放存储空间显的格外重要,使用Curator可以删除旧的索引并优化系统。

1.Curator的功能

使用Curator可以完成以下功能:

  • 为别名(Alias)添加或移除索引
  • 创建索引
  • 删除索引
  • 关闭索引
  • 删除快照
  • 打开已经关闭的索引
  • 更改分片路由配置
  • 强制合并索引
  • 重建索引(包括从远程的集群)
  • 更改索引每个分片的副本数量
  • 为索引创建快照
  • 从快照还原
  • rollover indices(当某个别名指向的实际索引过大的时候,自动将别名指向下一个实际索引)

2.安装Curator

安装Curator十分简单,可以通过python pip工具来完成。

1pip install elasticsearch-curator

需要注意不同版本的curator兼容不同版本的ES,具体参考这里Compatibility Matrix

1curator --version
2curator, version 5.4.1

3.配置和命令行语法

先来看一下curator的配置文件curator.yml,主要用来指定curator作为ES客户端的连接和日志配置:

 1client:
 2  hosts:
 3    - 127.0.0.1
 4  port: 9200
 5  url_prefix:
 6  use_ssl: False
 7  certificate:
 8  client_cert:
 9  client_key:
10  ssl_no_validate: False
11  http_auth:
12  timeout: 30
13  master_only: False
14
15logging:
16  loglevel: INFO
17  logfile:
18  logformat: default
19  blacklist: ['elasticsearch', 'urllib3']

再开看一下curator的命令行语法:

 1curator --help
 2Usage: curator [OPTIONS] ACTION_FILE
 3
 4  Curator for Elasticsearch indices.
 5
 6  See http://elastic.co/guide/en/elasticsearch/client/curator/current
 7
 8Options:
 9  --config PATH  Path to configuration file. Default: ~/.curator/curator.yml
10  --dry-run      Do not perform any changes.
11  --version      Show the version and exit.
12  --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:

 1---
 2# Remember, leave a key empty if there is no value.  None will be a string,
 3# not a Python "NoneType"
 4#
 5# Also remember that all examples have 'disable_action' set to True.  If you
 6# want to use this action as a template, be sure to set this to False after
 7# copying it.
 8actions:
 9  1:
10    action: delete_indices
11    description: >-
12      Delete indices older than 120 days (based on index name), for logstash-
13      prefixed indices. Ignore the error if the filter does not result in an
14      actionable list of indices (ignore_empty_list) and exit cleanly.      
15    options:
16      ignore_empty_list: True
17      disable_action: False
18    filters:
19    - filtertype: pattern
20      kind: prefix
21      value: logstash-
22    - filtertype: age
23      source: name
24      direction: older
25      timestring: '%Y.%m.%d'
26      unit: days
27      unit_count: 120

执行命令curator --config curator.yml deleteLogs.yml完成清理工作。

日志的清理工作一般可以简单配置成系统的Cron或在Kubernetes中以CronJob的形式调度执行。

参考