一,當前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)