对于存储在云搜索中的日志类型,指标类型的数据有一个共同的特点:索引中的数据随着时间的流逝,索引大小不断增长,相反的,数据的价值逐渐降低,我们希望可以实现一种自动化的管理操作,定期关闭或删除索引,以此来减少空间占用,降低成本。 从 ElasticSearch 6.6开始,Elasticsearch 提供索引生命周期管理功能,索引生命周期管理可以通过 API 或者 kibana 界面配置,从而实现自动的索引状态管理。
1. 创建 ISM 策略
创建 ISM 策略,您可以在 Kibana 界面主菜单中,选择 Index Management,点击 Index Policies,然后再右上角点击 Create policy,如图所示: 接着我们可以填写 Policy ID,在 Define policy 框中填写 ISM 策略。 如下所示策略在 7 天后将副本计数减少到零以节省磁盘空间,然后在 10 天后删除索引:
{
"policy_id": "rudonx_test",
"description": "Changes replica count and deletes.",
"last_updated_time": 1654484798389,
"schema_version": 1,
"error_notification": null,
"default_state": "current",
"states": [
{
"name": "current",
"actions": [],
"transitions": [
{
"state_name": "old",
"conditions": {
"min_index_age": "7d"
}
}
]
},
{
"name": "old",
"actions": [
{
"replica_count": {
"number_of_replicas": 0
}
}
],
"transitions": [
{
"state_name": "delete",
"conditions": {
"min_index_age": "10d"
}
}
]
},
{
"name": "delete",
"actions": [
{
"delete": {}
}
],
"transitions": []
}
],
"ism_template": null
}
注:将副本数修改为 0 会有数据丢失的风险,如果有修改副本数的需求,需提前确认此索引为非关键索引。 对于 ISM policy 支持的操作,您可以参考文档[1]。
2. 将索引和策略绑定
在创建完策略之后,我们需要将策略和具体的索引进行绑定,您可以在 Kibana 界面主菜单中,选择 Index Management,点击 Indices,然后勾选一个具体的索引,选择 Apply Policy,如图所示:
将 ISM 策略和 index template 结合使用
您可以使用如下语句,将 ISM 策略与 index template 进行结合使用,这样在满足索引创建规则后,会自动匹配到相关的 policy
PUT _index_template/<index template name>
{
"index_patterns": [
"user_info-*"
],
"template": {
"settings": {
"opendistro.index_state_management.policy_id": "policy name"
}
}
}
[1] https://opendistro.github.io/for-elasticsearch-docs/docs/im/ism/policies/#ism-supported-operations [2] https://opendistro.github.io/for-elasticsearch-docs/docs/im/ism/ 如果您有其他问题,欢迎您联系火山引擎技术支持服务