Elasticsearch索引管理利器——Curator深入详解

干货 | Elasticsearch索引管理利器——Curator深入详解

1、痛点

Elasticsearch集群管理中索引的管理非常重要。

数据量少的时候,一个或者几个索引就能满足问题。

但是一旦数据量每天几TB甚至几十TB的增长时,索引的生命周期管理显得尤为重要。

痛点1:你是否遇到过磁盘不够,要删除几个月前甚至更早时间数据的情况?

如果没有基于时间创建索引,单一索引借助delete_by_query结合时间戳,会越删磁盘空间越紧张,以至于对自己都产生了怀疑?

痛点2:你是否还在通过复杂的脚本管理索引?

1个增量rollover动态更新脚本,
1个定期delete脚本,
1个定期force_merge脚本,
1个定期shrink脚本,
1个定期快照脚本。

索引多了或者集群规模大了,脚本的维护是一笔不菲的开销。

如果以上痛点,你都遇到过了。

那么,客官别走,本文利器curator会给你答案。

2、curator是什么?

2.1 被Elastic收编的历史

curator最早被称为clearESindices.py。 它的唯一功能是删除索引,
而后重命名:logstash_index_cleaner.py。它在logstash存储库下作用:过期日志清理。

此后不久,原作者加入Elastic,它成为了Elasticsearch Curator,
Git地址:https://github.com/elastic/curator

2.2 收编后功能强大

curator允许对索引和快照执行许多不同的操作,包括:

  1. 从别名添加或删除索引(或两者!)

  2. 更改分片路由分配更改分片路由分配

  3. 关闭索引关闭索引

  4. 创建索引创建索引

  5. 删除索引删除索引

  6. 删除快照删除快照

  7. 打开被关闭的索引打开被关闭的索引

  8. 对索引执行forcemerge段合并操作对索引执行forcemerge段合并操作

  9. reindex索引,包括来自远程集群的索引reindex索引,包括来自远程集群的索引

  10. 更改索引的每个分片的副本数 更改索引的每个分片的副本数

  11. rollover索引rollover索引

  12. 生成索引的快照(备份)生成索引的快照(备份)

  13. 还原快照还原快照

3、curator 版本

不同于Elasticsearch甚至ELKB的版本统一规范,curator有自己的一套版本规范。

 


简化记录如下:
6.XES使用 curator 5;
5.XES可以使用curator5 或者 curator4 ,

具体参考官网:http://t.cn/EGi2nLX

还等什么,赶紧用起来!

4、Curator使用指南

4.1 curator安装

curator可以通过多种方式安装,具体取决于您的需求。

值得注意的是,Curator只需要安装在可访问Elasticsearch集群中机器上就可以运行。 它不需要安装在群集中的一个节点上。

我的机器是5.X版本,使用如下操作ok。

Step1:安装:

1pip install elasticsearch-curator

centos6/7用户更简洁:

1yum install elasticsearch-curator

Step 2:升级至最新版本(非必须,根据自己需要):

1pip install -U elasticsearch-curator

验证执行成功方法1:

1curator_cli show_indices

若成功,会显示索引信息。

4.2 curator用法讲解

4.2.1 用法1:curator_cli 命令行

用法举例:curator_cli 关闭全部一天前创建的索引名称为logs_*开头的索引。

1curator_cli --host 192.168.1.2 --port 9200 close --filter_list '[{"filtertype":"age","source":"creation_date","direction":"older","unit":"days","unit_count":1},{"filtertype":"pattern","kind":"prefix","value":"logs_"}]'

好处:无需配置文件,一行命令即可成功。
坏处:不能便捷的适应复杂的操作。

4.2.2 用法2:curator命令行

用法举例:

1curator [--config CONFIG.YML] [--dry-run] ACTION_FILE.YML

解释:
1、CONFIG.YML是配置文件,用于配置ES集群信息。
CONFIG.YML样例:

1[root@localhost .curator]# cat curator.yml 
2# Remember, leave a key empty if there is no value.  None will be a string,
3## not a Python "NoneType"
4
5client:
6  hosts: 192.168.1.1
7  port: 9200
8  url_prefix:
9  use_ssl: False
10  certificate:
11  client_cert:
12  client_key:
13  ssl_no_validate: False
14  http_auth:
15  timeout: 30
16  master_only: False
17
18logging:
19  loglevel: INFO
20  logfile: /home/curator/logs
21  logformat: default
22  blacklist: ['elasticsearch', 'urllib3']

核心配置:

1)集群IP; 
2)安全认证信息; 
3)日志信息。

2、ACTION_FILE.YML 执行索引操作的配置信息
由于支持的操作非常多,建议直接参考官网配置即可:

http://t.cn/EGiLwyk

http://t.cn/EGiL4EF

拿删除历史索引举例:

以下命令删除了30天前,以logs_*开头的索引。

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

如果执行多个任务怎么办呢?

注意:actions: 后面的,依次类推

2:执行操作 
3:执行操作 
4:执行操作 
N:执行操作

好处:1个配置搞定10+处操作,不费劲!

4.3 curator 实践

经过4.2的配置,实践执行如下:

curator --config config.yml   指定路径/action.yml

4.4 周期性执行

借助crontab,每天零点5分执行

1$ crontab -e

加上如下的命令:

15 0 * * * curator --config config.yml action.yml

5、小结

  • 切记:
    curator适用于基于时间或者template其他方式创建的索引
    不适合单一索引存储N久历史数据的操作的场景

  • 思考:
    遇到通用问题,不要重复造轮子,看看官方或者别人是否已经实现了,已经有更好的方案了。

    如果有,就“拿来主义”,和自己业务不一致,可以考虑优化。

    比如:类似curator,有很多公司已经进一步加工为可视化工具,极大提高效率。

 

 

 

posted @ 2019-11-05 16:26  Aslan-a  阅读(789)  评论(0编辑  收藏  举报