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

一、痛点
Elasticsearch集群管理中索引的管理很是重要。git

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

可是一旦数据量天天几TB甚至几十TB的增加时,索引的生命周期管理显得尤其重要。centos

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

若是没有基于时间建立索引,单一索引借助delete_by_query结合时间戳,会越删磁盘空间越紧张,以致于对本身都产生了怀疑?elasticsearch

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

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

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

若是以上痛点,你都遇到过了。this

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

二、curator是什么?
干货 | Elasticsearch索引管理利器——Curator深刻详解
2.1 被Elastic收编的历史
curator最先被称为clearESindices.py。 它的惟一功能是删除索引,
然后重命名:logstash_index_cleaner.py。它在logstash存储库下做用:过时日志清理。

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

2.2 收编后功能强大
curator容许对索引和快照执行许多不一样的操做,包括:

从别名添加或删除索引(或二者!)

更改分片路由分配更改分片路由分配

关闭索引关闭索引

建立索引建立索引

删除索引删除索引

删除快照删除快照

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

对索引执行forcemerge段合并操做对索引执行forcemerge段合并操做

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

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

rollover索引rollover索引

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

还原快照还原快照

三、curator 版本
不一样于Elasticsearch甚至ELKB的版本统一规范,curator有本身的一套版本规范。

干货 | Elasticsearch索引管理利器——Curator深刻详解
简化记录以下:
6.XES使用 curator 5;
5.XES可使用curator5 或者 curator4 ,

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

还等什么,赶忙用起来!

四、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 命令行
用法举例:curatorcli 关闭所有一天前建立的索引名称为logs*开头的索引。

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

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

4.2.2 用法2:curator命令行
用法举例:

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

解释:
一、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)日志信息。

二、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 disableaction: 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

五、小结
切记:
curator适用于基于时间或者template其余方式建立的索引,
不适合单一索引存储N久历史数据的操做的场景。

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

若是有,就“拿来主义”,和本身业务不一致,能够考虑优化。

好比:相似curator,有不少公司已经进一步加工为可视化工具,极大提升效率。
干货 | Elasticsearch索引管理利器——Curator深刻详解

相关文章
相关标签/搜索