1、安装 curator

elasticsearch 与 curator 版本适配表:https://www.elastic.co/guide/en/elasticsearch/client/curator/5.8/version-compatibility.html#version-compatibility

下载方式有很多,比如:

  • rpm 包安装
  • pip 安装
  • 二进制安装

这里我是下载的二进制包,地址:https://github.com/elastic/curator/archive/v5.8.4.tar.gz

将 tar.gz 包解压,执行:python setup.py install

2、config.yml 文件(编辑与es的连接)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
---
# Remember,leave a key empty if there is no value. None will be a string,
# not a Python "NoneType"
client:
hosts:
- 192.168.66.65
- 192.168.66.52
- 192.168.66.53
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/curaor-5.8.4/log/curator.log
logformat: default
blacklist: ['elasticsearch', 'urllib3']

注:上面配置的日志目录及文件需要自己预先创建好,否则报错。

3、action.yml 文件(编辑策略)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
---
# 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 index holli-msa-nacos-logfile-*
options:
ignore_empty_list: True
filters:
- filtertype: pattern
kind: regex
value: '^(holli-msa-nacos-logfile-).*$'
- filtertype: space
disk_space: 0.5

上面的过滤原则是:先用正则匹配索引列表,然后当哪个索引容量超过 0.5G,就删除哪个索引。

还有很多过滤场景,可以参考官方:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/filter_elements.html

4、执行看效果

1
curator --config my-config.yml my-action.yml --dry-run
  • –dry-run 表示预执行,不更改任何东西

5、设置定时任务

crontab -e,进入文件编辑:

1
2
#定时清理es索引
0 * * * * cd /opt/curaor-5.8.4 && curator --config my-config.yml my-action.yml

crontab -l,查看定时任务列表。