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

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: curator最早被称为clearESindices.py,它的唯一功能是删除索引,而后重命名:logstash_index_cleaner.py。

1、痛点

Elasticsearch集群管理中索引的管理非常重要。
数据量少的时候,一个或者几个索引就能满足问题。

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

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

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

1个增量rollover动态更新脚本,
1个定期delete脚本,
1个定期force_merge脚本,
1个定期shrink脚本,
1个定期快照脚本。
索引多了或者集群规模大了,脚本的维护是一笔不菲的开销。

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

2、curator是什么?

image.png

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索引
生成索引的快照(备份)生成索引的快照(备份)
还原快照还原快照

3、curator 版本

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

image.png

简化记录如下:
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,有很多公司已经进一步加工为可视化工具,极大提高效率。

image.png


20190117225823895.png

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

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
7天前
|
运维 安全 API
Elasticsearch 悬挂索引解析与管理指南
Elasticsearch 悬挂索引解析与管理指南
28 7
|
7天前
|
安全 API 数据安全/隐私保护
Elasticsearch 通过索引阻塞实现数据保护深入解析
Elasticsearch 通过索引阻塞实现数据保护深入解析
24 4
|
7天前
|
存储 数据处理 索引
Elasticsearch 8.X 小技巧:使用存储脚本优化数据索引与转换过程
Elasticsearch 8.X 小技巧:使用存储脚本优化数据索引与转换过程
34 6
|
7天前
|
存储 自然语言处理 搜索推荐
Elasticsearch 8.10 同义词管理新篇章:引入同义词 API
Elasticsearch 8.10 同义词管理新篇章:引入同义词 API
21 0
|
7天前
|
存储 数据库 索引
Elasticsearch ILM 索引生命周期管理常见坑及避坑指南
Elasticsearch ILM 索引生命周期管理常见坑及避坑指南
13 0
|
7天前
|
安全 API 数据安全/隐私保护
Elasticsearch 通过索引阻塞实现数据保护深入解析
Elasticsearch 通过索引阻塞实现数据保护深入解析
19 0
|
7天前
|
安全 Java API
SpringBoot 实现 elasticsearch 索引操作(RestHighLevelClient 的应用)
SpringBoot 实现 elasticsearch 索引操作(RestHighLevelClient 的应用)
20 1
|
7天前
|
Java Maven 开发工具
【ElasticSearch 】IK 分词器安装
【ElasticSearch 】IK 分词器安装
25 1
|
7天前
|
数据可视化 索引
elasticsearch head、kibana 安装和使用
elasticsearch head、kibana 安装和使用
|
7天前
|
Java Windows
windows下 安装 Elasticsearch报错warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
windows下 安装 Elasticsearch报错warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
47 0

热门文章

最新文章