路漫漫其修远兮
吾将上下而求索

elasticsearch学习:索引跨集群迁移

迁移目标

  • 将老ES集群Index:zrcf_index_q,Type:archives中的文档迁移至新ES集群zrcf_index_q_archives索引中

  • 将老ES集群Index:zrcf_index_q,Type:snapshot中的文档迁移至新ES集群zrcf_index_q_snapshot索引中

迁移前准备

  • 老ES集群地址:10.16.37.194:9200

  • 新ES集群地址: 10.16.16.144:9200

  • 老Index索引名称: zrcf_index_q

  • 老Index Type名称:archives, snapshot

  • 新Index索引名称: zrcf_index_q_archives, zrcf_index_q_snapshot

  • 新Index索引mapping: {"mappings":{…}}

白名单 -> 目标集群添加白名单允许远端集群reindex

  • 目标集群副本数设置为1,防止有索引0副本,停止es.node节点导致索引red

# 在新ES集群节点10.16.16.144上执行curl -XPUT -H 'Content-Type: application/json' "http://10.16.16.144:9200/_settings" -d '{"index":{"number_of_replicas":1}}'
  • 修改新ES集群任意node节点的elasticsearch.yml配置文件

# 10.16.37.194为老ES集群node节点IPecho 'reindex.remote.whitelist: "10.16.37.194:9200"'  >> /home/ziroom/runtime/elasticsearch/config/elasticsearch.yml
  • 重启修改elasticsearch.yml配置文件的node节点

# 停机前,确保集群状态green
curl -XGET "http://10.16.16.144:9200/_cat/health?v"
# kill elasticsearch pid 
ssh 10.16.16.144
jps|grep -i elastic|awk '{print $1}'|xargs kill
# 启动ES
ssh 10.16.16.144
su - ziroom -c 'cd /home/ziroom/runtime/elasticsearch/bin/ && ./elasticsearch -d'

在新ES集群创建新索引及设置mapping

  • 创建zrcf_index_q_archives索引及mapping

curl -XPUT -H 'Content-Type:application/json' "http://10.16.16.144:9200/zrcf_index_q_archives" -d '{"mappings":{...}}'
  • 创建zrcf_index_q_snapshot索引及mapping

curl -XPUT -H 'Content-Type:application/json' "http://10.16.16.144:9200/zrcf_index_q_snapshot" -d '{"mappings":{...}}'

将新ES集群新建索引的副本数改为0

  • 设置zrcf_index_q_archives, zrcf_index_q_snapshot副本数为0,减少reindex写入时间

curl -XPUT -H 'Content-Type: application/json' "http://10.16.16.144:9200/zrcf_index_q_archives,zrcf_index_q_snapshot/_settings" -d '{"index":{"number_of_replicas":0}}'

Reindex导入数据

  • 老ES集群索引需要停止写入,保证数据完整性

# zrcf_index_q_archives索引数据导入
curl -XPOST-H 'Content-Type: application/json' "http://10.16.16.144:9200/_reindex?pretty" -d '
{
  "source": {
    "remote": {
      "host": "http://10.16.37.194:9200"
    },
    "index": "zrcf_index_q",
    "type": "archives"
  },
  "dest": {
    "index": "zrcf_index_q_archives"
  }
}'
# zrcf_index_q_archives索引数据导入
curl -XPOST -H 'Content-Type: application/json' "http://10.16.16.144:9200/_reindex?pretty" -d '
{
  "source": {
    "remote": {
      "host": "http://10.16.37.194:9200"
    },
    "index": "zrcf_index_q",
    "type": "snapshot"
  },
  "dest": {
    "index": "zrcf_index_q_snapshot"
  }
}'

将新ES集群新建索引的副本数改为1

  • 设置zrcf_index_q_archives, zrcf_index_q_snapshot副本数为1,保证数据高可用性

curl -XPUT -H 'Content-Type: application/json' "http://10.16.16.144:9200/zrcf_index_q_archives,zrcf_index_q_snapshot/_settings" -d '{"index":{"number_of_replicas":1}}'

查看索引及mapping

  • 查看索引

curl  -XGET '10.16.16.144:9200/_cat/indices/zrcf_index_q_*?v'
  • 查看mapping

# mapping: zrcf_index_q_archives
curl  -XGET '10.16.16.145:9200/zrcf_index_q_archives/_mapping?pretty'
# mapping: zrcf_index_q_snapshot
curl  -XGET '10.16.16.145:9200/zrcf_index_q_snapshot/_mapping?pretty'

问题:数据同步速度慢

  • 解决方法: 如果单索引数据量比较大,可以在迁移前将目标索引的副本数设置为 0,刷新时间为-1。待数据迁移完成后,再更改回来,加快数据同步速度。

// 迁移索引数据前可以先将索引副本数设为0,不刷新,用于加快数据迁移速度。
curl -u user:password -XPUT 'http://<host:port>/indexName/_settings' -d' {
        "number_of_replicas" : 0,
        "refresh_interval" : "-1"
}'
// 索引数据迁移完成后,可以重置索引副本数为1,刷新时间1s(1s是默认值)。
curl -u user:password -XPUT 'http://<host:port>/indexName/_settings' -d' {
        "number_of_replicas" : 1,
        "refresh_interval" : "1s"
}'
  • 提升批量写入大小值,默认情况下,_reindex使用1000进行批量操作,您可以在source中调整batch_size

curl -XPOST -H 'Content-Type: application/json' "http://10.16.16.144:9200/_reindex?pretty" -d '
{
  "source": {
    "remote": {
      "host": "http://10.16.37.194:9200"
    },
    "index": "zrcf_index_q",
    "type": "snapshot",
    "size": 5000
  },
  "dest": {
    "index": "zrcf_index_q_snapshot"
  }
}'
  • 借助scroll的sliced提升写入效率

    • Reindex支持Sliced Scroll以并行化重建索引过程,这种并行化可以提高效率,并提供一种方便的方法将请求分解为更小的部分。

    • 用过Scroll接口吧,很慢?如果你数据量很大,用Scroll遍历数据那确实是接受不了,现在Scroll接口可以并发来进行数据遍历了

    • 每个Scroll请求,可以分成多个Slice请求,可以理解为切片,各Slice独立并行,利用Scroll重建或者遍历要快很多倍

    • slicing的设定分为两种方式:手动设置分片、自动设置分片。

curl -XPOST -H 'Content-Type: application/json' "http://10.16.16.144:9200/_reindex?slices=5&refresh" -d '
{
  "source": {
    "remote": {
      "host": "http://10.16.37.194:9200"
    },
    "index": "zrcf_index_q",
    "type": "snapshot"
  },
  "dest": {
    "index": "zrcf_index_q_snapshot"
  }
}'
  • slices大小设置注意事项:

    • slices大小的设置可以手动指定,或者设置slices设置为auto,auto的含义是:针对单索引,slices大小=分片数;针对多索引,slices=分片的最小值

    • 当slices的数量等于索引中的分片数量时,查询性能最高效。slices大小大于分片数,非但不会提升效率,反而会增加开销

    • 如果这个slices数字很大(例如500),建议选择一个较低的数字,因为过大的slices 会影响性能

未经允许不得转载:江哥架构师笔记 » elasticsearch学习:索引跨集群迁移

分享到:更多 ()

评论 抢沙发

评论前必须登录!