elasticsearch 使用curator管理索引

最近来了一个需求,希望能控制某一批索引的数据量,当超出大小之后对索引进行清理。网上搜了好多关键词都搜不到,后来再一篇提问的帖子中找到了蛛丝马迹,主要原因还是对ES不够了解,根本不知道有Curator这么一个工具,还得感谢这为仁兄。https://elasticsearch.cn/question/1268#!answer_form


简介

Curator 是elasticsearch 官方的一个索引管理工具,可以删除、创建、关闭、段合并等等功能

安装

版本兼容性:

关于安装方式官网提供了很多种,其实对于国内大部分开发人员来说,下载rpm包再在linux环境安装方式是最合适的。

4.2下载地址:

Elasticsearch Curator 4.2.6 RHEL/CentOS 6 Binary Package (RPM)

Elasticsearch Curator 4.2.6 RHEL/CentOS 7 Binary Package (RPM)

rpm -ivh elasticsearch-curator-4.2.6-1.x86_64.rpm
命令行能识别curator、curator_cli表示安装成功

默认安装路径为:/opt/elasticsearch-curator

创建配置文件

curator运行需两个配置文件config.yml(用于连接ES集群配置)、action.yml(用于配置要执行的操作),文件名称可以随意命名

config.yml样例:

---
# Remember, leave a key empty if there is no value.  None will be a string,
# not a Python "NoneType"
client:
  hosts:
    - 127.0.0.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: /opt/elasticsearch-curator/log/run.log
  logformat: default
  blacklist: ['elasticsearch', 'urllib3']

配置说明参考官网说明,config.yml

action.yml样例(删除3天前的数据):

---
# 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 metric indices older than 3 days (based on index name), for
      zou_data-2018-05-01
      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: True
    filters:
    - filtertype: pattern
      kind: regex
      value: '^(zou_data-).*$'
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y-%m-%d'
      unit: days
      unit_count: 3

配置说明参考官网说明:action.yml

这里详细讲解一下space过滤,因为本人就是为了这个而来

- filtertype: space
  disk_space: 100
  reverse: True
  use_age: False
  source: creation_date
  timestring:
  field:
  stats_result:
  exclude: False

disk_space: 设置一个临界值,单位为gb,当匹配的索引数据总和与这个临界值进行比较

reverse: 默认为True,可以这样理解,True时索引按名称倒序,删除时从后往前删。False时索引按名称顺序,删除时也是从后往前删。如果配置了use_age为True时这个配置就被忽略了。

user_age: 这个就与action.yml样例类似,根据日期来确定哪些数据为老数据

source: 从哪里来获取索引时间。当user_age为True时,该配置为必填项。可以为name、creation_date、field_stats

  1. name: 来源为索引名称,此时必须指定timestring来匹配索引名称中的日期
  2. creation_date: 来源为索引的创建时间,ES内部会保存每个索引创建的具体时间,可通过http://127.0.0.1:9200/zou_data*?pretty查看。
  3. filed_stats: 来源为索引数据中某个日期字段,这个字段必须时ES能识别的日期字段,Curator会通过ES API获取每个索引中这个字段的最大值跟最小值。

timestring: 当source为name时必须配置,用于匹配索引名称中的日期,如 '%Y-%m-%d'

field: 当source为field_stats时必须配置,用于指定索引中的日期字段,默认@timestamp字段

stats_result: 只有当source为field时才需配置,用于指定永min_value 还是max_value ,默认为min_value 

exclude: 是否需要排除,为True表示该filter匹配到的内容不执行action操作

使用样例:

#根据索引名称排序
- filtertype: space
  disk_space: 0.001
  reverse: True
#根据索引创建的时间排序
- filtertype: space
  disk_space: 0.001
  use_age: True
  source: creation_date
#根据索引名称获取时间排序
- filtertype: space
  disk_space: 0.001
  use_age: True
  source: name
  timestring: '%Y-%m-%d'
#根据索引时间字段的最小值排序
- filtertype: space
  disk_space: 0.001
  use_age: True
  source: field_stats
  field: logtime
  stats_result: min_value

运行Curator

单次运行:

curator --config config.yml action.yml 

实际生成环境中我们添加一个linux的cron定时任务

crontab -e
#添加如下配置,每天0时运行一次
0 0 */1 * * curator --config /opt/elasticsearch-curator/config.yml /opt/elasticsearch-curator/action.yml 
初学Curator,有说明问题欢迎留言指出


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Elasticsearch Curator是一个用于管理Elasticsearch集群的工具。它提供了两种接口:curator_cli命令行模式和curator API模式。curator_cli命令行模式可以使用各种命令来执行不同的操作,比如关闭索引、删除索引、创建快照等。curator API模式则可以通过编写脚本来调用Elasticsearch的API来实现相同的功能。\[1\] 在生产环境中,推荐先关闭一段时间观察索引的状态,然后再删除数据。这样可以避免意外删除数据而导致恢复困难。可以使用curator_cli命令行模式的close命令来关闭索引使用delete_indices命令来删除索引。\[2\] 当数据量达到一定量级时,为了节省内存或磁盘空间,通常会选择关闭或删除一定时间之前的索引。为了方便管理,可以编写脚本并定期执行,使用Elasticsearch的API来实现这些操作。使用curator API模式可以更好地管理和维护这些脚本。\[3\] #### 引用[.reference_title] - *1* *2* [Elasticsearch集群管理工具curator详解-curator_cli](https://blog.csdn.net/kjh2007abc/article/details/85149030)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【ElasticsearchCurator 从入门到实战](https://blog.csdn.net/qq_21383435/article/details/119081462)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值