问题描述
在日志收集集群中,集群会按照时间创建索引。我们希望保留在某段时间内的日志,比如保留在 30 天内的日志,超过 30 天的日志将被自动删除。
本文将记录管理索引的方法,并实现自动删除索引,而无需人为干预。
解决方法
Index Lifecycle Management
官方提供 Index Lifecycle Management 特性,但是属于 X-PACK 的非免费功能,因此这条路不通。
本笔记不记录与 Index Lifecycle Management 特性有关的内容
Curator
还有其他的方案 - 可以使用 Curator 整理 Elasticsearch 索引,但是需要考虑一下几个问题:
1)项目是否还在维护 => # 06/02/2020 Latest commit b131026 on Mar 17
2)是否支持我们的集群版本 => 根据 Version Compatibility 文档,Version 5 支持 ES 7.X 版本
官方文档:Curator Index Management 5.8
项目地址:GitHub/elastic/curator
本笔记将重点记录 Curator 工具的使用方法,以及相关参考文档的位置。
安装 Curator 工具
CentOS 7 and Curator 5
Curator Reference [5.8] » Installation » YUM repository
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch cat > /etc/yum.repos.d/curator-5.repo <<EOF [curator-5] name=CentOS/RHEL 7 repository for Elasticsearch Curator 5.x packages baseurl=https://packages.elastic.co/curator/5/centos/7 gpgcheck=1 gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch enabled=1 EOF yum makecache yum install elasticsearch-curator # 安装到 /opt/elasticsearch-curator 目录 # 并自动创建 /usr/bin/curator 软链接
Debian 10 and Curator 5
Curator Reference [5.8] » Installation » APT repository
wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | apt-key add - cat > /etc/apt/sources.list.d/curator-5.list <<EOF deb [arch=amd64] https://packages.elastic.co/curator/5/debian stable main EOF apt-get update apt-get install elasticsearch-curator # 安装到 /opt/elasticsearch-curator 目录 # 并自动创建 /usr/bin/curator 软链接
命令行使用方法
curator
该命令用于执行在 YAML 文件中预先定义的动作:
curator [--config CONFIG.YML] [--dry-run] ACTION_FILE.YML # --config CONFIG.YML => 配置文件 # 默认查找 ~/.curator/curator.yml 文件 # --dry-run => 模拟运行,而不会修改数据 # ACTION_FILE.YML => 任务文件,描述要执行的任务 # --help 查看帮助选项
curator_cli
该命令命令用于在命令行直接使用,属于交互模式
curator --help # --help 查看帮助选项
接下来就是学习配置文件(CONFIG.YML、ACTION_FILE.YML)的语法与内容。
配置文件 / 动作文件
Configuration File
用于控制 Curator 如何连接集群、输出日志等等方面。
详细记录参考官方文档:
Curator Reference [5.8] » Configuration » Configuration File
Action File
用于定义 Curator 要执行的任务。
关于 Action File 简单示例(Curator Reference [5.8] » Configuration » Action File),大致格式如下(对官方文档的示例进行简化):
--- actions: 1: action: ACTION1 description: OPTIONAL DESCRIPTION options: option1: value1 ... optionN: valueN continue_if_exception: False disable_action: True filters: - filtertype: *first* filter_element1: value1 ... filter_elementN: valueN - filtertype: *second* filter_element1: value1 ... filter_elementN: valueN 2: action: ACTION3 ...
通过简单示例可知:我们需要了解 action description options filters 使用方法,接下来将简单学习并记录这些内容。
action
将要执行的动作,参考 Curator Reference [5.8] » Actions 文档。这里简单列举我们用到的:
1)delete_indices => 删除匹配条件的索引
description
是对 ACTION 的描述,可选:
description: >- I can make the description span multiple lines by putting ">-" at the beginning of the line, as seen above. Subsequent lines must also be indented. options: option1: ...
options
是用于控制动作(Action)的选项(Option),每个动作(Action)都支持不同的选项(Option)。
这些选项会影响 Action 的方方面面,比如异样处理、超时、等待时间、是否禁用等等。
在使用 Action 功能时,文档会列举该 Action 支持的 Option 选项,届时再阅读文档即可。
filters / filtertype / <Filter Elements>
filters,关键字作用是选择要处理的索引
filtertype,用于指定过滤类型,比如:
1)通过正则表达式匹配索引名称,则 filtertype 为 pattern 类型
2)通过创建时间过滤,则 filtertype 为 age 类型
=> 无需专门学习,根据需求选择合适的类型即可,参考 Curator Reference [5.8] » Filters 文档。
<Filter Elements>,与 filtertype 有关的参数,比如:
1)通过正则表达式匹配索引名称,需要通过 value 指定正则表达式
=> 无需专门学习,filtertype 文档会列出有关 <Filter Elements> 参数,届时再阅读对应文档即可。
使用正则表达式过滤索引的示例:
filters: - filtertype: pattern kind: regex value: '^(alpha-|bravo-|charlie-).*$'
Environment Variables
在配置文件、动作文件中,可以引用环境变量,使用 ${VAR} 格式。
这是个实验性质的功能,本笔记不再张开,细节请参考官方文档:
Curator Reference [5.8] » Configuration » Environment Variables
官方示例参考
在 Curator Reference [5.8] » Examples 中是常用的示例,可以进行参考。
与安全有关的问题,可以参考 Curator Reference [5.8] » Security 文档。
常见问题参考 Curator Reference [5.8] » Frequently Asked Questions 页面。
相关文章
参考文献
GitHub/elastic/curator
Curator Reference [5.8] » Versions » Version Compatibility
Alternative to Curator to cleanup old indices