#/bin/bash
#es-index-clear
#只保留15天内的日志索引
LAST_DATA=`date -d "-15 days" "+%Y.%m.%d"`
#删除上个月份所有的索引
curl -XDELETE 'http://ip:port/*-'${LAST_DATA}'*
清理命令
curl -XDELETE 'http://ip:9200/logstash-2018.12.12?pretty'
实际操作发现:
以上方法纯属误导人,没有卵用,只是删除里索引的标签,磁盘空间并没有立即减少,反而增加了!!!
有没有更通用的方法?
有,使用ES官网工具——curator工具。
1.curator简介
Curator是一个用来管理Elasticsearch索引的工具,使用它可以管理需要删除或保留的索引数据。 当Elasticsearch作为ELK、EFK等日志收集方案的日志存储时,删除过期数据以释放存储空间显的格外重要,使用Curator可以删除旧的索引并优化系统。
我这里先只介绍物理删除索引,即日志|文档文件。
curator官网地址
http://t.cn/RuwN0oM
Git地址:https://github.com/elastic/curator
安装Curator
安装Curator十分简单,可以通过python pip工具来完成。
2.安装Curator
安装Curator十分简单,可以通过python pip工具来完成。
pip install elasticsearch-curator
需要注意不同版本的curator兼容不同版本的ES,具体参考这里Compatibility Matrix。
curator --version
curator, version 5.4.1
3.配置和命令行语法
先来看一下curator的配置文件curator.yml,主要用来指定curator作为ES客户端的连接和日志配置:
client:
hosts:
- 192.168.1.60
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: /data/logs/curator.log
logformat: default
blacklist: ['elasticsearch', 'urllib3']
actions定义在一个deleteLogs.yml文件中,通过缩进定义变量。例子中定义了2个action。它们会被顺序执行。当然,这三个任务(1,2)在这里没有先后依赖,如果有依赖关系,要保证被依赖的action写在前面。
---
# 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 60 days (based on index name), nodejs-
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: nodejs-
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count: 60
2:
action: delete_indices
description: >-
Delete indices older than 60 days (based on index name), php-
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: php-
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count: 60
两个个任务分别是,删除nodejs索引,删除php索引.
执行命令
curator --config curator.yml deleteLogs.yml
完成清理工作。
日志的清理工作一般可以简单配置成系统的Cron调度执行。
0 0 * * * curator --config /data/elk/curator/curator.yml /data/elk/curator/deleteLogs.yml