通过es索引生命周期策略删除日志索引

通过es索引生命周期策略删除日志索引

在es 7.x版本之后,多了个索引生命周期的概念,可以一系列的设置,给新生成的索引绑定生命周期策略,到期后,索引自动删除。

也可以通过linux定时任务实现,请查看另一篇文章《通过linux定时任务删除es日志索引》

流程

  • 创建索引生命周期策略
  • 创建索引模板,与生命周期策略绑定,匹配新生成的索引,关联索引生命周期

操作

下面的操作也可以通过kibana来完成

创建索引生命周期策略

创建名称为auto_delete_policy 索引生命周期策略,索引7天后,自动删除。测试时,可以设置策略时间短点。

json 复制代码
PUT /_ilm/policy/auto_delete_policy
{
	"policy": {
		"phases": {
			"delete": {
				"min_age": "7d",
				"actions": {
					"delete": {}
				}
			}
		}
	}
}

查询索引生命周期策略

复制代码
GET /_ilm/policy/auto_delete_policy

创建索引模板

索引模板作为中间桥梁,把索引生命周期策略和索引关联起来,这里匹配 my、index 开头,新生成的索引

json 复制代码
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。后面改回来就可以了。

json 复制代码
PUT /_cluster/settings
{
	"transient": {
		"indices.lifecycle.poll_interval": "30s"
	}
}

查看索引

查看新生成的索引,有没有关联到索引生命周期策略,

这里查看my-开头的索引情况

复制代码
GET my-*/_ilm/explain

返回

json 复制代码
{
	"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
			}
		}
	}
}

参考官网索引管理章节

相关推荐
Elastic 中国社区官方博客8 小时前
如何比较两个 Elasticsearch 索引并找出缺失的文档
大数据·运维·数据库·elasticsearch·搜索引擎
阿里云大数据AI技术12 小时前
Elasticsearch 智能助手:Agent 让运维从经验驱动迈向智能协同
人工智能·elasticsearch·agent
贾天佑忆月1 天前
Mac 本地项目推送至 GitHub 完整指南
elasticsearch·macos·github
望江东浪1 天前
我的 Claude Code 效率工具全套配置分享
大数据·elasticsearch·搜索引擎
Elasticsearch2 天前
如何使用 OpenTelemetry 在 Elastic 上构建搜索分析,无需额外的管道
elasticsearch
Elasticsearch2 天前
用于 Kubernetes 监测的常用 ES|QL 查询
elasticsearch
本旺2 天前
Elasticsearch 前缀匹配性能优化实战,附4种方式即执行计划判别
大数据·elasticsearch·性能优化
Elastic 中国社区官方博客2 天前
Elastic 在 Everest Group 企业搜索产品 PEAK Matrix® 评估 2026 中被评为领导者
大数据·运维·人工智能·elasticsearch·搜索引擎·ai·全文检索
Java小白笔记3 天前
Codex Skills 分类整理
大数据·人工智能·elasticsearch·搜索引擎·ai·ai编程·ai写作
Elastic 中国社区官方博客3 天前
谁来评判评判者?在 Elasticsearch Workflows 中使用 LLM-as-a-Judge
大数据·运维·人工智能·elasticsearch·搜索引擎·ai·全文检索