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,有说明问题欢迎留言指出


猜你喜欢

转载自blog.csdn.net/zou79189747/article/details/80526221
今日推荐