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


    通过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": {}
    				}
    			}
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    查询索引生命周期策略

    GET /_ilm/policy/auto_delete_policy
    
    • 1

    创建索引模板

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

    PUT _template/elk_template
    
    {
    	"index_patterns": [
    		"my*",
    		"index*"
    	],
    	"template": {
    		"settings": {
    			"index": {
    				"lifecycle": {
    					"name": "auto_delete_policy",
    					"indexing_complete": "true"
    				}
    			}
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    创建索引模板(elk_tempalte),index.lifecycle.name 把上面的自动删除策略绑定到elk索引模板
    后来新生成 my-、index- 开头的索引时就会应用这个模板。
    indexing_complete:true,必须设为true,跳过HOT阶段的Rollover

    查询索引模板

    GET _template/elk_template
    
    • 1

    测试

    测试设置
    生命周期策略默认10分钟检测一次,为了方便测试,这里设为30s。后面改回来就可以了。

    PUT /_cluster/settings
    {
    	"transient": {
    		"indices.lifecycle.poll_interval": "30s"
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    查看索引

    查看新生成的索引,有没有关联到索引生命周期策略,
    这里查看my-开头的索引情况

    GET my-*/_ilm/explain
    
    • 1

    返回

    {
    	"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
    			}
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    参考官网索引管理章节

  • 相关阅读:
    [ROS系列]ubuntu 20.04 从零配置orbslam3(无坑版)
    SpringMVC学习篇(三)
    [C++](8)模板的初步了解
    十天学完Vue学习总结
    华为交换技术:BGP基础实验
    maven的下载以及配置的详细教程(附网盘下载地址)
    数据库学习
    antd 表格getCheckboxProps禁用
    【操作系统】操作系统的大端模式和小端模式
    JavaWeb 项目 --- 博客系统(前后分离)
  • 原文地址:https://blog.csdn.net/huangliuyu00/article/details/132640712