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

一、痛点

Elasticsearch集群管理中索引的管理很是重要。
数据量少的时候,一个或者几个索引就能知足问题。
可是一旦数据量天天几TB甚至几十TB的增加时,索引的生命周期管理显得尤其重要。html

痛点1:你是否遇到过磁盘不够,要删除几个月前甚至更早时间数据的状况?
若是没有基于时间建立索引,单一索引借助delete_by_query结合时间戳,会越删磁盘空间越紧张,
以致于对本身都产生了怀疑?git

痛点2:你是否还在经过复杂的脚本管理索引?
1个增量rollover动态更新脚本,
1个按期delete脚本,
1个按期force_merge脚本,
1个按期shrink脚本,
1个按期快照脚本。
索引多了或者集群规模大了,脚本的维护是一笔不菲的开销。github

若是以上痛点,你都遇到过了。
那么,客官别走,本文利器curator会给你答案。web

二、curator是什么?

在这里插入图片描述

2.1 被Elastic收编的历史

curator最先被称为clearESindices.py。 它的惟一功能是删除索引,
然后重命名:logstash_index_cleaner.py。它在logstash存储库下做用:过时日志清理。
此后不久,原做者加入Elastic,它成为了Elasticsearch Curator,
Git地址:https://github.com/elastic/curatorcentos

2.2 收编后功能强大

curator容许对索引和快照执行许多不一样的操做,包括:安全

  1. 从别名添加或删除索引(或二者!)
  2. 更改分片路由分配更改分片路由分配
  3. 关闭索引关闭索引
  4. 建立索引建立索引
  5. 删除索引删除索引
  6. 删除快照删除快照
  7. 打开被关闭的索引打开被关闭的索引
  8. 对索引执行forcemerge段合并操做对索引执行forcemerge段合并操做
  9. reindex索引,包括来自远程集群的索引reindex索引,包括来自远程集群的索引
  10. 更改索引的每一个分片的副本数 更改索引的每一个分片的副本数
  11. rollover索引rollover索引
  12. 生成索引的快照(备份)生成索引的快照(备份)
  13. 还原快照还原快照

三、curator 版本

不一样于Elasticsearch甚至ELKB的版本统一规范,curator有本身的一套版本规范。
在这里插入图片描述
简化记录以下:
6.XES使用 curator 5;
5.XES可使用curator5 或者 curator4 ,具体参考官网:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/version-compatibility.htmlelasticsearch

还等什么,赶忙用起来!ide

四、Curator使用指南

4.1 curator安装

curator能够经过多种方式安装,具体取决于您的需求。
值得注意的是,Curator只须要安装在可访问Elasticsearch集群中机器上就能够运行。 它不须要安装在群集中的一个节点上。svg

个人机器是5.X版本,使用以下操做ok。工具

Step1:安装:

pip install elasticsearch-curator

centos6/7用户更简洁:

yum install elasticsearch-curator

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

pip install -U elasticsearch-curator

验证执行成功方法1:

curator_cli show_indices

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

4.2 curator用法讲解

4.2.1 用法1:curator_cli 命令行

用法举例:curator_cli 关闭所有一天前建立的索引名称为logs_*开头的索引。

curator_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命令行

用法举例:

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

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

[root@localhost .curator]# cat curator.yml 
# Remember, leave a key empty if there is no value.  None will be a string,
## not a Python "NoneType"

client:
  hosts: 192.168.1.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: /home/curator/logs
  logformat: default
  blacklist: ['elasticsearch', 'urllib3']

核心配置:

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

二、ACTION_FILE.YML 执行索引操做的配置信息
因为支持的操做很是多,建议直接参考官网配置便可:
https://www.elastic.co/guide/en/elasticsearch/client/curator/current/actions.html
https://www.elastic.co/guide/en/elasticsearch/client/curator/current/examples.html

拿删除历史索引举例:
如下命令删除了30天前,以logs_*开头的索引。

[root@localhost .curator]# cat action.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 20 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: logs_
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y.%m.%d'
      unit: days
      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分执行

$ crontab -e

加上以下的命令:

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

五、小结

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

思考:
遇到通用问题,不要重复造轮子,看看官方或者别人是否已经实现了,已经有更好的方案了。
若是有,就“拿来主义”,和本身业务不一致,能够考虑优化。
好比:相似curator,有不少公司已经进一步加工为可视化工具,极大提升效率。
在这里插入图片描述

Elasticsearch基础 Elasticsearch基础、进阶、实战第一公众号