公司测试的elasticsearch是直接在ECS上安装的,现在需要迁移服务器,准备通过docker安装一个elasticsearch,并将数据迁移过去。

查询资料,有多种迁移方式。

  • 通过Oss快照
  • 通过阿里云Logstash的管道配置功能实现
  • 通过reindex方式

本文通过reindex方式,将旧数据迁移到新的es中。

Docker启动elasticsearch

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
docker stop elasticsearch
docker rm elasticsearch
docker run --name elasticsearch -d \
-p 9200:9200 \
-e "discovery.type=single-node" \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
-v /data/services/elasticsearch/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /data/services/elasticsearch/data:/usr/share/elasticsearch/data \
docker.elastic.co/elasticsearch/elasticsearch:7.2.0

配置文件elasticsearch.yml

1
2
cluster.name: "docker-cluster"
network.host: 0.0.0.0

安装可能错误

问题一

es ElasticsearchException[failed to bind service 权限错误,挂载data目录权限不足

问题二

OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release

修改启动参数内存大小

迁移准备

  • 为保证数据迁移前后一致,需要上游业务停止自建Elasticsearch集群的写操作,读操作才可以正常进行。迁移完毕后,直接切换到阿里云Elasticsearch集群进行读写操作。如果不停止写操作,可能会导致迁移前后数据不一致。
  • 使用以下方案迁移数据时,如果是通过IP:Port的方式访问自建Elasticsearch集群,则必须在阿里云Elasticsearch集群的YML文件中配置reindex白名单,添加自建Elasticsearch集群的IP地址,例如reindex.remote.whitelist: 1.1.1.1:9200,1.2.*.*:*,详情请参见配置YML参数

增加白名单设置

1
2
3
cluster.name: "docker-cluster"
network.host: 0.0.0.0
reindex.remote.whitelist: "172.18.128.152:9200, localhost:*"

迁移Api

使用reindex api 将源集群索引迁移到目标集群。
需要单个索引,慢慢迁移。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
"source": {
"remote": {
"host": "http://otherhost:9200",
"username": "user",
"password": "pass"
},
"index": "source",
"query": {
"match": {
"test": "data"
}
}
},
"dest": {
"index": "dest"
}
}
'

通过该api,我们可以写一个reindex.sh,将索引迁移过去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
# file:reindex.sh
indexName="您的索引名"
newClusterUser="新集群用户名"
newClusterPass="新集群密码"
newClusterHost="新集群host"
oldClusterUser="旧集群用户名"
oldClusterPass="旧集群密码"
# 旧集群host必须是[scheme]://[host]:[port],例如http://10.37.1.*:9200
oldClusterHost="旧集群host"
curl -u ${newClusterUser}:${newClusterPass} -XPOST "http://${newClusterHost}/_reindex?pretty" -H "Content-Type: application/json" -d'{
"source": {
"remote": {
"host": "'${oldClusterHost}'",
"username": "'${oldClusterUser}'",
"password": "'${oldClusterPass}'"
},
"index": "'${indexName}'",
"query": {
"match_all": {}
}
},
"dest": {
"index": "'${indexName}'"
}
}'

Docker安装Kibana

通过Kibana可以方便查看elasticsearch数据

1
2
3
4
5
6
7
docker stop kibana
docker rm kibana

docker run -d --name kibana \
-p 5601:5601 \
-v /data/services/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml \
kibana:7.2.0

配置文件kibana.yml

1
2
3
4
5
# Default Kibana configuration for docker target
server.name: kibana
server.host: "0"
elasticsearch.hosts: [ "http://127.0.0.1:9200" ]
xpack.monitoring.ui.container.elasticsearch.enabled: true

参考文章

https://help.aliyun.com/knowledge_detail/61145.html?spm=a2c4g.11186623.6.773.3a937868IDlBrS

https://www.elastic.co/guide/en/elasticsearch/reference/6.7/docs-reindex.html

https://elasticsearch.cn/question/9120