Elasticsearch 跨網(wǎng)絡(luò)、跨集群同步選型指南
1、兩個(gè)同步實(shí)戰(zhàn)問(wèn)題
問(wèn)題1:我想從目前的阿里云上6.7版本的es商業(yè)版,遷移到自己的7.10的自建環(huán)境,證書(shū)不一樣,無(wú)法遠(yuǎn)程 無(wú)法ccr,有沒(méi)有實(shí)時(shí)同步的工具呀?還是只能用logstash ?
問(wèn)題2:es 2個(gè)索引數(shù)據(jù)同步有什么組件或者方案嗎?
2、問(wèn)題解析
這是個(gè)經(jīng)常被問(wèn)到的問(wèn)題。涉及到跨版本、跨網(wǎng)絡(luò)、跨集群的索引數(shù)據(jù)的遷移或同步。我們拆解一下:
2.1 跨版本
7.X 是當(dāng)前的主流版本,早期的業(yè)務(wù)系統(tǒng)會(huì)停留在6.X、5.X 甚至 2.X、1.X 版本。
同步數(shù)據(jù)要注意:7.X 和 早期版本的不同?
7.X 版本已經(jīng)經(jīng)歷了7.0——7.12 12+個(gè)小版本的迭代了,且7.0版本發(fā)布時(shí)間:2019-04-10,已經(jīng)過(guò)去了2年+時(shí)間。
同步要關(guān)注的一個(gè)核心點(diǎn):
官方說(shuō)明更具備說(shuō)服力:“Before 7.0.0, the mapping definition included a type name. Elasticsearch 7.0.0 and later no longer accept a default mapping. ”
6.X版本:還有 type 的概念,可以自己定義。
7.X版本:type 就是_doc。
實(shí)戰(zhàn)舉例說(shuō)明:在 7.X 指定 type 寫入數(shù)據(jù):
PUT test-002/mytype/1
{
"title":"testing"
}
會(huì)有如下的警告:
#! [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
2.2 跨網(wǎng)絡(luò)
兩個(gè)集群不在一個(gè)局域網(wǎng)內(nèi),一個(gè)掛在云端、一個(gè)在本地。
這是常見(jiàn)的業(yè)務(wù)場(chǎng)景之一,至少我也這么干過(guò)。
2.3 跨集群
源數(shù)據(jù)和目的數(shù)據(jù)分布在兩個(gè)不同的集群。
3、同步方案對(duì)比
如下幾個(gè)同步方案,我們邊實(shí)戰(zhàn)邊解讀。

3.0 實(shí)戰(zhàn)環(huán)境準(zhǔn)備
為了演示方便,我們把環(huán)境簡(jiǎn)化。復(fù)雜環(huán)境,原理一致。
集群1:云端,單結(jié)點(diǎn)源集群:172.21.0.14:19022。
集群2:云端,單結(jié)點(diǎn)目的集群:172.21.0.14:19205。
兩個(gè)集群共享一臺(tái)云服務(wù)器,CPU:4核,內(nèi)存:8G。
版本都一致,都是 7.12.0 版本。
測(cè)試數(shù)據(jù):100W條(腳本自動(dòng)生成)。

單條記錄如下:
"_source" : {
"name" : "9UCROh3",
"age" : 16,
"last_updated" : 1621579460000
}
3.1 方案一:reindex 跨集群同步
3.1.1 reindex 前置條件:設(shè)置白名單
在目標(biāo)集群上設(shè)置源集群的白名單,具體設(shè)置只能在:elasticsearch.yml 中。
reindex.remote.whitelist: "172.21.0.14:19022"
注意,如下實(shí)戰(zhàn)不要在kibana dev tools測(cè)試,除非你已經(jīng)修改了默認(rèn)超時(shí)時(shí)間。
3.1.2 reindex 同步實(shí)戰(zhàn)
POST _reindex
{
"source": {
"remote": {
"host": "http://172.21.0.14:19022"
},
"index": "test_data",
"size":10000,
"slice": {
"id": 0,
"max": 5
}
},
"dest": {
"index": "test_data_from_reindex"
}
}
兩個(gè)核心參數(shù)說(shuō)明如下:
size:默認(rèn)一次 scroll 值大小是 1000,這里設(shè)置大了 10 倍,是 10000。
slice:把大的請(qǐng)求切分成小的請(qǐng)求,并發(fā)執(zhí)行。(ps:我這里用法不嚴(yán)謹(jǐn))。
3.1.3 reindex 同步實(shí)戰(zhàn)結(jié)論

腳本測(cè)試,reindex 同步 100W 數(shù)據(jù),耗時(shí):34 s。
3.2 方案二:elasticdump 同步
https://github.com/elasticsearch-dump/elasticsearch-dump

3.2.1elasticdump 安裝注意事項(xiàng)
elasticdump 前置依賴是 node,node要8.0+之后的版本。
[root@VM-0-14-centos test]# node -v
v12.13.1
[root@VM-0-14-centos test]# npm -v
6.12.1
安裝成功標(biāo)志:
[root@VM-0-14-centos test]# elasticdump --help
elasticdump: Import and export tools for elasticsearch
version: 6.71.0
Usage: elasticdump --input SOURCE --output DESTINATION [OPTIONS]
... ...
3.2.2 elasticdump 同步實(shí)戰(zhàn)
elasticdump \
--input=http://172.21.0.14:19022/test_data \
--output=http://172.21.0.14:19205/test_data_from_dump \
--type=analyzer
elasticdump \
--input=http://172.21.0.14:19022/test_data \
--output=http://172.21.0.14:19205/test_data_from_dump \
--type=mapping
elasticdump \
--input=http://172.21.0.14:19022/test_data \
--output=http://172.21.0.14:19205/test_data_from_dump \
--type=data \
--concurrency=5 \
--limit=10000
基本上面的參數(shù)能做到:見(jiàn)名識(shí)意。
input :源集群索引。
output :目標(biāo)集群索引。
analyzer :同步分詞器。
mapping :同步映射schema。
data :同步數(shù)據(jù)。
concurrency :并發(fā)請(qǐng)求數(shù)。
limit:一次請(qǐng)求同步的文檔數(shù),默認(rèn)是100。
3.2.3 elasticdump 同步實(shí)戰(zhàn)驗(yàn)證結(jié)論

elasticdump 同步 100W數(shù)據(jù),耗時(shí):106 s。
3.3 方案四:ESM 工具同步
ESM 是 medcl 開(kāi)源的派生自:Elasticsearch Dumper 的工具,基于 go 語(yǔ)言開(kāi)發(fā)。
地址:https://github.com/medcl/esm
3.3.1 ESM 工具安裝注意事項(xiàng)
依賴 go 版本:>= 1.7。
3.3.2 ESM 工具同步實(shí)戰(zhàn)
esm -s http://172.21.0.14:19022 -d http://172.21.0.14:19205 -x test_data -y test_data_from_esm -w=5 -b=10 -c 10000
w:并發(fā)數(shù)。
b:bulk 大小,單位MB。
c:scroll 批量值大小。
3.3.3 ESM 工具同步實(shí)戰(zhàn)結(jié)論
100萬(wàn) 數(shù)據(jù) 38 s 同步完,速度極快。
esm -s http://172.21.0.14:19022 -d http://172.21.0.14:19205 -x test_data -y test_data_from_esm -w=5 -b=10 -c 10000
test_data
[05-19 13:44:58] [INF] [main.go:474,main] start data migration..
Scroll 1000000 / 1000000 [================================================================================================================] 100.00% 38s
Bulk 999989 / 1000000 [===================================================================================================================] 100.00% 38s
[05-19 13:45:36] [INF] [main.go:505,main] data migration finished.
同步時(shí):CPU 被打爆,說(shuō)明并發(fā)參數(shù)生效了。

3.4 方案五:logstash 同步
3.4.1 logstash 同步注意事項(xiàng)
本文基于 logstash 7.12.0,相關(guān)插件:logstash_input_elasticsearch 和 logstash_output_elasticsearch 都已經(jīng)集成安裝,無(wú)需再次安裝。
注意:配置的輸入、輸出即是插件的名字,要小寫。國(guó)外的很多博客都有錯(cuò)誤,要實(shí)戰(zhàn)一把甄別。
3.4.2 logstash 同步實(shí)戰(zhàn)
input {
elasticsearch {
hosts => ["172.21.0.14:19022"]
index => "test_data"
size => 10000
scroll => "5m"
codec => "json"
docinfo => true
}
}
filter {
}
output {
elasticsearch {
hosts => ["172.21.0.14:19205"]
index => "test_data_from_logstash"
}
}
3.4.3 logstash同步測(cè)試
100W 數(shù)據(jù) 74 s 同步完。

3.5 方案三:快照&恢復(fù)同步
3.5.1 快照&恢復(fù)配置注意事項(xiàng)
提前在 elasticsearch.yml 配置文件配置快照存儲(chǔ)路徑。
path.repo: ["/home/elasticsearch/elasticsearch-7.12.0/backup"]
詳細(xì)配置參考:干貨 | Elasitcsearch7.X集群、索引備份與恢復(fù)實(shí)戰(zhàn)。
3.5.2 快照&恢復(fù)實(shí)戰(zhàn)
# 一個(gè)節(jié)點(diǎn)創(chuàng)建快照
PUT /_snapshot/my_backup
{
"type": "fs",
"settings": {
"location": "/home/elasticsearch/elasticsearch-7.12.0/backup"
}
}
PUT /_snapshot/my_backup/snapshot_testdata_index?wait_for_completion=true
{
"indices": "test_data_from_dump",
"ignore_unavailable": true,
"include_global_state": false,
"metadata": {
"taken_by": "mingyi",
"taken_because": "backup before upgrading"
}
}
# 另外一個(gè)恢復(fù)快照
curl -XPOST "http://172.21.0.14:19022/_snapshot/my_backup/snapshot_testdata_index/_restore"
3.5.2 快照&恢復(fù)實(shí)戰(zhàn)結(jié)論
執(zhí)行快照時(shí)間:2 s。

恢復(fù)快照時(shí)間:1s 之內(nèi)。
4、小結(jié)
本文針對(duì) Elasticsearch 跨網(wǎng)絡(luò)、跨集群之間的數(shù)據(jù)同步(模擬),給出了5 種方案,并分別在實(shí)戰(zhàn)環(huán)境進(jìn)行了驗(yàn)證。

初步驗(yàn)證結(jié)論如下:

當(dāng)然,結(jié)論并非絕對(duì),僅供參考。
各同步工具本質(zhì)上都是:scroll + bulk + 多線程綜合實(shí)現(xiàn)。
本質(zhì)不同是:開(kāi)發(fā)語(yǔ)言不同、并發(fā)處理實(shí)現(xiàn)不同等。
reindex 基于 Java 語(yǔ)言開(kāi)發(fā)
esm 基于 go 語(yǔ)言開(kāi)發(fā)
logstash 基于 ruby + java 開(kāi)發(fā)
elastidump 基于 js 語(yǔ)言開(kāi)發(fā)
快照涉及異地拷貝文件,速度制約因素是網(wǎng)絡(luò)帶寬,所以沒(méi)有統(tǒng)計(jì)在內(nèi)。
如何選型?相信看了本文的介紹,應(yīng)該做到胸中有數(shù)了。
reindex 方案涉及配置白名單,快照和恢復(fù)快照涉及配置快照庫(kù)和文件的傳輸。
esm、logstash、elastidump 同步不需要特殊配置。
耗時(shí)長(zhǎng)短和集群規(guī)模、集群各個(gè)節(jié)點(diǎn)硬件配置、數(shù)據(jù)類型、寫入優(yōu)化方案等都有關(guān)系。
你實(shí)戰(zhàn)開(kāi)發(fā)中是如何同步數(shù)據(jù)的?歡迎留言討論。
推薦:
全網(wǎng)首發(fā)!《 Elasticsearch 最少必要知識(shí)教程 V1.0 》低調(diào)發(fā)布
從實(shí)戰(zhàn)中來(lái),到實(shí)戰(zhàn)中去——Elasticsearch 技能更快提升方法論
實(shí)戰(zhàn) | canal 實(shí)現(xiàn)Mysql到Elasticsearch實(shí)時(shí)增量同步
中國(guó)最大的 Elastic 非官方公眾號(hào)
點(diǎn)擊查看“閱讀原文”,和全球近 1100 位+ Elastic 愛(ài)好者(含中國(guó) 50%+ Elastic 認(rèn)證工程師)一起每日精進(jìn) ELK 技能!
