Elasticsearch ILM DSL 索引生命周期管理

1、冷热集群架构

冷热架构也叫冷暖架构,是“Hot-Warm” Architecture的中文翻译。

冷热架构本质是给节点设置不同的属性,让每个节点具备了不同的属性。为演示 ILM,需要首先配置冷热架构,三个节点在 elasticsearch.yml 分别设置的属性如下:

 

查看es冷热集群架构

2、不同阶段(Phrase)的功能点(Acitons)

 

注意:仅在 Hot 阶段可以设置:Rollover 滚动。

3、各生命周期 Actions 设定

后两节演示要用。

3.1 Hot 阶段

基于:max_age=3天、最大文档数为3、最大size为:30gb rollover 滚动索引。

设置优先级为:100(值越大,优先级越高)。

3.2 Warm 阶段

实现段合并,max_num_segments 设置为1.

副本设置为 0。

数据迁移到:warm 节点。

优先级设置为:50。

3.3 Cold 阶段

冷冻索引

数据迁移到冷节点

3.4 Delete 阶段

删除索引

关于触发滚动的条件:

Hot 阶段的触发条件:手动创建第一个满足模板要求的索引。

其余阶段触发条件:min_age,索引自创建后的时间。

时间类似:业务里面的 热节点保留 3 天,温节点保留 7 天,冷节点保留 30 天的概念。

4、 索引生命周期管理 ILM DSL

# step1: 前提:演示刷新需要,将索引生命管理刷新为1秒
 curl  -XPUT "elastic:Ntn71CrUSaTjk7TNWora@172.26.180.1:9200/_cluster/settings" -H 'Content-Type: application/json' -d'{
  "persistent": {
    "indices.lifecycle.poll_interval": "1s"
  }
}'


# step2:测试需要,值调的很小
 curl  -XPUT "elastic:Ntn71CrUSaTjk7TNWora@172.26.180.1:9200/_ilm/policy/my_test_policy"   -H 'Content-Type: application/json'  -d'{
  "policy": {
    "phases": {
      "hot": {
        "actions": {
          "rollover": {
            "max_age": "3d",
            "max_docs": 3,
            "max_size": "30gb"
          },
          "set_priority": {
            "priority": 100
          }
        }
      },
      "warm": {
        "min_age": "50s",
        "actions": {
          "forcemerge": {
            "max_num_segments": 1
          },
          "allocate": {
            "require": {
              "box_type": "warm"
            },
            "number_of_replicas": 0
          },
          "set_priority": {
            "priority": 50
          }
        }
      },
      "cold": {
        "min_age": "50s",
        "actions": {
          "allocate": {
            "require": {
              "box_type": "cold"
            }
          },
          "freeze": {}
        }
      },
      "delete": {
        "min_age": "60s",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

# step3:创建模板,关联配置的ilm_policy
 curl  -XPUT "elastic:Ntn71CrUSaTjk7TNWora@172.26.180.1:9200/_index_template/ilm_template" -H 'Content-Type: application/json' -d'{
  "index_patterns": ["ilm-*"],                 
  "template": {
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 0,
      "index.lifecycle.name": "my_test_policy",      
      "index.lifecycle.rollover_alias": "ilm",
      "index.routing.allocation.require.box_type": "hot"
    }
  }
}'

# step4:创建起始索引(便于滚动)
 curl  -XPUT "elastic:Ntn71CrUSaTjk7TNWora@172.26.180.1:9200/ilm-000001"  -H 'Content-Type: application/json' -d'{
  "aliases": {
    "ilm": {
      "is_write_index": true
    }
  }
}'

# step5:插入数据
 curl  -XPUT "elastic:Ntn71CrUSaTjk7TNWora@172.26.180.1:9200/ilm/_doc/1"  -H 'Content-Type: application/json' -d'
{"title":"count1"}
'
 curl  -XPUT "elastic:Ntn71CrUSaTjk7TNWora@172.26.180.1:9200/ilm/_doc/2"  -H 'Content-Type: application/json' -d'
{"title":"count2"}
'
查看索引的数据量
curl 'http://elastic:Ntn71CrUSaTjk7TNWora@172.26.180.1:9200/_cat/count/ilm?v&pretty'

查案索引分片的分布
curl 'http://elastic:Ntn71CrUSaTjk7TNWora@172.26.180.1:9200/ilm/_search_shards?&pretty'

# step6:临界值(会滚动)
 curl  -XPUT "elastic:Ntn71CrUSaTjk7TNWora@172.26.180.1:9200/ilm/_doc/3"  -H 'Content-Type: application/json' -d'
{"title":"count3"}
' 
# 下一个索引数据写入(max_docs 满足条件索引会由hot -> warn -> cold & delete )
 curl  -XPUT "elastic:Ntn71CrUSaTjk7TNWora@172.26.180.1:9200/ilm/_doc/4"  -H 'Content-Type: application/json' -d'
{"title":"count4"}
'

 

索引生命周期管理效果:

图片

核心步骤总结如下:

第一步:创建生周期 policy,定义phases与actions。

第二步:创建索引模板,模板中关联 policy 和别名。

第三步:创建符合模板的起始索引,并插入数据。

第四步: 索引基于配置的 ilm 滚动,触发各个阶段的actions。

posted on 2023-04-24 09:53  张冲andy  阅读(46)  评论(0编辑  收藏  举报

导航