Elasticsearch 7.x 小白到高手

elasticsearch 7.0 新特性之 索引生命周期管理(

2019-04-22  本文已影响56人  郭彦超

这将是es索引管理的第三篇文章了,本篇将总结策略构建的顺序,方便大家在项目中使用;在开始本篇内容之前,我补发一个phrase比较全的策略配置,并回顾一下其中的几个重点配置项

1、总览

curl -X PUT "localhost:9200/_ilm/policy/full_policy" -H 'Content-Type: application/json' -d'
{
  "policy": {
    "phases": {
      "hot": {
        "actions": {
          "rollover": {
            "max_age": "7d",
            "max_size": "50G"
          }
        }
      },
      "warm": {
        "min_age": "30d",
        "actions": {
          "forcemerge": {
            "max_num_segments": 1
          },
          "shrink": {
            "number_of_shards": 1
          },
          "allocate": {
            "number_of_replicas": 2
          }
        }
      },
      "cold": {
        "min_age": "60d",
        "actions": {
          "allocate": {
            "require": {
              "box_type": "cold"
            }
          }
        }
      },
      "delete": {
        "min_age": "90d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}
'

上面策略大致意思是,当前索引会在7天后或者单分片大于50G后进行归档,并生成新的索引;warm阶段在第30天后触发,将分片数降低为1分片,segment强制合并为1,通过allocation将副本数增大到2;cold阶段第60天触发,会关闭索引,并将数据转移至box_type = cold标签节点;90天后删除索引。

2、操作

使用索引管理功能大体上分两步完成:

a. 我们先定义一个名为 my_policy 的策略,并添加了hot、delete两个phrase

curl -X PUT "localhost:9200/_ilm/policy/my_policy" -H 'Content-Type: application/json' -d'
{
  "policy": {
    "phases": {
      "hot": {
        "actions": {
          "rollover": {
            "max_size": "25GB" 
          }
        }
      },
      "delete": {
        "min_age": "30d",
        "actions": {
          "delete": {} 
        }
      }
    }
  }
}
'

hot 阶段单分片大于25G创建新索引,30天后删除旧索引

b1. 通过创建索引模板使用策略;创建一个索引模板,将 my_policy 赋值给 index.lifecycle.name ,然后创建第一个名为 test-000001 的索引激活整个流程【推荐】

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

curl -X PUT "localhost:9200/test-000001" -H 'Content-Type: application/json' -d'
{
  "aliases": {
    "test-alias":{
      "is_write_index": true 
    }
  }
}
'

其中index.lifecycle.rollover_alias指定了索引别名,通过别名可以查询模板创建的所有索引,is_write_index = true 意思是当前新建索引为写索引,触发hot阶段归档后的索引该值是false

b2. 通过创建索引直接使用策略,在setting中将 my_policy 赋值给 index.lifecycle.name 【不推荐】

PUT test-index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1,
    "index.lifecycle.name": "my_policy"
  }
}

需要注意的是,因为没有使用索引模板,这种方式rollover创建的索引将使用默认配置,并且新的索引也不会引用到策略

上一篇 下一篇

猜你喜欢

热点阅读