Redis数据在不同集群之间进行迁移,可以使用离线迁移的方式(rdb),但是降低了服务的可用性。 为了尽可能保证服务可用性,我们使用
redis-shake
实时迁移数据,并同时使用redis-full-check
完成数据对比,确保一致性。
wget https://github.com/alibaba/RedisShake/releases/download/release-v2.0.3-20200724/redis-shake-v2.0.3.tar.gz
redis-shake.conf
# 源redis的类型,支持standalone,sentinel,cluster和proxy四种模式
source.type: cluster
# 源redis地址,cluster模式下需要配置所有master节点或slave节点
source.address = 127.0.0.1:6581;127.0.0.1:6582;127.0.0.1:6583
# 目的redis的类型,支持standalone,sentinel,cluster和proxy四种模式
target.type = cluster
# 目的redis地址,cluster模式下需要配置所有master节点
target.address = 127.0.0.1:7481;127.0.0.1:7482;127.0.0.1:7483
./redis-shake.linux -conf=redis-shake.conf -type=sync
https://github.com/alibaba/RedisFullCheck
redis-full-check
通过全量对比源端和目的端的redis中的数据的方式来进行数据校验,其比较方式通过多轮次比较:每次都会抓取源和目的端的数据进行差异化比较,记录不一致的数据进入下轮对比(记录在sqlite3 db中)。然后通过多伦比较不断收敛,减少因数据增量同步导致的源库和目的库的数据不一致。最后sqlite中存在的数据就是最终的差异结果。
./redis-full-check --sourcedbtype=1 -s "127.0.0.1:6581;127.0.0.1:6582;127.0.0.1:6583" --targetdbtype=1 -t "127.0.0.1:7481;127.0.0.1:7482;127.0.0.1:7483"
[tenmao@VM_77_51_centos ~/redis-full-check-1.4.8]$ ./redis-full-check --sourcedbtype=1 -s "127.0.0.1:6581;127.0.0.1:6582;127.0.0.1:6583" --targetdbtype=1 -t "127.0.0.1:7481;127.0.0.1:7482;127.0.0.1:7483"
[INFO 2021-01-14-20:51:46 main.go:65]: init log success
[INFO 2021-01-14-20:51:46 main.go:168]: configuration: {127.0.0.1:6581;127.0.0.1:6582;127.0.0.1:6583 auth 1 -1 127.0.0.1:7481;127.0.0.1:7482;127.0.0.1:7483 auth 1 -1 result.db 3 2 unknown unknown unknown 15000 5 256 5 false 16384 20445 false}
[INFO 2021-01-14-20:51:46 main.go:170]: ---------
[INFO 2021-01-14-20:51:46 full_check.go:238]: sourceDbType=1, p.sourcePhysicalDBList=[127.0.0.1:6581 127.0.0.1:6582 127.0.0.1:6583]
[INFO 2021-01-14-20:51:46 full_check.go:241]: db=0:keys=0(inaccurate for type cluster)
[INFO 2021-01-14-20:51:46 full_check.go:253]: ---------------- start 1th tenmaoe compare
[INFO 2021-01-14-20:51:46 full_check.go:278]: start compare db 0
[INFO 2021-01-14-20:51:46 scan.go:20]: build connection[source redis addr: [127.0.0.1:6583]]
[INFO 2021-01-14-20:51:46 scan.go:20]: build connection[source redis addr: [127.0.0.1:6582]]
[INFO 2021-01-14-20:51:46 scan.go:20]: build connection[source redis addr: [127.0.0.1:6581]]
[INFO 2021-01-14-20:51:47 full_check.go:203]: stat:
tenmaoes:1, db:0, dbkeys:0, finish:-1%, finished:true
KeyScan:{11 11 0}
KeyConflictInProcess|string|value|{1 1 0}
[INFO 2021-01-14-20:51:47 full_check.go:250]: wait 5 seconds before start
[INFO 2021-01-14-20:51:52 full_check.go:253]: ---------------- start 2th tenmaoe compare
[INFO 2021-01-14-20:51:52 full_check.go:278]: start compare db 0
[INFO 2021-01-14-20:51:53 full_check.go:203]: stat:
tenmaoes:2, db:0, finished:true
KeyScan:{1 1 0}
KeyConflictInProcess|string|value|{1 1 0}
[INFO 2021-01-14-20:51:53 full_check.go:250]: wait 5 seconds before start
[INFO 2021-01-14-20:51:58 full_check.go:253]: ---------------- start 3th tenmaoe compare
[INFO 2021-01-14-20:51:58 full_check.go:278]: start compare db 0
[INFO 2021-01-14-20:51:59 full_check.go:203]: stat:
tenmaoes:3, db:0, finished:true
KeyScan:{1 1 0}
KeyConflictAtLast|string|value|{1 1 0}
[INFO 2021-01-14-20:51:59 full_check.go:328]: --------------- finished! ----------------
all finish successfully, totally 2 key(s) and 0 field(s) conflict
源redis实际上只有1个key不在目的redis,但是最后的结果是:
totally 2 key(s) and 0 field(s) conflict
(还没弄明白)
结果会保存在sqlite3 db file中,不指定的话,就是当前目录的 result.db 文件:比如有3轮比较,那么会有result.db.1
,result.db.2
,result.db.3
3个文件
[tenmao@VM_77_51_centos ~/redis-full-check-1.4.8]$ sqlite3 result.db.3
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from key;
1|tenmao|string|lack_target|0|4|0
sqlite>
redis-full-check
是扫描源redis的每一个key,然后跟目标redis对比;所以如果源redis不存在,目的redis存在,则扫描不出来,如果我们也需要的话,就需要再反向做一次对比[PANIC] target key name is busy: name8
因为目标redis已经存在该key,默认就会推出同步,可以修改配置key_exists = ignore
,其他方式有rewrite
-覆盖