Redis cluster使用slots来分配集群中的资源,因此官方提供了热迁移slots的方案,以便于迁移redis cluster节点中的信息。此方案不仅可以使用于节点迁移,也可以根据资源的不同,配置不同的slots数量。
集群原环境:
主机IP: 192.168.170.101
集群节点端口:10001-10006
集群当前主备关系:
?
迁移需求:现在有节点redis007,需要将redis002替换掉
迁移步骤:
?
由上可以看出节点信息如下:
10001M<-10004S? slots=5461
10002M<-10005S? ? ? slots=5462
10003M<-10006S? ? ? slots=5461
from rediscluster import StrictRedisCluster
redis_nodes = [
? ? ? ? {"host": "192.168.170.101", "port": "10001"},
? ? ? ? {"host": "192.168.170.101", "port": "10002"},
? ? ? ? {"host": "192.168.170.101", "port": "10003"},
? ? ? ? {"host": "192.168.170.101", "port": "10004"},
? ? ? ? {"host": "192.168.170.101", "port": "10005"},
? ? ? ? {"host": "192.168.170.101", "port": "10006"}
? ? ]
redis_conn = StrictRedisCluster(startup_nodes=redis_nodes, decode_responses=True)
for key in range(0, 100000):
? ? print key
? ? value = key
? ? key = 'zhang%s' % key
? ? redis_conn.set(key, value)
./redis-server redis007.conf
[root@lab001 redis]# redis-trib.rb add-node 192.168.170.101:10007 192.168.170.101:10001
>>> Adding node 192.168.170.101:10007 to cluster 192.168.170.101:10001
>>> Performing Cluster Check (using node 192.168.170.101:10001)
M: b30fdabd07a4bef611c160828965b91a1cdd462a 192.168.170.101:10001
? slots:0-5460 (5461 slots) master
? 1 additional replica(s)
S: 74805544792cc22f09af941fa117b2974c9c0abf 192.168.170.101:10005
? slots: (0 slots) slave
? replicates 6036af6afc6567b74ce2fecc734a2d3908d561d1
M: 9c6a98926b23585e2eeb49ae17cc77521041d3ed 192.168.170.101:10003
? slots:10923-16383 (5461 slots) master
? 1 additional replica(s)
S: 57a2e7313b952b38e32d6a1030d4812ea4decb5f 192.168.170.101:10006
? slots: (0 slots) slave
? replicates 9c6a98926b23585e2eeb49ae17cc77521041d3ed
S: 58b7b25501fdba04aef9b8cb47edef89092f10a7 192.168.170.101:10004
? slots: (0 slots) slave
? replicates b30fdabd07a4bef611c160828965b91a1cdd462a
M: 6036af6afc6567b74ce2fecc734a2d3908d561d1 192.168.170.101:10002
? slots:5461-10922 (5462 slots) master
? 1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.170.101:10007 to make it join the cluster.
[OK] New node added correctly.
[root@lab001 redis]# redis-trib.rb check 192.168.170.101:10001
>>> Performing Cluster Check (using node 192.168.170.101:10001)
M: b30fdabd07a4bef611c160828965b91a1cdd462a 192.168.170.101:10001
? slots:0-5460 (5461 slots) master
? 1 additional replica(s)
M: 7c1a0acf84b2cf08876036b07518fb8f2ea9b8f8 192.168.170.101:10007
? slots: (0 slots) master
? 0 additional replica(s)
S: 74805544792cc22f09af941fa117b2974c9c0abf 192.168.170.101:10005
? slots: (0 slots) slave
? replicates 6036af6afc6567b74ce2fecc734a2d3908d561d1
M: 9c6a98926b23585e2eeb49ae17cc77521041d3ed 192.168.170.101:10003
? slots:10923-16383 (5461 slots) master
? 1 additional replica(s)
S: 57a2e7313b952b38e32d6a1030d4812ea4decb5f 192.168.170.101:10006
? slots: (0 slots) slave
? replicates 9c6a98926b23585e2eeb49ae17cc77521041d3ed
S: 58b7b25501fdba04aef9b8cb47edef89092f10a7 192.168.170.101:10004
? slots: (0 slots) slave
? replicates b30fdabd07a4bef611c160828965b91a1cdd462a
M: 6036af6afc6567b74ce2fecc734a2d3908d561d1 192.168.170.101:10002
? slots:5461-10922 (5462 slots) master
? 1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All