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

版权声明:本文为博主原创文章,未经博主允许不得转载。转载请务必加上原作者:铭毅天下,原文地址:blog.csdn.net/laoyang360 https://blog.csdn.net/wojiushiwo987/article/details/85882832

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 ,具体参考官网:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/version-compatibility.html

还等什么,赶紧用起来!

4、Curator使用指南

4.1 curator安装

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

我的机器是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

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

2、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

5、小结

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

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

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

猜你喜欢

转载自blog.csdn.net/wojiushiwo987/article/details/85882832