/article/2019/04/19/elastic-es-index-lifecycle/

Elasticsearch 索引生命周期管理(lifecycle)

  • 【作者】看不见我
  • 【分类】Service
  • 【发布】2019-04-19 09:40
  • 【更新】2019-04-19 09:40

索引生命周期(index lifecycle)

索引生命周期管理在ES6.6开始支持,在ES7.0已经是稳定版特性了。

  • https://www.elastic.co/guide/en/elasticsearch/reference/current/index-lifecycle-management.html
  • https://www.elastic.co/guide/en/elasticsearch/reference/current/index-lifecycle-management-api.html#index-lifecycle-management-api

索引生命周期管理包含内容: - 生命周期任务管理(_ilm) - 生命周期策略管理(policy) - 索引周期策略管理

生命周期任务管理(_ilm)

接口:

# 获取 _ilm 运行状态
GET /_ilm/status

# 关闭 _ilm 任务
POST /_ilm/stop

# 启动 _ilm 任务
POST /_ilm/start

使用

curl 'http://192.168.13.56:9200/_ilm/status?pretty'
curl 'http://192.168.13.56:9200/_ilm/stop?pretty' -H 'Content-Type: application/json' -XPOST
curl 'http://192.168.13.56:9200/_ilm/start?pretty' -H 'Content-Type: application/json' -XPOST

生命周期策略管理(policy)

lifesycle包含4个阶段(stages)

  • hot: The index is actively being written to
  • warm: The index is generally not being written to, but is still queried
  • cold: The index is no longer being updated and is seldom queried. The information still needs to be searchable, but it’s okay if those queries are slower.
  • delete: The index is no longer needed and can safely be deleted

lifesycle策略接口(policy)

# 创建和更新policy:
PUT _ilm/policy/<policy_id>

# 查看policy:
GET _ilm/policy
GET _ilm/policy/<policy_id>

# 删除policy:
DELETE _ilm/policy/<policy_id>

使用policy接口

# 创建lifecycle策略:
curl '192.168.13.56:9200/_ilm/policy/my_policy' -H 'Content-Type: application/json' -XPUT -d '
{
  "policy": {
    "phases": {
      "warm": {
        "min_age": "10d",
        "actions": {
          "forcemerge": {
            "max_num_segments": 1
          }
        }
      },
      "delete": {
        "min_age": "30d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}
'


# 查看lifecycle策略:
curl '192.168.13.56:9200/_ilm/policy/my_policy?pretty' -XGET
# 查看所有策略
curl '192.168.13.56:9200/_ilm/policy?pretty' -H 'Content-Type: application/json' -XGET


# 删除lifecycle策略:
curl '192.168.13.56:9200/_ilm/policy/my_policy?pretty' -H 'Content-Type: application/json' -XDELETE

索引周期策略管理

在模版中添加生命周期策略:

curl '192.168.13.56:9200/_template/my_template' -H 'Content-Type: application/json' -XPUT -d'
{
  "index_patterns": ["test-*"], 
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1,
    "index.lifecycle.name": "my_policy"
  }
}
'

配置索引的生命周期策略:

curl '192.168.13.56:9200/my_index/_settings' -H 'Content-Type: application/json' -XPUT -d'
{
  "lifecycle.name": "my_other_policy"
}
'

从索引中移除生命周期策略:

# POST <index>/_ilm/remove
curl '192.168.13.56:9200/my_index/_ilm/remove' -H 'Content-Type: application/json' -XPOST

查看索引的生命周期当前阶段状态:

curl '192.168.13.56:9200/myindex/_ilm/explain?pretty'

对索引重试lifecycle策略执行失败的操作:

curl '192.168.13.56:9200/myindex/_ilm/retry?pretty' -H 'Content-Type: application/json' -XPOST
Top