ES 生命周期管理

一 .概念

ILM定义了四个生命周期阶段:
Hot:正在积极地更新和查询索引。
Warm:不再更新索引,但仍在查询。
cold:不再更新索引,很少查询。信息仍然需要可搜索,但是如果这些查询速度较慢也可以。
Delete:不再需要该索引,可以安全地将其删除

仅仅在 Hot阶段 可以设置 rollover滚动

在实验中可以修改设置,来缩短ILM检测时间间隔。ILM定期运行(indices.lifecycle.poll_interval),默认是10分钟,检查索引是否符合策略标准,并执行所需的任何步骤

PUT /_cluster/settings

{ "transient": { "indices.lifecycle.poll_interval": "1m" } }

参数的设置:满足任意一个创建新的索引

HOT

WARM

Delete

二. 实战

1 .创建生命周期策略模板:PUT 请求

http://localhost:9099/_ilm/policy/my_index_rollover_policy

"max_docs": 5000000 设置超过500万是创建新的索引,实际测试可以设置小一点

{
    "policy": {
        "phases": {
            "hot": {
                "actions": {
                    "rollover": {
                        "max_docs": 5000000
                    },
                    "set_priority": {
                        "priority": 100
                    }
                }
            },
            "warm": {
                "min_age": "30d",
                "actions": {
                    "forcemerge": {
                        "max_num_segments": 1
                    },
                    "shrink": {
                        "number_of_shards": 1
                    },
                    "allocate": {
                        "number_of_replicas": 1
                    },
                    "set_priority": {
                        "priority": 50
                    }
                }
            },
            "cold": {
                "min_age": "90d",
                "actions": {
                    "freeze": {},
                    "allocate": {
                        "require": {
                            "type": "cold"
                        }
                    },
                    "set_priority": {
                        "priority": 10
                    }
                }
            },
            "delete": {
                "min_age": "3650d",
                "actions": {
                    "delete": {}
                }
            }
        }
    }
}

2. 创建索引模板(日志统计索引) PUT 请求:

http://localhost:9099/_template/my_statistic_log

{
    "index_patterns": [
        "my_statistic_log-*"
    ],
    "settings": {
        "index.number_of_shards": 5,
        "index.number_of_replicas": 1,
        "refresh_interval": "60s",
        "index.lifecycle.name": "my_index_rollover_policy",
        "index.lifecycle.rollover_alias": "statistic_log_write"
    },
    "aliases": {
        "statistic_log_read": {}
    },
    "mappings": {
        "log": {
            "dynamic": "strict",
            "_field_names": {
                "enabled": false
            },
            "dynamic_templates": [
                {
                    "date": {
                        "match": "*Date",
                        "mapping": {
                            "type": "date"
                        }
                    }
                },
                {
                    "strings": {
                        "match_mapping_type": "string",
                        "mapping": {
                            "type": "keyword"
                        }
                    }
                },
                {
                    "long_or_text": {
                        "match": "*Time",
                        "mapping": {
                            "type": "date"
                        }
                    }
                },
                {
                    "dataFormat": {
                        "match": "*Date",
                        "mapping": {
                            "type": "date"
                        }
                    }
                },
                {
                    "timeFormat": {
                        "match": "*Time",
                        "mapping": {
                            "type": "date"
                        }
                    }
                },
                {
                    "noAnalyzed": {
                        "match_mapping_type": "string",
                        "mapping": {
                            "type": "keyword"
                        }
                    }
                }
            ],
            "properties": {
                "area": {
                    "type": "keyword"
                },
                "desc": {
                    "type": "keyword"
                },
                "hour": {
                    "type": "integer"
                },
                "day": {
                    "type": "integer"
                },
                "startTime": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss"
                },
                "totalCount": {
                    "type": "long"
                }
            }
        }
    }
}

3.    根据索引模板创建起始索引(日志统计索引)
put 请求: http://localhost:9099/my_statistic_log-000001

{
    
    "aliases": {
        "statistic_log_write": {
            "is_write_index": true
        }
    }
}

4.    查看索引生效:GET请求 
http://localhost:9099/my_statistic_log-000001/_ilm/explain

https://www.elastic.co/guide/en/elasticsearch/reference/6.8/_setting_up_a_new_policy.html

要在Elasticsearch中创建生命周期,您需要执行以下步骤: 1. 配置索引模板:首先,您需要定义一个索引模板,该模板将包含有关如何配置生命周期策略的信息。您可以使用Elasticsearch的索引模板API来创建和管理索引模板。在模板中,您可以指定索引名称的模式匹配、索引别名、设置和映射等。 2. 创建生命周期策略:接下来,您需要创建一个生命周期策略,该策略将定义索引在不同阶段的行为,如何管理数据的保留期、删除旧数据等。您可以使用Elasticsearch生命周期策略API来创建和管理生命周期策略。在策略中,您可以定义不同阶段的动作,如设置索引生命周期、转换索引状态、执行某些操作等。 3. 关联策略和模板:一旦您创建了生命周期策略和索引模板,接下来需要将它们关联起来。您可以使用Elasticsearch的索引模板API将生命周期策略与相应的索引模板关联起来。这样,当新的索引符合模板的条件时,生命周期策略将自动应用于该索引。 4. 测试和验证:最后,在将生命周期策略应用于实际的索引之前,建议进行测试和验证。您可以手动创建一个测试索引,并使用模板和策略进行测试。确保策略按预期工作,并且索引在不同阶段的行为符合您的需求。 通过按照上述步骤创建和管理生命周期,您可以自动化索引的生命周期管理,包括数据保留、删除旧数据等操作,从而优化存储和性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值