一,当前redis cluster的node情况:
我们的添加删除等操作都是以这个cluster作为demo
cluster采用六台redis,3主3从
redis1 : ip: 172.17.0.2
redis2 : ip: 172.17.0.3
redis3 : ip: 172.17.0.4
redis4 : ip: 172.17.0.5
redis5 : ip: 172.17.0.6
redis6 : ip: 172.17.0.7
说明:如何创建一个redis cluster,请参考这一篇:
https://www.cnblogs.com/architectforest/p/12714401.html
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,如果redis cluster发生单点故障,集群能否起作用?
1,节点172.17.0.4上,有3个key, 我们关掉这个节点
[root@redis3 cluster]# /usr/local/soft/redis5/bin/redis-cli -h 172.17.0.4 172.17.0.4:6379> auth lhd123 OK 172.17.0.4:6379> keys * 1) "e" 2) "d" 3) "a" 172.17.0.4:6379> get d "bb" 172.17.0.4:6379> get e "11" 172.17.0.4:6379> get a "aaaa 172.17.0.4:6379> exit [root@redis3 cluster]# systemctl stop redis
登录到其他节点后查看:
#CLUSTER NODES:用来列出cluster中的各个node
[root@redis1 conf]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 -c -h 172.17.0.2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.17.0.2:6379> CLUSTER NODES 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a 172.17.0.5:6379@16379 master - 0 1586862373000 7 connected 10923-16383 5bdaafe57b1c46f61c5910d3822633a516feb4ae 172.17.0.3:6379@16379 master - 0 1586862373000 2 connected 5461-10922 e024e898a21f3f4051abfb0957046dc4a81ef947 172.17.0.4:6379@16379 master,fail - 1586862344468 1586862343862 3 disconnected eb701616b26b5a350605ae3ea5f80e4fc79d84c3 172.17.0.2:6379@16379 myself,master - 0 1586862371000 1 connected 0-5460 9cd94c491211542dbfae96002489c9b63a5a54e7 172.17.0.6:6379@16379 slave eb701616b26b5a350605ae3ea5f80e4fc79d84c3 0 1586862373104 5 connected 6f90338cef5af2aa9f0580cd660c80d2ea5fab82 172.17.0.7:6379@16379 slave 5bdaafe57b1c46f61c5910d3822633a516feb4ae 0 1586862374115 6 connected
可以看到这个节点:172.17.0.4fail了
同时:172.17.0.5这个以前的slave角色变成了master
查询数据:数据的读取变成了172.17.0.5
172.17.0.2:6379> get e -> Redirected to slot [15363] located at 172.17.0.5:6379 "11" 172.17.0.5:6379> get a "aaaa" 172.17.0.5:6379> get d "bb"
以上说明当有节点宕掉时,cluster起到了高可用的作用
2,我们把原来的172.17.0.4上的redis服务再次启动:
[root@redis3 cluster]# systemctl start redis 172.17.0.5:6379> CLUSTER NODES eb701616b26b5a350605ae3ea5f80e4fc79d84c3 172.17.0.2:6379@16379 master - 0 1586862654195 1 connected 0-5460 5bdaafe57b1c46f61c5910d3822633a516feb4ae 172.17.0.3:6379@16379 master - 0 1586862655199 2 connected 5461-10922 e024e898a21f3f4051abfb0957046dc4a81ef947 172.17.0.4:6379@16379 slave 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a 0 1586862656000 7 connected 6f90338cef5af2aa9f0580cd660c80d2ea5fab82 172.17.0.7:6379@16379 slave 5bdaafe57b1c46f61c5910d3822633a516feb4ae 0 1586862656203 6 connected 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a 172.17.0.5:6379@16379 myself,master - 0 1586862654000 7 connected 10923-16383 9cd94c491211542dbfae96002489c9b63a5a54e7 172.17.0.6:6379@16379 slave eb701616b26b5a350605ae3ea5f80e4fc79d84c3 0 1586862653190 5 connected
172.17.0.4不再是fail,但角色变成了slave
三,如何在cluster中新添加一个node?
1,新添加一台机器,上面安装redis
此台机器的ip: 172.17.0.8,
安装过程参考这一篇:
https://www.cnblogs.com/architectforest/p/12714401.html
2,从集群中一个实例的redis-cli添加新实例(ip:172.17.0.8)到cluster
说明:使用 cluster meet ip port
#CLUSTER meet :添加一个node到cluster
172.17.0.2:6379> CLUSTER meet 172.17.0.8 6379 OK 172.17.0.2:6379> cluster nodes 6f90338cef5af2aa9f0580cd660c80d2ea5fab82 172.17.0.7:6379@16379 slave 5bdaafe57b1c46f61c5910d3822633a516feb4ae 0 1586920922000 6 connected 9cd94c491211542dbfae96002489c9b63a5a54e7 172.17.0.6:6379@16379 slave eb701616b26b5a350605ae3ea5f80e4fc79d84c3 0 1586920922000 5 connected 5bdaafe57b1c46f61c5910d3822633a516feb4ae 172.17.0.3:6379@16379 master - 0 1586920922740 2 connected 5461-10922 c9f3606e65e6d05349949aeb7ab71c5e1a9b0457 172.17.0.8:6379@16379 master - 0 1586920923000 0 connected 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a 172.17.0.5:6379@16379 master - 0 1586920923746 7 connected 10923-16383 eb701616b26b5a350605ae3ea5f80e4fc79d84c3 172.17.0.2:6379@16379 myself,master - 0 1586920921000 1 connected 0-5460 e024e898a21f3f4051abfb0957046dc4a81ef947 172.17.0.4:6379@16379 slave 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a 0 1586920921000 7 connected
可以看到:172.17.0.8已添加到了cluster中,角色是master
3,也可从命令行进行添加,效果一样
例:
#--cluster add-node :给cluster中添加一个node,注意
# 172.17.0.8:6379是要添加的node,
# 172.17.0.2:6379是用来获取cluster信息的node
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster add-node 172.17.0.8:6379 172.17.0.2:6379
四,添加node到cluster时指定主从
1,查看现有节点:
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster check 172.17.0.2:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.17.0.2:6379 (eb701616...) -> 2 keys | 5461 slots | 1 slaves. 172.17.0.3:6379 (5bdaafe5...) -> 2 keys | 5462 slots | 1 slaves. 172.17.0.5:6379 (1ca00d6a...) -> 3 keys | 5461 slots | 1 slaves. [OK] 7 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 172.17.0.2:6379) M: eb701616b26b5a350605ae3ea5f80e4fc79d84c3 172.17.0.2:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 6f90338cef5af2aa9f0580cd660c80d2ea5fab82 172.17.0.7:6379 slots: (0 slots) slave replicates 5bdaafe57b1c46f61c5910d3822633a516feb4ae S: 9cd94c491211542dbfae96002489c9b63a5a54e7 172.17.0.6:6379 slots: (0 slots) slave replicates eb701616b26b5a350605ae3ea5f80e4fc79d84c3 M: 5bdaafe57b1c46f61c5910d3822633a516feb4ae 172.17.0.3:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a 172.17.0.5:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: e024e898a21f3f4051abfb0957046dc4a81ef947 172.17.0.4:6379 slots: (0 slots) slave replicates 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a
2,添加节点:172.17.0.8
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster add-node 172.17.0.8:6379 172.17.0.2:6379
3,检查节点:172.17.0.8已添加成功,但没有分配slot
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster check 172.17.0.2:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.17.0.2:6379 (eb701616...) -> 2 keys | 5461 slots | 1 slaves. 172.17.0.8:6379 (d1cfb890...) -> 0 keys | 0 slots | 0 slaves. 172.17.0.3:6379 (5bdaafe5...) -> 2 keys | 5462 slots | 1 slaves. 172.17.0.5:6379 (1ca00d6a...) -> 3 keys | 5461 slots | 1 slaves. [OK] 7 keys in 4 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 172.17.0.2:6379) M: eb701616b26b5a350605ae3ea5f80e4fc79d84c3 172.17.0.2:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: d1cfb89071861866baa7ba53f878905f15616445 172.17.0.8:6379 slots: (0 slots) master
4,再添加节点:172.17.0.9,并指定它为172.17.0.8的slave
# add-node: 后面的分别跟着新加入的slave和slave对应的master
# cluster-slave:表示加入的是slave节点
# --cluster-master-id:表示slave对应的master的node ID
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster add-node 172.17.0.9:6379 172.17.0.2:6379
--cluster-slave --cluster-master-id d1cfb89071861866baa7ba53f878905f15616445
5,再次check,已添加成功,且是作为172.17.0.8的slave
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster check 172.17.0.2:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.17.0.2:6379 (eb701616...) -> 2 keys | 5461 slots | 1 slaves. 172.17.0.8:6379 (d1cfb890...) -> 0 keys | 0 slots | 1 slaves. 172.17.0.3:6379 (5bdaafe5...) -> 2 keys | 5462 slots | 1 slaves. 172.17.0.5:6379 (1ca00d6a...) -> 3 keys | 5461 slots | 1 slaves. [OK] 7 keys in 4 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 172.17.0.2:6379) M: eb701616b26b5a350605ae3ea5f80e4fc79d84c3 172.17.0.2:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: d1cfb89071861866baa7ba53f878905f15616445 172.17.0.8:6379 slots: (0 slots) master 1 additional replica(s) S: 9cd94c491211542dbfae96002489c9b63a5a54e7 172.17.0.6:6379 slots: (0 slots) slave replicates eb701616b26b5a350605ae3ea5f80e4fc79d84c3 M: 5bdaafe57b1c46f61c5910d3822633a516feb4ae 172.17.0.3:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: e024e898a21f3f4051abfb0957046dc4a81ef947 172.17.0.4:6379 slots: (0 slots) slave replicates 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a S: ec780fe8b66f90db670949046d4c8b9fc0478c40 172.17.0.9:6379 slots: (0 slots) slave replicates d1cfb89071861866baa7ba53f878905f15616445 S: 6f90338cef5af2aa9f0580cd660c80d2ea5fab82 172.17.0.7:6379 slots: (0 slots) slave replicates 5bdaafe57b1c46f61c5910d3822633a516feb4ae M: 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a 172.17.0.5:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s)
五,新添加node到cluster后,重新分配slot到各个node
1,查看cluster的slot分配
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster info 172.17.0.2:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.17.0.2:6379 (eb701616...) -> 2 keys | 5461 slots | 1 slaves. 172.17.0.8:6379 (d1cfb890...) -> 0 keys | 0 slots | 1 slaves. 172.17.0.3:6379 (5bdaafe5...) -> 2 keys | 5462 slots | 1 slaves. 172.17.0.5:6379 (1ca00d6a...) -> 3 keys | 5461 slots | 1 slaves. [OK] 7 keys in 4 masters. 0.00 keys per slot on average.
2,迁移一部分slot到172.17.0.8
#--cluster reshard :重新分配slot
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster reshard 172.17.0.2:6379 How many slots do you want to move (from 1 to 16384)? 3000
要回答系统的提问:
What is the receiving node ID? d1cfb89071861866baa7ba53f878905f15616445 Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1: 5bdaafe57b1c46f61c5910d3822633a516feb4ae Source node #2: done
说明:5bdaafe57b1c46f61c5910d3822633a516feb4ae是172.17.0.3的node id
d1cfb89071861866baa7ba53f878905f15616445 是172.17.0.8的node id
3000是从172.17.0.3迁移的slot数量
3,检查slot数量:
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster info 172.17.0.2:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.17.0.2:6379 (eb701616...) -> 2 keys | 4461 slots | 1 slaves. 172.17.0.8:6379 (d1cfb890...) -> 1 keys | 3000 slots | 1 slaves. 172.17.0.3:6379 (5bdaafe5...) -> 2 keys | 4461 slots | 1 slaves. 172.17.0.5:6379 (1ca00d6a...) -> 2 keys | 4462 slots | 1 slaves. [OK] 7 keys in 4 masters. 0.00 keys per slot on average.
4,平衡slot数量
#--cluster rebalance: 均衡cluster中各node上分配到的slot数量
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster rebalance --cluster-threshold 1 172.17.0.2:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing Cluster Check (using node 172.17.0.2:6379) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Rebalancing across 4 nodes. Total weight = 4.00 Moving 366 slots from 172.17.0.5:6379 to 172.17.0.8:6379 ##########################################################################################################################
##########################################################################################################################
########################################################################################################################## Moving 366 slots from 172.17.0.2:6379 to 172.17.0.8:6379 ##########################################################################################################################
##########################################################################################################################
########################################################################################################################## Moving 365 slots from 172.17.0.3:6379 to 172.17.0.8:6379 ##########################################################################################################################
##########################################################################################################################
#########################################################################################################################
5,再次检查slot数量
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster info 172.17.0.2:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.17.0.2:6379 (eb701616...) -> 2 keys | 4096 slots | 1 slaves. 172.17.0.8:6379 (d1cfb890...) -> 1 keys | 4096 slots | 1 slaves. 172.17.0.3:6379 (5bdaafe5...) -> 2 keys | 4096 slots | 1 slaves. 172.17.0.5:6379 (1ca00d6a...) -> 2 keys | 4096 slots | 1 slaves.
说明:--cluster rebalance起到了平均分配slot到各node的作用
六,如何从cluster中删除掉一个不包含slot的实例?
1,我们在172.17.0.8上查看cluster节点,
当前节点在cluster当中
172.17.0.8:6379> CLUSTER NODES 6f90338cef5af2aa9f0580cd660c80d2ea5fab82 172.17.0.7:6379@16379 slave 5bdaafe57b1c46f61c5910d3822633a516feb4ae 0 1586921268826 2 connected 5bdaafe57b1c46f61c5910d3822633a516feb4ae 172.17.0.3:6379@16379 master - 0 1586921266000 2 connected 5461-10922 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a 172.17.0.5:6379@16379 master - 0 1586921267816 7 connected 10923-16383 9cd94c491211542dbfae96002489c9b63a5a54e7 172.17.0.6:6379@16379 slave eb701616b26b5a350605ae3ea5f80e4fc79d84c3 0 1586921265798 1 connected e024e898a21f3f4051abfb0957046dc4a81ef947 172.17.0.4:6379@16379 slave 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a 0 1586921266810 7 connected c9f3606e65e6d05349949aeb7ab71c5e1a9b0457 172.17.0.8:6379@16379 myself,master - 0 1586921265000 0 connected eb701616b26b5a350605ae3ea5f80e4fc79d84c3 172.17.0.2:6379@16379 master - 0 1586921267000 1 connected 0-5460
从集群中的另一台机器上删除172.17.0.8这个节点:
#--cluster del-node 从cluster中删除指定的node id
#c9f3606e65e6d05349949aeb7ab71c5e1a9b0457 是172.17.0.8的node id
[root@redis4 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster del-node 172.17.0.5:6379 c9f3606e65e6d05349949aeb7ab71c5e1a9b0457 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Removing node c9f3606e65e6d05349949aeb7ab71c5e1a9b0457 from cluster 172.17.0.5:6379 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node.
成功了
注意172.17.0.8这个节点的服务已被关闭了
我们登录到该机器查看:
[root@redis7 ~]# systemctl status redis ● redis.service - Redis Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Wed 2020-04-15 03:50:33 UTC; 2min 9s ago Process: 242 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=1/FAILURE) Process: 234 ExecStart=/usr/local/soft/redis5/bin/redis-server /usr/local/soft/redis5/conf/redis.conf (code=exited, status=0/SUCCESS) Main PID: 235 (code=exited, status=0/SUCCESS)
说明:此时登录到cluster中现存的各个节点,已经看不到172.17.0.8这个节点
2,作为被删除的节点,
172.17.0.8上面仍然保存有cluster的各个节点信息,
可以把它的nodes.conf删除后重启一次,
例子:
[root@redis7 ~]# systemctl stop redis [root@redis7 ~]# rm /data/redis/cluster/nodes-6379.conf rm: remove regular file '/data/redis/cluster/nodes-6379.conf'? y [root@redis7 ~]# systemctl start redis
七,删除一个包含slot的节点
1,查看slot数量
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster info 172.17.0.2:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.17.0.2:6379 (eb701616...) -> 2 keys | 4096 slots | 1 slaves. 172.17.0.8:6379 (d1cfb890...) -> 1 keys | 4096 slots | 0 slaves. 172.17.0.3:6379 (5bdaafe5...) -> 2 keys | 4096 slots | 1 slaves. 172.17.0.5:6379 (1ca00d6a...) -> 2 keys | 4096 slots | 1 slaves. [OK] 7 keys in 4 masters. 0.00 keys per slot on average.
可以看到172.17.0.8这个node包含有4096 slots
2,我们来删除172.17.0.8:
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster del-node 172.17.0.2:6379 d1cfb89071861866baa7ba53f878905f15616445 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Removing node d1cfb89071861866baa7ba53f878905f15616445 from cluster 172.17.0.2:6379 [ERR] Node 172.17.0.8:6379 is not empty! Reshard data away and try again.
删除失败,提示node非空,需要reshard数据
3,迁移slot
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster reshard 172.17.0.2:6379 ... How many slots do you want to move (from 1 to 16384)? 4096 What is the receiving node ID? 5bdaafe57b1c46f61c5910d3822633a516feb4ae Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1: d1cfb89071861866baa7ba53f878905f15616445 Source node #2: done
说明:5bdaafe57b1c46f61c5910d3822633a516feb4ae是172.17.0.3的node id
d1cfb89071861866baa7ba53f878905f15616445 是172.17.0.8的node id
4096是172.17.0.8的slot数量
4,再次检查slot数量
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster info 172.17.0.2:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.17.0.2:6379 (eb701616...) -> 2 keys | 4096 slots | 1 slaves. 172.17.0.8:6379 (d1cfb890...) -> 0 keys | 0 slots | 0 slaves. 172.17.0.3:6379 (5bdaafe5...) -> 3 keys | 8192 slots | 1 slaves. 172.17.0.5:6379 (1ca00d6a...) -> 2 keys | 4096 slots | 1 slaves.
说明:172.17.0.8上的slot已迁移完成了,slot数量变成了0,
172.17.0.3上slot数量变成了8192
说明迁移成功了
5,重新删除
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster del-node 172.17.0.2:6379 d1cfb89071861866baa7ba53f878905f15616445 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Removing node d1cfb89071861866baa7ba53f878905f15616445 from cluster 172.17.0.2:6379 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node.
说明:删除成功了
八,redis cluster中改变一个从节点的master id
1,查看节点的情况
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 --cluster check 172.17.0.2:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.17.0.2:6379 (eb701616...) -> 2 keys | 4096 slots | 2 slaves. 172.17.0.3:6379 (5bdaafe5...) -> 3 keys | 8192 slots | 1 slaves. 172.17.0.5:6379 (1ca00d6a...) -> 2 keys | 4096 slots | 1 slaves. 172.17.0.8:6379 (ac3a634d...) -> 0 keys | 0 slots | 0 slaves. [OK] 7 keys in 4 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 172.17.0.2:6379) M: eb701616b26b5a350605ae3ea5f80e4fc79d84c3 172.17.0.2:6379 slots:[1365-5460] (4096 slots) master 2 additional replica(s) S: 9cd94c491211542dbfae96002489c9b63a5a54e7 172.17.0.6:6379 slots: (0 slots) slave replicates eb701616b26b5a350605ae3ea5f80e4fc79d84c3 M: 5bdaafe57b1c46f61c5910d3822633a516feb4ae 172.17.0.3:6379 slots:[0-1364],[5461-12287] (8192 slots) master 1 additional replica(s) S: e024e898a21f3f4051abfb0957046dc4a81ef947 172.17.0.4:6379 slots: (0 slots) slave replicates 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a S: 6f90338cef5af2aa9f0580cd660c80d2ea5fab82 172.17.0.7:6379 slots: (0 slots) slave replicates 5bdaafe57b1c46f61c5910d3822633a516feb4ae M: 1ca00d6a680fc1b0d617a46996eaaefc3636fd5a 172.17.0.5:6379 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: ac3a634decfbe7f29fd90b2181d794ac66f5a4c5 172.17.0.8:6379 slots: (0 slots) master S: 6aa9b9f8166345c0039f1249ab4f06bad2a7b534 172.17.0.9:6379 slots: (0 slots) slave replicates eb701616b26b5a350605ae3ea5f80e4fc79d84c3
说明:
172.17.0.2有两个slave,包括:172.17.0.6 和 172.17.0.9
而172.17.0.8有0个slave,
我们手动把172.17.0.9改成为172.17.0.8的slave
2,指定当前node的master,我们登录到172.17.0.9上面进行操作
# CLUSTER REPLICATE 使当前node作为指定node的slave node
# CLUSTER REPLICAS 查询指定的node有哪些从node?
[root@redis8 redis]# /usr/local/soft/redis5/bin/redis-cli -a lhd123 -c -h 172.17.0.9 172.17.0.9:6379> CLUSTER REPLICATE ac3a634decfbe7f29fd90b2181d794ac66f5a4c5 OK 172.17.0.9:6379> CLUSTER REPLICAS ac3a634decfbe7f29fd90b2181d794ac66f5a4c5 1) "6aa9b9f8166345c0039f1249ab4f06bad2a7b534 172.17.0.9:6379@16379 myself,slave ac3a634decfbe7f29fd90b2181d794ac66f5a4c5 0 1586933584000 0 connected"
可以看到操作成功了
九,报错的解决:添加node时遇到报错:
[ERR] Node 172.17.0.9:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
说明包含有以前的数据,清除后重启redis服务
[root@redis8 redis]# systemctl stop redis [root@redis8 redis]# rm cluster/nodes-6379.conf rm: remove regular file 'cluster/nodes-6379.conf'? y [root@redis8 redis]# rm data/dump.rdb rm: remove regular file 'data/dump.rdb'? y [root@redis8 redis]# systemctl start redis
重新添加后,会成功
十,附:cluster的管理命令:
cluster info 集群信息 cluster nodes 所有节点和slot分布 cluster slots 所有节点和slot分布 cluster slaves <node_id> 返回一个master节点的slaves 列表 cluster meet <ip> <port> 添加指定的节点到集群,默认成为maser,相当于redis-trib.rb add-node cluster forget <node-id> 删除指定的节点,相当于redis-trib.rb del-node cluster replicate <node-id> 将当前节点设置为指定node-id的slave; cluster saveconfig 将节点信息保存在nodes-6379.conf文化中; cluster addslots <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。 cluster delslots <slot> [slot ...] 移除一个或多个槽对当前节点点。 cluster flushslots 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。 cluster setslot <slot> node <node_id> 将槽 slot 指派给 node_id 指定的节点。 cluster setslot <slot> migrating <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。 cluster setslot <slot> importing <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。 cluster setslot <slot> stable 取消对槽 slot 的导入(import)或者迁移(migrate)。 cluster keyslot <key> 获得key对应的槽 cluster countkeysinslot<slot> 返回slot目前包含的key数量。 cluster getkeysinslot <slot> <count> 返回 count个slot 槽中的键
十一,查看redis的版本
[root@redis1 /]# /usr/local/soft/redis5/bin/redis-server --version Redis server v=5.0.7 sha=00000000:0 malloc=libc bits=64 build=c52ab39fadfc446c
十二,查看centos的版本:
[root@redis1 /]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core)