1. 快照(s3 file FS)
  2. 跨集群迁移
  3. es-dump
  4. remote-reindex
  5. Logstash

Elasticsearch 迁移方法

Elasticsearch 迁移是将数据、索引和配置从一个 Elasticsearch 集群转移到另一个集群的过程。以下是几种常见的迁移方法:

1. 快照和恢复 (Snapshot and Restore)

这是最推荐的迁移方法,适用于大型数据集。

步骤:

  1. 在源集群上创建共享文件系统仓库
1
2
3
4
5
6
7
PUT /_snapshot/my_backup
{
"type": "fs",
"settings": {
"location": "/mnt/backups/my_backup"
}
}
  1. 创建快照
1
2
3
4
5
6
PUT /_snapshot/my_backup/snapshot_1?wait_for_completion=true
{
"indices": "*",
"ignore_unavailable": true,
"include_global_state": false
}
  1. 将备份文件复制到目标集群可访问的位置
  2. 在目标集群上注册相同的仓库
  3. 从快照恢复
1
POST /_snapshot/my_backup/snapshot_1/_restore

2. 使用 Elasticsearch Reindex API

适用于小规模数据或需要转换数据的迁移。

步骤:

  1. 在目标集群创建索引(可选,可定义新映射)
  2. 使用 reindex 从远程集群拉取数据
1
2
3
4
5
6
7
8
9
10
11
12
POST _reindex
{
"source": {
"remote": {
"host": "http://source-cluster:9200"
},
"index": "source_index"
},
"dest": {
"index": "dest_index"
}
}

3. Logstash 迁移

使用 Logstash 作为数据管道进行迁移。

示例配置:

1
2
3
4
5
6
7
8
9
10
11
12
input {
elasticsearch {
hosts => ["http://source-cluster:9200"]
index => "source_index"
}
}
output {
elasticsearch {
hosts => ["http://target-cluster:9200"]
index => "target_index"
}
}

4. 使用 Elasticsearch-dump 工具

适用于小型索引的迁移。

安装:

1
npm install elasticdump -g

使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 导出映射
elasticdump \
--input=http://source:9200/my_index \
--output=my_index_mapping.json \
--type=mapping

# 导出数据
elasticdump \
--input=http://source:9200/my_index \
--output=my_index_data.json \
--type=data

# 导入到目标集群
elasticdump \
--input=my_index_mapping.json \
--output=http://target:9200/my_index \
--type=mapping

elasticdump \
--input=my_index_data.json \
--output=http://target:9200/my_index \
--type=data

迁移注意事项

  1. 版本兼容性:确保目标集群版本兼容源集群版本
  2. 网络带宽:大数据量迁移需要考虑网络带宽
  3. 停机时间:根据业务需求规划可能的停机窗口
  4. 验证数据:迁移后务必验证数据完整性和一致性
  5. 安全性:确保迁移过程中的数据传输安全

选择哪种方法取决于您的具体需求、数据量、Elasticsearch 版本和可用资源。

更多 Elasticsearch 迁移方法

除了之前提到的方法,这里还有几种额外的 Elasticsearch 迁移方案,适用于不同场景:

5. 跨集群复制 (CCR - Cross Cluster Replication)

适用场景:需要持续同步的迁移或零停机时间迁移

要求

  • 需要 Elasticsearch 7.0+ 商业版(白金许可)
  • 两个集群必须能够相互通信

步骤

  1. 在源集群(leader)上启用 CCR:
1
2
3
4
5
6
PUT /_cluster/settings
{
"persistent": {
"cluster.remote.remote_cluster.seeds": ["<target_cluster_transport_address>:9300"]
}
}
  1. 在目标集群(follower)上创建跟随索引:
1
2
3
4
5
POST /<index_name>/_ccr/follow
{
"remote_cluster": "remote_cluster",
"leader_index": "<index_name>"
}
  1. 当数据同步完成后,可以停止复制关系

10. 自定义工具迁移

对于特殊需求,可以开发自定义迁移工具:

  • 基于Scroll API的批量导出
1
2
3
4
5
6
7
8
9
10
from elasticsearch import Elasticsearch, helpers

es_source = Elasticsearch(['source_host'])
es_target = Elasticsearch(['target_host'])

query = {"query": {"match_all": {}}}
scroll_size = 1000

docs = helpers.scan(es_source, index="source_index", query=query, size=scroll_size)
helpers.bulk(es_target, docs, index="target_index")