一:Redis集群环境准备
1:需要先安装好Redis集群环境并配置好集群
192.168.0.113 7001-7003
192.168.0.162 7004-7006
2:检查redis集群
[root@localhost src]# ./redis-cli -c -h 192.168.0.113 -a 123456 -p 7001
192.168.0.113:7001> cluster nodes
3:确保redis集群没有数据
192.168.0.113:7001> dbsize
(integer)0
192.168.0.113:7002> dbsize
(integer) 0
192.168.0.113:7003> dbsize
(integer) 0
192.168.0.113:7004> dbsize
(integer) 0
192.168.0.113:7005> dbsize
(integer) 0
192.168.0.113:7006> dbsize
(integer) 0
4:如果有数据需要删除
47.91.110.137:7001> flushdb
二:Redis单节点环境准备
1:查看Redis单节点数据大小
[root@localhost src]# ./redis-cli -c -h 192.168.0.113 -p 6379 -a 123456
192.168.0.113:6379> dbsize
(integer) 10000
2:备份好单节点redis的 aof 数据
[root@ce redis-3.2.9]# du -h --max-depth=1 appendonly.aof
三:同步数据到Redis集群
1:查看Redis集群节点卡槽
[root@localhost src]# ./redis-cli --cluster check 192.168.0.113:7001 -a 123456
主节点对应的卡槽
7001
2fd4ec03e71d6f99718621303dcdf191189df70e 192.168.0.113:7001 slots:[0-5460] (5461 slots) master
7002
ccf4f69e46aa6c3918fb0ebb53172d463eba65e0 192.168.0.113:7002 slots:[10923-16383] (5461 slots) master
7004
1840c7329348d4f42dc89fa93526f91a4e172d33 192.168.0.162:7004 slots:[5461-10922] (5462 slots) master
2:在Redis集群上把 7002 和 7004 卡槽值迁移到7001卡槽上
7002值移动到7001
[root@localhost src]# ./redis-cli --cluster reshard 192.168.0.113:7001 --cluster-from ccf4f69e46aa6c3918fb0ebb53172d463eba65e0 --cluster-to 2fd4ec03e71d6f99718621303dcdf191189df70e --cluster-slots 5461 --cluster-yes -a 123456
7004值移动到7001
[root@localhost src]# ./redis-cli --cluster reshard 192.168.0.113:7001 --cluster-from 1840c7329348d4f42dc89fa93526f91a4e172d33 --cluster-to 2fd4ec03e71d6f99718621303dcdf191189df70e --cluster-slots 5462 --cluster-yes -a 123456
3:可以看到7001卡槽拥有了全部16384个slots,7002、7004卡槽上已经没有slots了
[root@localhost src]# ./redis-cli --cluster check 192.168.0.113:7001 -a 123456
四:把Redis单节点的aof数据导入到集群7001对应的aof目录下替换集群aof源文件
1:先备份好集群7001的aof数据
[root@localhost src]# cd /usr/local/cluster/7001/db/
172.26.225.1:7001> set name diaocow
[root@localhost 7001]# cp appendonly.aof appendonly.aof.bwk
[root@localhost 7001]# cd /usr/local/redis-5.0.5/
[root@localhost redis-5.0.5]# cp appendonly.aof /usr/local/cluster/7001/db/
2:重启Redis集群7001节点
关闭
[root@localhost 7001]# ps -ef | grep redis
[root@localhost 7001]# kill -9 1371
启动
[root@localhost 7001]# ./redis-server redis.conf
查看redis集群7001节点是否正常启动
[root@localhost 7001]# ps -ef | grep redis
3:重启之后可以看到数据加载到7001卡槽上
[root@localhost src]# ./redis-cli -c -h 192.168.0.113 -p 7001 -a 123456
192.168.0.113:7001> dbsize
(integer) 1000
五:分配Redis集群7001卡槽值
1:7001卡槽值分配到7002
[root@localhost src]# ./redis-cli --cluster reshard 192.168.0.113:7001 -a 123456 --cluster-from 2fd4ec03e71d6f99718621303dcdf191189df70e --cluster-to ccf4f69e46aa6c3918fb0ebb53172d463eba65e0 --cluster-slots 5461 --cluster-yes
2:7001卡槽值分配到7004
[root@localhost src]# ./redis-cli --cluster reshard 192.168.0.113:7001 -a 123456 --cluster-from 2fd4ec03e71d6f99718621303dcdf191189df70e --cluster-to 1840c7329348d4f42dc89fa93526f91a4e172d33 --cluster-slots 5462 --cluster-yes
3:查看slots是否移动成功
[root@localhost src]# ./redis-cli -c -h 192.168.0.113 -a 123456 -p 7001
192.168.0.113:7001> cluster nodes
六:验证数据的完整
1:查看集群是否正常,slots分配是否正常(cluster_state:ok)
[root@localhost src]# ./redis-cli -c -h 192.168.0.113 -a 123456 -p 7001
192.168.0.113:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:2 cluster_current_epoch:9
cluster_my_epoch:7
cluster_stats_messages_ping_sent:7151
cluster_stats_messages_pong_sent:7087
cluster_stats_messages_update_sent:36
cluster_stats_messages_sent:14274
cluster_stats_messages_ping_received:7087
cluster_stats_messages_pong_received:7151
cluster_stats_messages_received:14238
2:验证key的数量是否正常
192.168.0.113(7001-7003)
[root@localhost src]# ./redis-cli -c -h 192.168.0.113 -a 123456 -p 7001 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. (integer) 0
[root@localhost src]# ./redis-cli -c -h 192.168.0.113 -a 123456 -p 7002 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. (integer) 6685
[root@localhost src]# ./redis-cli -c -h 192.168.0.113 -a 123456 -p 7003 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. (integer) 3315
192.168.0.162(7004-7006)
[root@localhost src]#
[root@localhost src]# ./redis-cli -c -h 192.168.0.162 -a 123456 -p 7004 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. (integer) 3315
[root@localhost src]# ./redis-cli -c -h 192.168.0.162 -a 123456 -p 7005 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. (integer) 6685
[root@localhost src]# ./redis-cli -c -h 192.168.0.162 -a 123456 -p 7006 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. (integer) 0
192.168.0.113(7001-7003)
6685+ 3315=10000 redis单卡槽key数量10000
192.168.0.162(7004-7006)
3315+6685=10000redis单卡槽key数量10000
七:注意 如果数据量很大 slots不要一次性迁移到Redis集群 ,需要一部分一部分转移