1. 首页
  2. 数据库

基于Redis 5的redis cluster部署

 

Redis cluster工作原理:
在哨兵sentinel机制中,可解决redis高可用问题,保证redis服务的正常使用,但是无法解决redis单机写入的瓶颈问题,即单机redis写入性能受限于单机的内存大小、并发数量、网卡速率等因素;
 
Redis cluster特点:
  • 所有redis节点使用PING机制互联;
  • 集群中某个节点是否失效,是由整个集群中超过半数的节点检测都失效,才能算真正的失效
  • 客户端不需要proxy即可直接连接redis,应用程序中需要配置有全部的redis服务器IP
  • Redis cluster把所有的redis node平均映射到0-16383个槽位slot上,读写需要到指定的redis node上进行操作,因此有多少个redis node相当于redis并发扩展了多少倍,每个redis node承担16383/N个槽位。
  • Redis cluster预先分配16384个槽位,当需要在redis集群中写入一个key-value的时候,会使用CRC16(key)mod 16384之后的值,决定将key写入哪一个槽位从而决定写入哪一个redis节点上,从而有效解决单机瓶颈。
 
Redis cluster基本架构:
假如三个主节点A B C,采用哈希槽(hash slot)的方式来分配16384个slot,那么三个节点分别承担的slot区间可以是:
节点A覆盖 0—5460
节点B覆盖 5461—10922
节点C覆盖 10923—16383
 
Redis cluster的架构虽然解决了并发的问题,但是每个redis master的高可用如果解决?
那就是对每个master主节点都实现主从复制,从而实现redis高可用性。
 
例子:6台服务器,分别是三组master/slave,适用于生产环境。
基于redis5 的redis cluster部署步骤如下:
官方文档  https://redis.io/topics/cluster-tutorial
redis cluster相关命令,执行该命令查看:redis-cli –cluster help
环境:centos8  关闭firewalld selinux
192.168.112.100 redis-node1
192.168.112.101 redis-node2
192.168.112.102 redis-node3
192.168.112.103 redis-node4
192.168.112.104 redis-node5
192.168.112.105 redis-node6
 
#每个redis节点采用相同的硬件配置、相同的密码,相同的redis版本
#所有redis服务器必须没有任何数据
1、安装redis,启用redis cluster配置
dnf -y install redis #所有6台主机都执行安装命令  
vim /etc/redis.conf #每个节点修改redis配置,必须开启cluster功能的参数,配置如下:
bind 0.0.0.0
masterauth 123456 #建议配置,为后期master和slave主从复制做准备
requirepass 123456
cluster-enabled yes #取消注释,必须开启集群,开启后redis进程会有cluster显示
cluster-config-file nodes-6379.conf #取消注释,此为集群状态文件,记录主从关系及slot范围消息,由redis cluster集群自动创建和维护。
cluster-require-full-coverage no #默认为yes,设为no可以防止一个节点不可用导致整个cluster不可用。
 
#或者执行以下命令,批量修改
sed -i -e ‘s/bind 127.0.0.1/bind 0.0.0.0/’ -e ‘s/# masterauth .*/masterauth 123456/’ -e ‘s/# requirepass .*/requirepass 123456/’ -e ‘s/# cluster-enabled yes/cluster-enabled yes/’ -e ‘s/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-6379.conf/’ -e ‘s/# cluster-require-full-coverage yes/cluster-require-full-coverage no/’ -e ‘s/daemonize no/daemonize yes/’ /etc/redis.conf
 
systemctl enable –now redis #所有几点执行启动redis
ss -tnl #开启了16379的cluster的端口,实际的端口是redis的port + 10000   #可以看到6379和16379端口
ps -ef | grep redis #查看redis进程,有[cluster]状态
redis       2374       1  0 10:29 ?        00:00:00 /usr/bin/redis-server 0.0.0.0:6379 [cluster]

 

2、创建集群
# redis-cli –cluster-replicas 1  #表示每个master对应一个slave节点
[root@redis-node1 ~]# redis-cli -a 123456 –cluster create 192.168.112.100:6379  192.168.112.101:6379 192.168.112.102:6379 192.168.112.103:6379192.168.112.104:6379 192.168.112.105:6379  –cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes…
Master[0] -> Slots 0 – 5460
Master[1] -> Slots 5461 – 10922
Master[2] -> Slots 10923 – 16383
Adding replica 192.168.112.103:6379 to 192.168.112.100:6379
Adding replica 192.168.112.104:6379 to 192.168.112.101:6379
Adding replica 192.168.112.105:6379 to 192.168.112.102:6379
M: d5a1b8871c930a7298c2e230e1f8f13e0a459080 192.168.112.100:6379
   slots:[0-5460] (5461 slots) master
M: d998b2e51461ac1b829cb45549fa6752a6036338 192.168.112.101:6379
   slots:[5461-10922] (5462 slots) master
M: ae52629045b3dd1ea67a2f5dfaff72ea6279720c 192.168.112.102:6379
   slots:[10923-16383] (5461 slots) master
S: 43e1436b14c1298b1bb37e382b4970a8b7528c3c 192.168.112.103:6379
   replicates d5a1b8871c930a7298c2e230e1f8f13e0a459080
S: 98c893e6fa44d3a434e57466f1cd4ea4ddcbf987 192.168.112.104:6379
   replicates d998b2e51461ac1b829cb45549fa6752a6036338
S: d21ad3986daf2907bf7e1c394fbe392842c8bcba 192.168.112.105:6379
   replicates ae52629045b3dd1ea67a2f5dfaff72ea6279720c
Can I set the above configuration? (type ‘yes’ to accept): yes                 #输入yes,自动创建集群
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
….
>>> Performing Cluster Check (using node 192.168.112.100:6379)
M: d5a1b8871c930a7298c2e230e1f8f13e0a459080 192.168.112.100:6379
   slots:[0-5460] (5461 slots) master                      #已经分配的槽位
   1 additional replica(s) #分配了一个slave
S: d21ad3986daf2907bf7e1c394fbe392842c8bcba 192.168.112.105:6379
   slots: (0 slots) slave                                 #slave没有分配槽位
   replicates ae52629045b3dd1ea67a2f5dfaff72ea6279720c
M: d998b2e51461ac1b829cb45549fa6752a6036338 192.168.112.101:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: ae52629045b3dd1ea67a2f5dfaff72ea6279720c 192.168.112.102:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 43e1436b14c1298b1bb37e382b4970a8b7528c3c 192.168.112.103:6379
   slots: (0 slots) slave
   replicates d5a1b8871c930a7298c2e230e1f8f13e0a459080
S: 98c893e6fa44d3a434e57466f1cd4ea4ddcbf987 192.168.112.104:6379
   slots: (0 slots) slave
   replicates d998b2e51461ac1b829cb45549fa6752a6036338
[OK] All nodes agree about slots configuration.      #所有节点槽位分配完成
>>> Check for open slots…    #检查打开的槽位
>>> Check slots coverage… #检查插槽覆盖范围
[OK] All 16384 slots covered. #所有槽位16384个分配完成
 
#观察以上结果,可以看到3组master/slave
master:192.168.21.100  <—>  slave:192.168.112.103
master:192.168.21.101  <—>  slave:192.168.112.104
master:192.168.21.102  <—>  slave:192.168.112.105
 
3、查看主从状态
[root@redis-node1 ~]# redis-cli -a 123456 -c info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.112.103,port=6379,state=online,offset=994,lag=1
 
[root@redis-node2 ~]# redis-cli -a 123456 -c info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.112.104,port=6379,state=online,offset=966,lag=1
 
[root@redis-node3 ~]# redis-cli -a 123456 -c info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.112.105,port=6379,state=online,offset=1162,lag=0
#查看指定master节点的slave节点信息
[root@redis-node1 ~]# redis-cli -a 123456 cluster nodes
d21ad3986daf2907bf7e1c394fbe392842c8bcba 192.168.112.105:6379@16379 slave ae52629045b3dd1ea67a2f5dfaff72ea6279720c 0 1618241015932 6 connected
d998b2e51461ac1b829cb45549fa6752a6036338 192.168.112.101:6379@16379 master – 0 1618241014000 2 connected 5461-10922
ae52629045b3dd1ea67a2f5dfaff72ea6279720c 192.168.112.102:6379@16379 master – 0 1618241016955 3 connected 10923-16383
d5a1b8871c930a7298c2e230e1f8f13e0a459080 192.168.112.100:6379@16379 myself,master – 0 1618241015000 1 connected 0-5460
43e1436b14c1298b1bb37e382b4970a8b7528c3c 192.168.112.103:6379@16379 slave d5a1b8871c930a7298c2e230e1f8f13e0a459080 0 1618241015000 4 connected
98c893e6fa44d3a434e57466f1cd4ea4ddcbf987 192.168.112.104:6379@16379 slave d998b2e51461ac1b829cb45549fa6752a6036338 0 1618241013892 5 connected
 
#以下命令查看指定master节点的slave节点信息,d5a1b8871c930a7298c2e230e1f8f13e0a459080为master节点ID。
[root@redis-node1 ~]# redis-cli -a 123456  cluster slaves d5a1b8871c930a7298c2e230e1f8f13e0a459080
1) “43e1436b14c1298b1bb37e382b4970a8b7528c3c 192.168.112.103:6379@16379 slave d5a1b8871c930a7298c2e230e1f8f13e0a459080 0 1618241585154 4 connected”
 
4、验证集群状态
[root@redis-node4 ~]# redis-cli -a 123456 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:3 #3个集群
 
#查看任意节点的集群状态
[root@redis-node1 ~]# redis-cli -a 123456 –cluster info 192.168.112.104:6379
192.168.112.100:6379 (d5a1b887…) -> 0 keys | 5461 slots | 1 slaves.
192.168.112.102:6379 (ae526290…) -> 0 keys | 5461 slots | 1 slaves.
192.168.112.101:6379 (d998b2e5…) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
 
#查看集群node对应关系
redis-cli -a 123456 cluster nodes
redis-cli -a 123456 –cluster check 192.168.112.105:6379
[root@redis-node1 ~]# redis-cli -a 123456 –cluster check 192.168.112.105:6379
Warning: Using a password with ‘-a’ or ‘-u’ option on the command line interface may not be safe.
192.168.112.101:6379 (d998b2e5…) -> 0 keys | 5462 slots | 1 slaves.
192.168.112.102:6379 (ae526290…) -> 0 keys | 5461 slots | 1 slaves.
192.168.112.100:6379 (d5a1b887…) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.112.105:6379)
S: d21ad3986daf2907bf7e1c394fbe392842c8bcba 192.168.112.105:6379
   slots: (0 slots) slave
   replicates ae52629045b3dd1ea67a2f5dfaff72ea6279720c
M: d998b2e51461ac1b829cb45549fa6752a6036338 192.168.112.101:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 98c893e6fa44d3a434e57466f1cd4ea4ddcbf987 192.168.112.104:6379
   slots: (0 slots) slave
   replicates d998b2e51461ac1b829cb45549fa6752a6036338
M: ae52629045b3dd1ea67a2f5dfaff72ea6279720c 192.168.112.102:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: d5a1b8871c930a7298c2e230e1f8f13e0a459080 192.168.112.100:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 43e1436b14c1298b1bb37e382b4970a8b7528c3c 192.168.112.103:6379
   slots: (0 slots) slave
   replicates d5a1b8871c930a7298c2e230e1f8f13e0a459080
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
 
5、验证集群写入key
[root@redis-node1 ~]# redis-cli -a 123456 -h 192.168.112.105 set key1 value1
(error) MOVED 9189 192.168.112.101:6379    
#经过算法计算,当前key的槽位需要写入指定的node,槽位不在当前node所以无法写入
 
[root@redis-node1 ~]# redis-cli -a 123456 -h 192.168.112.101 set key1 value1
OK                              #指定node可写入
 
[root@redis-node2 ~]# redis-cli -a 123456 get key1
“value1”      #对应的master节点可以keys *,可以get key1,但是slave节点只能keys *,不能get key1
 
#redis cluster计算key所属的slot
redis-cli -h 192.168.112.101 -a 123456 –no-auth-warning cluster nodes   #查看各个主节点slot范围
 
[root@redis-node1 ~]# redis-cli -h 192.168.112.100 -a 123456 –no-auth-warning cluster keyslot hello
(integer) 866
[root@redis-node1 ~]# redis-cli -h 192.168.112.100 -a 123456 –no-auth-warning set hello wzstyle.cn
OK
 
[root@redis-node1 ~]# redis-cli -h 192.168.112.100 -a 123456 –no-auth-warning cluster keyslot name
(integer) 5798                     #查看slot槽位是5798,属于master 101主机的槽位
[root@redis-node1 ~]# redis-cli -h 192.168.112.100 -a 123456 –no-auth-warning set name wang
(error) MOVED 5798 192.168.112.101:6379     #确实显示要移动到101上
[root@redis-node1 ~]# redis-cli -h 192.168.112.101 -a 123456 –no-auth-warning set name wang
OK                                      #指定在101上可以set key
 
6、模拟master故障,与之对应的slave节点自动提升为新master
#停掉master100这个主节点,验证它的从节点104是否能自动提升新主
[root@redis-node1 ~]# redis-cli -a 123456
127.0.0.1:6379> shutdown
not connected> exit
[root@redis-node1 ~]# ss -tnl
State        Recv-Q       Send-Q              Local Address:Port               Peer Address:Port
LISTEN       0            128                       0.0.0.0:22                      0.0.0.0:*
LISTEN       0            128                          [::]:22                         [::]:*
[root@redis-node1 ~]# redis-cli -a 123456 –cluster check 192.168.112.101:6379
Could not connect to Redis at 192.168.112.100:6379: Connection refused
192.168.112.101:6379 (d998b2e5…) -> 2 keys | 5462 slots | 1 slaves.
192.168.112.102:6379 (ae526290…) -> 1 keys | 5461 slots | 1 slaves.
192.168.112.103:6379 (43e1436b…) -> 1 keys | 5461 slots | 0 slaves.     #103提升为新主
[OK] 4 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.112.101:6379)
M: d998b2e51461ac1b829cb45549fa6752a6036338 192.168.112.101:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: ae52629045b3dd1ea67a2f5dfaff72ea6279720c 192.168.112.102:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: d21ad3986daf2907bf7e1c394fbe392842c8bcba 192.168.112.105:6379
   slots: (0 slots) slave
   replicates ae52629045b3dd1ea67a2f5dfaff72ea6279720c
S: 98c893e6fa44d3a434e57466f1cd4ea4ddcbf987 192.168.112.104:6379
   slots: (0 slots) slave
   replicates d998b2e51461ac1b829cb45549fa6752a6036338
M: 43e1436b14c1298b1bb37e382b4970a8b7528c3c 192.168.112.103:6379
   slots:[0-5460] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
#可以看到上面还是有3个master,只有2个slave了;因为103提升为新主了。
#查看103节点的info,如下:
[root@redis-node4 ~]# redis-cli -a 123456 -h 192.168.112.103 info replication
# Replication
role:master          #提升为master了
connected_slaves:0 #没有slave了
 
#然后恢复100这个故障节点,再启动redis服务;
[root@redis-node1 ~]# systemctl start redis
[root@redis-node1 ~]# redis-cli -a 123456 -h 192.168.112.100 info replication
# Replication
role:slave
master_host:192.168.112.103
master_port:6379
master_link_status:up
##可以看到重新启动的100节点变成了slave,并不会再直接提升为master。
 
 
7、Redis cluster集群动态扩容
由于业务量增加,需要增加192.168.112.106和192.168.112.107两台服务器,动态加入到redis cluster集群中,不能影响业务使用和数据丢失。
增加redis node节点,需要和之前的redis node版本相同、配置一致,再分别启动两台redis node,应为一主一从。
#1,安装redis
106和107节点全部安装和各个节点相同版本的redis:  
dnf -y install redis
执行以下命令,批量修改redis配置:
sed -i -e ‘s/bind 127.0.0.1/bind 0.0.0.0/’ -e ‘s/# masterauth .*/masterauth 123456/’ -e ‘s/# requirepass .*/requirepass 123456/’ -e ‘s/# cluster-enabled yes/cluster-enabled yes/’ -e ‘s/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-6379.conf/’ -e ‘s/# cluster-require-full-coverage yes/cluster-require-full-coverage no/’ -e ‘s/daemonize no/daemonize yes/’ /etc/redis.conf
然后启动两台redis:
systemctl enable –now redis
 
#2,添加新的master节点到集群
使用以下命令添加新节点,格式是添加的新redis节点IP和端口,添加到已有集群中任意节点的IP:端口
格式: add-node new-host:port existing-host:port   [–slave –master-id]
将106加入到redis cluster集群:
redis-cli -a 123456 –cluster add-node 192.168.112.106:6379 192.168.112.100:6379      #可以看到如下加入集群成功
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.112.106:6379 to make it join the cluster.
[OK] New node added correctly.
 
#3,106节点已经成功加入到集群,但此节点上没有slot位,也无从节点,而且106这个新节点是master
redis-cli -a 123456 –cluster info 192.168.112.100:6379
192.168.112.100:6379 (d5a1b887…) -> 0 keys | 5461 slots | 1 slaves.
192.168.112.106:6379 (9c3d53c2…) -> 0 keys | 0 slots | 0 slaves.
192.168.112.101:6379 (d998b2e5…) -> 2 keys | 5462 slots | 1 slaves.
192.168.112.102:6379 (ae526290…) -> 1 keys | 5461 slots | 1 slaves.
 
redis-cli -a 123456 –cluster check 192.168.112.100:6379
M: 9c3d53c2664e53aeddec2f9c3773e61fe5e3f28d 192.168.112.106:6379
   slots: (0 slots) master
 
redis-cli -a 123456 cluster info   #查看集群状态
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:7        #显示有7个,之前集群节点是6个
cluster_size:3 #还是3个cluster主从
 
#4,在新master 106上重新分配槽位。新node加入到集群之后默认是master节点,但是无slots,需要重新分配。
添加主机之后需要对添加至集群中的新主机重新分片,否则其没有分片也就无法写入数据了。
注意:重新分配槽位需要清空数据,需要先备份数据,扩展后再的恢复数据。
执行该命令开始分配slots槽位,进行重新分片:
redis-cli  -a 123456 –cluster reshard  <任意节点>:6379
执行之后,根据交互提示,需要手动输入以下选项:
How many slots …… (from 1 to 16384)?    4096          #新分配的槽位数=16384/master个数
What is the receiving node ID?                      #这里需要输入新的master 106的ID
Source node #all,是自动在所有redis node上划分槽位给新的master节点。
Do you…… (yes/no)?   yes       #确认分配
 
检查slot分配成功:
redis-cli  -a 123456 –cluster check 192.168.112.100:6379
192.168.112.100:6379 (d5a1b887…) -> 0 keys | 4096 slots | 1 slaves.
192.168.112.106:6379 (9c3d53c2…) -> 1 keys | 4096 slots | 0 slaves.
192.168.112.101:6379 (d998b2e5…) -> 1 keys | 4096 slots | 1 slaves.
192.168.112.102:6379 (ae526290…) -> 1 keys | 4096 slots | 1 slaves.
[OK] 3 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.112.100:6379)
M: 9c3d53c2664e53aeddec2f9c3773e61fe5e3f28d 192.168.112.106:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
 
#5,为新的master添加新的slave节点
需要再向当前的redis集群中添加一个redis单机服务器192.168.112.107,作为106的slave节点,实现高可用。
在新加节点到集群时,直接将之设置为slave,命令如下:
redis-cli -a 123456 –cluster add-node 192.168.112.107:6379 192.168.112.100:6379 –cluster-slave –cluster-master-id 9c3d53c2664e53aeddec2f9c3773e61fe5e3f28d   #112.100这里可以是任意节点IP,运行成功有以下提示:
>>> Send CLUSTER MEET to node 192.168.112.107:6379 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.112.106:6379.
 
然后验证是否成功:
redis-cli -a 123456 –cluster check 192.168.112.100:6379
192.168.112.100:6379 (d5a1b887…) -> 0 keys | 4096 slots | 1 slaves.
192.168.112.106:6379 (9c3d53c2…) -> 1 keys | 4096 slots | 1 slaves.         #可以看到这里已经有一个slave了
192.168.112.101:6379 (d998b2e5…) -> 1 keys | 4096 slots | 1 slaves.
192.168.112.102:6379 (ae526290…) -> 1 keys | 4096 slots | 1 slaves.
 
在107上查看info信息,看到确实是slave,且master节点指向106。
redis-cli -a 123456 info replication
# Replication
role:slave
master_host:192.168.112.106
 
 
8、Redis cluster集群动态缩容
添加节点的时候是先添加node节点到集群,然后分配槽位,删除节点的操作与添加节点的操作正好相反,是先将被删除的redis node上的槽位迁移到集群中其他redis node节点上,然后再将其删除。
 
#1,迁移master的槽位至其他master。被迁移的master源服务器必须保证没有数据,否则迁移会报错并中断。
redis-cli -a 123456 –cluster check 192.168.112.103:6379    #查看当前状态
redis-cli -a 123456 –cluster reshard 192.168.112.100:6379   #执行迁移操作,根据交互提示,需要手动输入以下选项:
How many slots …… (from 1 to 16384)?    1365      #每台迁走的槽位数=4096/3 也就是剩余的master个数
What is the receiving node ID?                   #这里需要输入迁移到目标的master的ID
Source node #1:                   #这里输入要删除的master 102节点的ID
Source node #2: done
Do you…… (yes/no)?   yes       #确认分配
以上只迁移了102节点的slot到一个master节点上,还要把slot槽位迁移到其余master节点上;用非交互式命令:
迁移到101上
redis-cli -a 123456 –cluster reshard 192.168.112.100:6379  –cluster-slots 1375 –cluster-from ae52629045b3dd1ea67a2f5dfaff72ea6279720c –cluster-to d998b2e51461ac1b829cb45549fa6752a6036338 –cluster-yes
迁移到100上
redis-cli -a 123456 –cluster reshard 192.168.112.100:6379  –cluster-slots 1375 –cluster-from ae52629045b3dd1ea67a2f5dfaff72ea6279720c –cluster-to d5a1b8871c930a7298c2e230e1f8f13e0a459080 –cluster-yes
 
102上的slot迁移完之后,它对应的slave也自动删除,成为其他master的slave
redis-cli -a 123456 –cluster check 192.168.112.103:6379       
192.168.112.106:6379 (9c3d53c2…) -> 1 keys | 5452 slots | 1 slaves.
192.168.112.101:6379 (d998b2e5…) -> 2 keys | 5471 slots | 1 slaves.
192.168.112.100:6379 (d5a1b887…) -> 0 keys | 5461 slots | 2 slaves.
192.168.112.102:6379 (ae526290…) -> 0 keys | 0 slots | 0 slaves.
 
redis-cli -a 123456 –no-auth-warning cluster info     #查看cluster信息
cluster_known_nodes:8
cluster_size:3                     #看到cluster_size变少了一个
 
#2,从集群中删除服务器:102槽位是迁移完成了,但是这个服务器ip信息还在集群中,因此要从集群中删除。
注意,删除服务器前,必须清楚主机上面的槽位,否则删除主机会失败,
redis-cli -a 123456 –cluster del-node <任意节点>:6379 ae52629045b3dd1ea67a2f5dfaff72ea6279720c
>>> Removing node ae52629045b3dd1ea67a2f5dfaff72ea6279720c from cluster 192.168.112.100:6379
>>> Sending CLUSTER FORGET messages to the cluster…
>>> SHUTDOWN the node.        #完成。删除节点后,redis进程自动关闭
然后手动删除节点信息:
[root@redis-node3 ~]# ss -tnl        #redis进程已经不在
[root@redis-node3 ~]# rm -rf /var/lib/redis/nodes-6379.conf
 
redis-cli -a 123456 –cluster check 192.168.112.103:6379   #验证一下删除结果,102这个master节点已不在
192.168.112.106:6379 (9c3d53c2…) -> 1 keys | 5452 slots | 1 slaves.
192.168.112.101:6379 (d998b2e5…) -> 2 keys | 5471 slots | 1 slaves.
192.168.112.100:6379 (d5a1b887…) -> 0 keys | 5461 slots | 2 slaves.    #多余了一个从节点
 
#3,删除多余的slave从节点,
redis-cli -a 123456 –cluster del-node 192.168.112.100:6379 564f3aaa72e1ae85fb56d88486918c93c0093f34 #105节点ID
>>> Removing node 564f3aaa72e1ae85fb56d88486918c93c0093f34 from cluster 192.168.112.100:6379
>>> Sending CLUSTER FORGET messages to the cluster…
>>> SHUTDOWN the node
 
手动删除105节点信息
[root@redis-node8 ~]# ss -tnl        #redis进程已经不在
[root@redis-node8 ~]# rm -rf /var/lib/redis/nodes-6379.conf
 
最后,查询cluster信息:
redis-cli -a 123456 –cluster info 192.168.112.100:6379
192.168.112.100:6379 (d5a1b887…) -> 0 keys | 5461 slots | 1 slaves.
192.168.112.106:6379 (9c3d53c2…) -> 1 keys | 5452 slots | 1 slaves.
192.168.112.101:6379 (d998b2e5…) -> 2 keys | 5471 slots | 1 slaves
 
查询集群信息:
redis-cli -a 123456 -h 192.168.112.100 cluster info
cluster_known_nodes:6            #只剩6个节点了
cluster_size:3
 
9、redis cluster的局限性
大多数时客户端性能会降低;命令无法扩节点使用:mget 、keys、flush等;
不支持多个数据库,集群模式下只有一个db 0;cluster主从复制只支持一层,不支持树形和级联复制。
 

 

 

 

原创文章,作者:wz,如若转载,请注明出处:https://www.wzstyle.cn/992.html

发表评论

邮箱地址不会被公开。 必填项已用*标注