一, 简述
当日志发送到 ELK 之后, Elasticsearch 随着日志的增加, 占用磁盘量会越来越大. 这时候, 需要我们写角本定期 DELETE 日志. 角本写法, 也很简单, 只是发送 HTTP 的 DELETE 方式到: http://<ip>:<port>/*-yyyy.MM.dd * 即可.
二, 定期删除 Elasticsearch 中日志的角本: 新建一个 es-index-clear.sh 到 / opt 目录下, 内容如下:
- #/bin/bash
- #es-index-clear
- # 只保留 15 天内的日志索引
- LAST_DATA=`date -d "-15 days" "+%Y.%m.%d"`
- # 删除上个月份所有的索引
- curl -XDELETE 'http://127.0.0.1:9200/*-'${
- LAST_DATA
- }'*'
三, 使用 crontab -e 添加定时任务: 执行 crontab -e, 在打开的内容中, 输入 (前面'0 * * * *'表示 Cron 表达式, 可以参考我前面的文章):
比如下列表示每小时整时执行一次:
0 * * * * root /opt/es-index-clear.sh
如果要每天凌晨执行一次:
0 0 * * * root /opt/es-index-clear.sh
四, 启动定时任务, 并开机自动运行
- systemctl enable crond
- systemctl restart crond
- systemctl status crond
来源: https://www.cnblogs.com/songxingzhu/p/10213230.html