elasticsearch 集群迁移

一、两个集群数据迁移前提

  • 两个集群网络互通
  • 两个集群版本相同(v6.5.4)

二、集群样例信息

源集群:(source_cluster)

节点IP 节点名称 节点角色 是否为master节点
192.168.0.1 s_node1 data,master 是
192.168.0.2 s_node2 data,master 否
192.168.0.3 s_node3 data,master 否
目标集群:(target_cluster)
节点IP 节点名称 节点角色 是否为master节点
192.168.1.1 t_node1 data,master 是
192.168.1.2 t_node2 data,master 否
192.168.1.3 t_node3 data,master 否

三、操作步骤

3.1:集群改造

源集群和目标集群合并成一个大集群,修改目标集群的配置信息。

3.1.1 目标集群改造
  • 关闭目标集群
  • 修改目标集群所有节点的集群名称,与源集群名称一样
    cluster.name: target_cluster
  • 修改目标集群所有节点dscovery.zen.ping.unicast.hosts:,把源集群节点IP加入。
    discovery.zen.ping.unicast.hosts:
    [“192.168.0.1”, “192.168.0.2”,”192.168.0.3”,”192.168.1.1”, “192.168.1.2”,”192.168.1.3”]
3.1.2 关闭源集群索引均衡功能,防止目标集群节点还未全部加入完成,数据开始迁移。
1
2
3
4
5
6
curl -XPUT "http://192.168.0.1:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
"transient" :
"cluster.routing.rebalance.enable" : "none"
}
}'
3.1.3 逐个开启目标集群节点加入到源集群,同时保证集群运行的状态为green。
3.1.4 检查新集群健康状态、节点信息、索引状态
1
2
3
curl -XGET "http://192.168.0.1:9200/_cluster/health?pretty"
curl -XGET "http://192.168.0.1:9200/_cat/nodes?v"
curl -XGET "http://192.168.0.1:9200/_cat/indices?v"

3.2:数据迁移

3.2.1 开启均衡和源集群节点数据下线,数据会迁移到目标集群
1
2
3
4
5
6
7
curl -XPUT "http://192.168.0.1:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"transient": {
"cluster.routing.allocation.exclude._name": "s_node1,s_node2,s_node3",
"cluster.routing.rebalance.enable" : "all"
}
}'
3.2.2 检查数据迁移状态,节点迁移结束标准:
1
2
3
curl -X GET '192.168.0.1:9200/_nodes/s_node1/stats/indices/docs?pretty'
curl -X GET '192.168.0.1:9200/_nodes/s_node2/stats/indices/docs?pretty'
curl -X GET '192.168.0.1:9200/_nodes/s_node3/stats/indices/docs?pretty'

源集群所有节点数据迁移完成,执行结果都为:

1
2
3
4
5
6
"indices" : {
"docs" : {
"count" : 0,
"deleted" : 0
}
}
3.2.3 迁移中,可能有IO,网络资源不足情况,可以做一些调整

https://www.elastic.co/guide/en/elasticsearch/reference/6.5/shards-allocation.html

  • cluster.routing.allocation.node_concurrent_incoming_recoveries
  • cluster.routing.allocation.node_concurrent_outgoing_recoveries
  • cluster.routing.allocation.node_concurrent_recoveries
  • cluster.routing.allocation.node_initial_primaries_recoveries
1
2
3
4
5
6
7
curl -XPUT "http://192.168.0.1:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"transient": {
"cluster.routing.allocation.cluster_concurrent_rebalance": 4,
"cluster.routing.allocation.node_concurrent_recoveries": 10
}
}'
3.2.4 如果索引恢复速度慢,可以复速度修改

https://www.elastic.co/guide/en/elasticsearch/reference/6.5/recovery.html

  • indices.recovery.max_bytes_per_sec
1
2
3
4
5
6
7
curl -XPUT "http://192.168.0.1:9200/_cluster/settings?flat_settings=true&pretty" -H 'Content-Type: application
/json' -d'
{
"transient": {
"indices.recovery.max_bytes_per_sec": "100mb"
}
}'

3.3:更改客户端连接到目标集群节点IP

“192.168.0.1”, “192.168.0.2”,”192.168.0.3” ==> “192.168.1.1”, “192.168.1.2”,”192.168.1.3”
保障客户端正常运行

3.4 四:源集群关闭

  • 先关闭源集群非master节点,同时检查当前运行集群状态是否为green;如果出现异常,停止操作,恢复当前关闭节点。
    操作顺序 操作动作

stop s_node2
stop s_node3

  • 最后关闭源集群master节点,同时检查当前运行集群状态是否为green。如果出现异常,可以配置discovery.zen.ping.unicast.hosts:
    [“192.168.0.1”, “192.168.0.2”,”192.168.0.3”,”192.168.1.1”, “192.168.1.2”,”192.168.1.3”],恢复当前关闭节点。
    操作顺序 操作动作

stop s_node1

  • 关闭所有源集群节点后,检查目标集群是否健康,索引数据是否正常;如果没有问题,分离成功,数据迁移完毕。

3.5:恢复目标集群配置正常

  • 取消节点下线配置
1
2
3
4
5
6
curl -XPUT "http://192.168.1.1:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"transient": {
"cluster.routing.allocation.exclude._name": null
}
}'

四、可能风险

  • 在迁移数据时,ES节点物理机出现异常,丢失数据
  • 网络出现异常,出现脑裂