Redis 5 Cluster手工指定主从关系

2019-05-25 19:35:11 浏览数 (1)

1. 检查当前Cluster的主从关系

代码语言:javascript复制
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster check 140.210.73.39:20001

结果如下:

代码语言:javascript复制
140.210.73.39:20001 (a1addf19...) -> 929146 keys | 5461 slots | 1 slaves.
140.210.73.40:20001 (1b6d2731...) -> 929423 keys | 5461 slots | 2 slaves.
140.210.73.41:20001 (fcba0b8e...) -> 928499 keys | 5462 slots | 3 slaves.
[OK] 2787068 keys in 3 masters.
170.11 keys per slot on average.
>>> Performing Cluster Check (using node 140.210.73.39:20001)
M: a1addf195454731ccf529519085b2001c257401c 140.210.73.39:20001
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: e6ece3b812c8b8bfb9626bb73c611cde3ba2d3c5 140.210.73.39:20003
   slots: (0 slots) slave
   replicates 1b6d27312a2bf3f01d9d1fb7b3a6bece5aacc580
S: 22cbefb1c1107f5356de004365129f9599d910c4 140.210.73.41:20003
   slots: (0 slots) slave
   replicates fcba0b8ef4d1568a6f7e6c4e48b8f66bb2adca72
S: 668d6cd727a518ba0e7e94515af2e1c54841468b 140.210.73.40:20003
   slots: (0 slots) slave
   replicates fcba0b8ef4d1568a6f7e6c4e48b8f66bb2adca72
S: b5845b3ae6c45e806f4facdaa2259ab97ec3515d 140.210.73.40:20002
   slots: (0 slots) slave
   replicates a1addf195454731ccf529519085b2001c257401c
M: 1b6d27312a2bf3f01d9d1fb7b3a6bece5aacc580 140.210.73.40:20001
   slots:[5462-10922] (5461 slots) master
   2 additional replica(s)
S: 486b0c8eba757f152860e7a7e50a791899ba0d12 140.210.73.39:20002
   slots: (0 slots) slave
   replicates fcba0b8ef4d1568a6f7e6c4e48b8f66bb2adca72
S: 791cbd383de4882d5f693752a966f214420abb93 140.210.73.41:20002
   slots: (0 slots) slave
   replicates 1b6d27312a2bf3f01d9d1fb7b3a6bece5aacc580
M: fcba0b8ef4d1568a6f7e6c4e48b8f66bb2adca72 140.210.73.41:20001
   slots:[0-5461] (5462 slots) master
   3 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@vvgg-z2-redis~]#

可以看到: 140.210.73.39:20001有1个slave 140.210.73.40:20001有2个slave 140.210.73.41:20001有3个slave

2. 重新规划想要的主从关系 140.210.73.39:20001有2个slave:140.210.73.40:20002 140.210.73.41:20003 140.210.73.40:20001有2个slave:140.210.73.41:20002 140.210.73.39:20003 140.210.73.41:20001有2个slave:140.210.73.39:20002 140.210.73.40:20003

3. 删除当前的slave

代码语言:javascript复制
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster del-node 140.210.73.39:20002 '486b0c8eba757f152860e7a7e50a791899ba0d12'
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster del-node 140.210.73.39:20003 'e6ece3b812c8b8bfb9626bb73c611cde3ba2d3c5'
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster del-node 140.210.73.40:20002 'b5845b3ae6c45e806f4facdaa2259ab97ec3515d'
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster del-node 140.210.73.40:20003 '668d6cd727a518ba0e7e94515af2e1c54841468b'
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster del-node 140.210.73.41:20002 '791cbd383de4882d5f693752a966f214420abb93'
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster del-node 140.210.73.41:20003 '22cbefb1c1107f5356de004365129f9599d910c4'

4. 删除原slave的相关文件

代码语言:javascript复制
# 分别在39、40、41上执行
rm -f /data1/rediscluster1/20002/*.rdb
rm -f /data1/rediscluster1/20002/nodes_20002.conf
rm -f /data1/rediscluster1/20003/*.rdb
rm -f /data1/rediscluster1/20003/nodes_20003.conf

5. 启动slave的redis实例

代码语言:javascript复制
# 分别在39、40、41上执行
/home/redis/redis-5.0.3/src/redis-server /data1/rediscluster1/20002/redis.conf 
/home/redis/redis-5.0.3/src/redis-server /data1/rediscluster1/20003/redis.conf

6. 向集群中添加slave

代码语言:javascript复制
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster add-node 140.210.73.40:20002 140.210.73.39:20001 --cluster-slave --cluster-master-id a1addf195454731ccf529519085b2001c257401c
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster add-node 140.210.73.41:20002 140.210.73.39:20001 --cluster-slave --cluster-master-id 1b6d27312a2bf3f01d9d1fb7b3a6bece5aacc580
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster add-node 140.210.73.39:20002 140.210.73.39:20001 --cluster-slave --cluster-master-id fcba0b8ef4d1568a6f7e6c4e48b8f66bb2adca72
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster add-node 140.210.73.41:20003 140.210.73.39:20001 --cluster-slave --cluster-master-id a1addf195454731ccf529519085b2001c257401c
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster add-node 140.210.73.39:20003 140.210.73.39:20001 --cluster-slave --cluster-master-id 1b6d27312a2bf3f01d9d1fb7b3a6bece5aacc580
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster add-node 140.210.73.40:20003 140.210.73.39:20001 --cluster-slave --cluster-master-id fcba0b8ef4d1568a6f7e6c4e48b8f66bb2adca72

7. 确认当前Cluster的主从关系

代码语言:javascript复制
/home/redis/redis-5.0.3/src/redis-cli -a 123456 --cluster check 140.210.73.39:20001

结果如下:

代码语言:javascript复制
140.210.73.39:20001 (a1addf19...) -> 929146 keys | 5461 slots | 2 slaves.
140.210.73.40:20001 (1b6d2731...) -> 929423 keys | 5461 slots | 2 slaves.
140.210.73.41:20001 (fcba0b8e...) -> 928499 keys | 5462 slots | 2 slaves.
[OK] 2787068 keys in 3 masters.
170.11 keys per slot on average.
>>> Performing Cluster Check (using node 140.210.73.39:20001)
M: a1addf195454731ccf529519085b2001c257401c 140.210.73.39:20001
   slots:[10923-16383] (5461 slots) master
   2 additional replica(s)
S: 589c8b936cf5cd38dd30293721847ae28a7c459d 140.210.73.39:20003
   slots: (0 slots) slave
   replicates 1b6d27312a2bf3f01d9d1fb7b3a6bece5aacc580
M: 1b6d27312a2bf3f01d9d1fb7b3a6bece5aacc580 140.210.73.40:20001
   slots:[5462-10922] (5461 slots) master
   2 additional replica(s)
S: b088e71d8cd9523ea61b2e4c878e159ef836e9d9 140.210.73.41:20003
   slots: (0 slots) slave
   replicates a1addf195454731ccf529519085b2001c257401c
S: 9ab057a051393c4ca7dbf18fc0c8469f7a30fb44 140.210.73.39:20002
   slots: (0 slots) slave
   replicates fcba0b8ef4d1568a6f7e6c4e48b8f66bb2adca72
S: bdb9d8918c0ec6c905808c45bc9f07c8d5c65cd7 140.210.73.40:20003
   slots: (0 slots) slave
   replicates fcba0b8ef4d1568a6f7e6c4e48b8f66bb2adca72
S: 675533444bd9e1c498454e83a7cc0bc47b42fa16 140.210.73.40:20002
   slots: (0 slots) slave
   replicates a1addf195454731ccf529519085b2001c257401c
S: 42661e45fb3d27c9d640bec9f8d5f9eba277ae2f 140.210.73.41:20002
   slots: (0 slots) slave
   replicates 1b6d27312a2bf3f01d9d1fb7b3a6bece5aacc580
M: fcba0b8ef4d1568a6f7e6c4e48b8f66bb2adca72 140.210.73.41:20001
   slots:[0-5461] (5462 slots) master
   2 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@vvgg-z2-redis~]#

可以看到: 140.210.73.39:20001有2个slave,分别是140.210.73.40:20002 140.210.73.41:20003 140.210.73.40:20001有2个slave,分别是140.210.73.41:20002 140.210.73.39:20003 140.210.73.41:20001有2个slave,分别是140.210.73.39:20002 140.210.73.40:20003

从复制信息中也可得到确认:

代码语言:javascript复制
/home/redis/redis-5.0.3/src/redis-cli -a 123456 -h 140.210.73.39 -p 20001 info replication
/home/redis/redis-5.0.3/src/redis-cli -a 123456 -h 140.210.73.40 -p 20001 info replication
/home/redis/redis-5.0.3/src/redis-cli -a 123456 -h 140.210.73.41 -p 20001 info replication

0 人点赞