通过es索引生命周期策略删除日志索引
在es 7.x版本之后,多了个索引生命周期的概念,可以一系列的设置,给新生成的索引绑定生命周期策略,到期后,索引自动删除。
也可以通过linux定时任务实现,请查看另一篇文章《通过linux定时任务删除es日志索引》
流程
- 创建索引生命周期策略
- 创建索引模板,与生命周期策略绑定,匹配新生成的索引,关联索引生命周期
操作
下面的操作也可以通过kibana来完成
创建索引生命周期策略
创建名称为auto_delete_policy 索引生命周期策略,索引7天后,自动删除。测试时,可以设置策略时间短点。
PUT /_ilm/policy/auto_delete_policy
{"policy": {"phases": {"delete": {"min_age": "7d","actions": {"delete": {}}}}}
}
查询索引生命周期策略
GET /_ilm/policy/auto_delete_policy
创建索引模板
索引模板作为中间桥梁,把索引生命周期策略和索引关联起来,这里匹配 my、index 开头,新生成的索引
PUT _template/elk_template{"index_patterns": ["my*","index*"],"template": {"settings": {"index": {"lifecycle": {"name": "auto_delete_policy","indexing_complete": "true"}}}}
}
创建索引模板(elk_tempalte),index.lifecycle.name 把上面的自动删除策略绑定到elk索引模板
后来新生成 my-、index- 开头的索引时就会应用这个模板。
indexing_complete:true,必须设为true,跳过HOT阶段的Rollover
查询索引模板
GET _template/elk_template
测试
测试设置
生命周期策略默认10分钟检测一次,为了方便测试,这里设为30s。后面改回来就可以了。
PUT /_cluster/settings
{"transient": {"indices.lifecycle.poll_interval": "30s"}
}
查看索引
查看新生成的索引,有没有关联到索引生命周期策略,
这里查看my-开头的索引情况
GET my-*/_ilm/explain
返回
{"indices": {"my-2023.08.30": {"index": "my-2023.08.30","managed": true,"policy": "auto_delete_policy","lifecycle_date_millis": 1693357650166,"age": "3.35d","phase": "new","phase_time_millis": 1693357650194,"action": "complete","action_time_millis": 1693357650194,"step": "complete","step_time_millis": 1693357650194,"phase_execution": {"policy": "auto_delete_policy","version": 1,"modified_date_in_millis": 1692951002180}}}
}
参考官网索引管理章节