1.Elasticsearch(6.6.0及以上版本)提供了ILM功能
2.索引生命周期分为4个阶段:hot、warm、cold、delete。
其中hot阶段主要负责对索引进行滚动更新操作。
阶段 | 描述 |
---|---|
hot | 主要处理时序数据的实时写入。可根据索引的文档数、大小、时长决定是否调用rollover API来滚动更新索引(通过policy自动管理) |
warm | 索引不再写入,主要用来提供查询 |
cold | 索引不再更新,查询很少,查询速度会变慢 |
delete | 索引即将删除 |
带有template
1.建立policy
## 注意关注自己的节点标签,例如 "box_type": "warm"
PUT /_ilm/policy/first_ilm_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_age": "60s",
"max_docs": "10"
}
}
},
"warm": {
"min_age": "5s",
"actions": {
"allocate": {
"include": {
"box_type": "warm"
}
}
}
},
"cold": {
"min_age": "20s",
"actions": {
"allocate": {
"include": {
"box_type": "cold"
}
}
}
},
"delete": {
"min_age": "40s",
"actions": {
"delete": {}
}
}
}
}
}
2.建立模版
## 建立与policy绑定的template(为了让后续的index都与policy绑定)
PUT /_template/zzmc_template
{
"order":1,
"index_patterns": ["zzmc-*"],
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"index.lifecycle.name": "first_ilm_policy",
"index.lifecycle.rollover_alias": "zzmc",
"index.routing.allocation.include.boxtype": "all"
}
}
3.建立初始索引
## 根据模版创建初始索引
PUT zzmc-1
{
}
4.建立别名
## 给初始索引设置别名,后续rollover会将别名指向新的索引
POST /_aliases
{
"actions": [
{
"add": {
"index": "zzmc-1",
"alias": "zzmc"
}
}
]
}
不带template(注:该方案不可行,新建一次索引之后会发现新的索引里面不带policy信息了,无法继续rollover)
1.建立policy,与上述一致,换一个写法
## 注意关注自己的节点标签,例如 "boxtype": "node"
PUT /_ilm/policy/first_ilm_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_age": "60s",
"max_docs": "10"
}
}
},
"warm": {
"min_age": "25s",
"actions": {
"allocate": {
"include": {
"boxtype": "node"
}
}
}
},
"delete": {
"min_age": "40s",
"actions": {
"delete": {}
}
}
}
}
}
2.建立初始索引(setting)
## 可以放到一起,也可以分开,如下demo分了两步主要是为了表达清晰
PUT test-1
{
}
PUT /test-*/_settings
{
"index.lifecycle.name": "second_ilm_policy",
"index.lifecycle.rollover_alias": "test",
"index.routing.allocation.include.boxtype": "all"
}
3.建立别名
## 给初始索引建立别名,后续rollover会将别名指向新的索引
POST /_aliases
{
"actions": [
{
"add": {
"index": "test-1",
"alias": "test"
}
}
]
}
测试时候设置的策略比较短,比如60s自动生成的设置不会生效,测试下来发现大概十分钟左右才会生效;
该现象和 indices.lifecycle.poll_interval 参数有关,可以参考官方文档:
官方文档
更多命令可参考博客:
博客