-
Curator
Curator 是elasticsearch 官方的一个索引管理工具,可以通过配置文件的方式帮助我们对指定的一批索引进行创建/删除、打开/关闭、快照/恢复等管理操作。
- ES版本为6.5.4,Curator版本为5.6
- ES和Curator版本对应关系
- Curator source安装方式:
- https://www.elastic.co/guide/en/elasticsearch/client/curator/5.6/python-source.html
- Curator的配置
- https://www.elastic.co/guide/en/elasticsearch/client/curator/5.6/configuration.html
- 装完之后测试curator_cli --help
- 使用curator_cli显示索引
- curat_cli --host 47.112.11.147 --port 9200 show_indices
- curator有curator_cli和curator,curator反人类操作,不用了,直接上curator
- curator语法格式为:
- curator [--config CONFIG.YML] [--dry-run] ACTION_FILE.YML
- --dry-run该参数只是测试,并不真的做操作
- CONFIG.YML是配置文件,用于配置ES集群信息。
- config.yml官网配置参数:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/configfile.html
-
--- # Remember, leave a key empty if there is no value. None will be a string, # not a Python "NoneType" client: hosts: - 47.112.11.147 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/soft/elk/log/curator.log logformat: default blacklist: ['elasticsearch', 'urllib3']
-
action.yml官网配置参数:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/actionfile.html
-
这里只演示删除操作,例如删除10之外的日期,同时匹配前缀名为:filebeat-6.5.4-
-
--- # 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: "删除过期索引" description: "delete_expire_index" options: # action开关,为True表示不执行这个action,默认False disable_action: False # 如果为True,则在filters空列表时,继续下一个action处理而不是退出程序。 ignore_empty_list: True # 发现错误后,继续执行下一个索引操作,默认False continue_if_exception: True filters: # 是否排除隐藏索引,如.kibana - filtertype: kibana exclude: True # 是否排除open状态的索引 - filtertype: opened exclude: False #前缀匹配 - filtertype: pattern kind: prefix value: filebeat-6.5.4- exclude: False # 处理10天前的索引 - filtertype: age source: name direction: older timestring: '%Y.%m.%d' unit: days unit_count: 10 exclude: False
-
执行:curator --config config.yml action.yml
-
新建一些索引但是不包含今天(2019.03.14)的索引如图:
-
///
-
删除失败description: "删除过期索引"不能有中文
-
///
-
修改为英文之后,测试,显示执行成功,查看日志,显示
-
很明显的看出来:filtertype该属性相当于and条件
-
删除的索引剩余数量可以看出,他删除的是从今天(2019.03.14)算起的。不是按照索引名字的最后一天删除的
-
做成Linux定时任务
-
crontab命令用于设置周期性被执行的指令。该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行
-
编写sh脚本:curator-delete-index.sh
-
#!/bin/sh /usr/local/bin/curator --config /home/soft/elk/config.yml /home/soft/elk/action.yml echo "delete index success"
-
注意:最好使用命令的全路径名,否则可能找不到
-
赋予权限
- chmod 777 /home/elk/curator-delete-index.sh
- 创建定时任务:crontab
- crontab -e:打开了vi,输入:
- 30 16 * * * /home/elk/curator-delete-index.sh,之后保存退出vi
- crontab -l:查看所有的root用户的定时任务
- crontab -e:打开了vi,输入:
- 附:crontab文件的基本格式
- * * * * * command
- 分 时 日 月 周 命令
- 第1列表示分钟1~59 每分钟用*或者 */1表示
- 第2列表示小时1~23(0表示0点)
- 第3列表示日期1~31
- 第4列表示月份1~12
- 第5列标识号星期0~6(0表示星期天)
- 第6列要运行的命令
- “*”代表取值范围内的数字,
“/”代表”每”,
“-”代表从某个数字到某个数字,
“,”分开几个离散的数字
- 用法:
- cat /etc/crontab 查看/etc/crontab文件
- crontab -e 或者以root用户运行:crontab -u root -e
- 如果要每周一到周六的8点执行一次命令:
- 0 8 * * 1-6 你要运行的命令 >> /你的路径/create-Index.log 2>&1
- 例如:每分钟执行一次curator --config /root/.curator/curator.yml /root/.curator/action_file.yml命令:
- PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
- */1 * * * * curator --config /root/.curator/curator.yml /root/.curator/action_file.yml>>/root/.curator/create-Index.log 2>&1
- //将日志打印到/root/.curator/目录下的create-Index.log中,其中2>&1 表示执行结果及错误信息
- :wq存盘退出
- 列出某个用户cron服务的详细内容:crontab -l
- 重启:service crond restart
- 删除所有任务调度工作:crontab -r
- //问题///
- 查看日志报:
- 出现该问题为xxx.sh文件我是在window上写的,解决方式查看
- https://blog.csdn.net/qq_39669058/article/details/88579134
- 修改完成之后
- 显示成功,该成功是定时任务执行成功,不是curator执行成功
- 查看curator日志
- curator删除索引执行成功
参考:
干货 | Elasticsearch索引管理利器——Curator深入详解
附9 elasticsearch-curator + Linux定时任务