Redis集群
为了解决主从复制中单节点的写入负载和存储问题,需要通过搭建Cluster集群来解决这些问题。最简单的Cluster集群有三个主机和三个从机,一共六个节点组成。
架构图
特点
- 所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽。
- 节点的fail是通过集群中超过半数的节点检测时效时才生效。
- 客户端与redis节点直连,不需要中间proxy层,客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可。
- redis-cluster把所有的物理节点映射[0-16383]slot上,cluster负责维护node<->slot<->value。
- 节点的分配是通过redis cluster哈希槽的算法(CRC16)来计算
搭建步骤
第一步:安装ruby和redis集群依赖
yum install -y ruby rubygems
gem install redis -v 3.2.1
第二步:编写配置文件
由于电脑配置因素,没办法一次运行6个虚拟机。因此这次集群操作就在一台机器上进行配置。
#先复制一个配置文件
cp redis.conf redis7001.conf
#在配置文件中的某些配置修改成以下内容
bind 0.0.0.0
port 7001
daemonize yes
dbfilename "dump7001.rdb"
cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-node-timeout 5000
appendonly yes
appendfilename "appendonly7001.aof"
#然后复制多份配置文件,重复上面的修改
cp redis7001.conf redis7002.conf
cp redis7001.conf redis7003.conf
cp redis7001.conf redis7004.conf
cp redis7001.conf redis7005.conf
cp redis7001.conf redis7006.conf
第三步:启动六台redis
redis-server redis7001.conf
redis-server redis7002.conf
redis-server redis7003.conf
redis-server redis7004.conf
redis-server redis7005.conf
redis-server redis7006.conf
第四步:复制redis-trib.rb文件到redis的安装目录下
redis-trib.rb文件在源码包中的src下
cp /usr/redis-4.0.10/src/redis-trib.rb /usr/local/bin/
创建集群
#创建集群,1代表每个主机有一个从机,前三个为主机,后三个为从机
[root@redis20 bin]# ./redis-trib.rb create --replicas 1 192.168.48.20:7001 192.168.48.20:7002 192.168.48.20:7003 192.168.48.20:7004 192.168.48.20:7005 192.168.48.20:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.48.20:7001
192.168.48.20:7002
192.168.48.20:7003
Adding replica 192.168.48.20:7005 to 192.168.48.20:7001
Adding replica 192.168.48.20:7006 to 192.168.48.20:7002
Adding replica 192.168.48.20:7004 to 192.168.48.20:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 40fd06219a31bbdc4011c030bd2b54a4fc437867 192.168.48.20:7001
slots:0-5460 (5461 slots) master
M: f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40 192.168.48.20:7002
slots:5461-10922 (5462 slots) master
M: 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba 192.168.48.20:7003
slots:10923-16383 (5461 slots) master
S: eedd603363a5965dec6950f27853604fcd3eee09 192.168.48.20:7004
replicates f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40
S: 91053d43d8888f95be8868f228cbbdfe518360c5 192.168.48.20:7005
replicates 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba
S: d62a0a1e55d73bd8836ff1c3303c6862528aecb0 192.168.48.20:7006
replicates 40fd06219a31bbdc4011c030bd2b54a4fc437867
Can I set the above configuration? (type 'yes' to accept): yes
#M代表主机,S代表从机。这里给出了主机和从机的分配方式,当前的主从是7001--7006、7002--7004、7003-7005。如果不满意这个分配方式可以输入no,集群创建失效!
>>> 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.48.20:7001)
M: 40fd06219a31bbdc4011c030bd2b54a4fc437867 192.168.48.20:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 91053d43d8888f95be8868f228cbbdfe518360c5 192.168.48.20:7005
slots: (0 slots) slave
replicates 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba
M: f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40 192.168.48.20:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba 192.168.48.20:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: d62a0a1e55d73bd8836ff1c3303c6862528aecb0 192.168.48.20:7006
slots: (0 slots) slave
replicates 40fd06219a31bbdc4011c030bd2b54a4fc437867
S: eedd603363a5965dec6950f27853604fcd3eee09 192.168.48.20:7004
slots: (0 slots) slave
replicates f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#出现上述代码,则代表集群创建成功!
#7001占用的哈希槽为0-5460,对应的从机为7006;7002占用的哈希槽为5461-10922,对应的从机为7004;7003占用的哈希槽为10923-16383,对应的从机为7005。
#当前的从节点都是没有哈希槽的,当从节点对应的主节点宕机了,将主节点的数据转到从节点上时才会有哈希槽。
至此集群已经搭建成功!
集群的操作
查看集群的状态
#check后面跟的是集群中任意一个节点的地址
[root@redis20 bin]# ./redis-trib.rb check 192.168.48.20:7002
>>> Performing Cluster Check (using node 192.168.48.20:7002)
M: f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40 192.168.48.20:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 40fd06219a31bbdc4011c030bd2b54a4fc437867 192.168.48.20:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: eedd603363a5965dec6950f27853604fcd3eee09 192.168.48.20:7004
slots: (0 slots) slave
replicates f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40
S: 91053d43d8888f95be8868f228cbbdfe518360c5 192.168.48.20:7005
slots: (0 slots) slave
replicates 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba
S: d62a0a1e55d73bd8836ff1c3303c6862528aecb0 192.168.48.20:7006
slots: (0 slots) slave
replicates 40fd06219a31bbdc4011c030bd2b54a4fc437867
M: 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba 192.168.48.20:7003
slots:10923-16383 (5461 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.
操作集群
# -c代表的是连接的是集群,只有连接集群中的主节点才能进行写操作,从节点只能进行读操作。
[root@redis20 bin]# redis-cli -p 7001 -c
#插入一条数据,发现此时通过CRC16算法得出这个值需存在哈希槽为5798的节点中,即为7002节点中
127.0.0.1:7001> set name zhangsan
-> Redirected to slot [5798] located at 192.168.48.20:7002
OK
#当我们进入到其他节点中查看这个key时,也是在7002节点中取出这个值
127.0.0.1:7003> get name
-> Redirected to slot [5798] located at 192.168.48.20:7002
"zhangsan"
某个主节点宕机
[root@redis20 bin]# ps aux|grep redis
avahi 736 0.0 0.1 62296 2060 ? Ss 21:15 0:00 avahi-daemon: running [redis20.local]
root 2951 0.0 1.2 205664 22740 pts/0 Tl 21:17 0:00 /usr/bin/ruby /usr/bin/gem install redis
root 3030 0.0 1.2 206564 23400 pts/0 Tl 21:20 0:00 /usr/bin/ruby /usr/bin/gem install redis
root 3460 0.2 0.6 149508 11884 ? Ssl 21:50 0:06 redis-server 0.0.0.0:7001 [cluster]
root 3520 0.2 0.6 149508 11888 ? Ssl 21:53 0:06 redis-server 0.0.0.0:7002 [cluster]
root 3525 0.2 0.6 149508 11888 ? Ssl 21:53 0:05 redis-server 0.0.0.0:7003 [cluster]
root 3530 0.2 0.6 149508 11896 ? Ssl 21:53 0:05 redis-server 0.0.0.0:7004 [cluster]
root 3535 0.2 0.6 149508 11900 ? Ssl 21:53 0:06 redis-server 0.0.0.0:7005 [cluster]
root 3540 0.2 0.6 149508 11900 ? Ssl 21:53 0:06 redis-server 0.0.0.0:7006 [cluster]
root 3980 0.0 0.2 16320 5420 pts/1 S+ 22:23 0:00 redis-cli -p 7001 -c
root 4058 0.0 0.2 16320 5408 pts/2 S+ 22:26 0:00 redis-cli -p 7003 -c
root 4115 0.0 0.0 112828 976 pts/0 S+ 22:30 0:00 grep --color=auto redis
#杀死某个主节点。
[root@redis20 bin]# kill 3460
[root@redis20 bin]# ps aux|grep redis
avahi 736 0.0 0.1 62296 2060 ? Ss 21:15 0:00 avahi-daemon: running [redis20.local]
root 2951 0.0 1.2 205664 22740 pts/0 Tl 21:17 0:00 /usr/bin/ruby /usr/bin/gem install redis
root 3030 0.0 1.2 206564 23400 pts/0 Tl 21:20 0:00 /usr/bin/ruby /usr/bin/gem install redis
root 3520 0.2 0.6 149508 11888 ? Ssl 21:53 0:06 redis-server 0.0.0.0:7002 [cluster]
root 3525 0.2 0.6 149508 11888 ? Ssl 21:53 0:06 redis-server 0.0.0.0:7003 [cluster]
root 3530 0.2 0.6 149508 11896 ? Ssl 21:53 0:06 redis-server 0.0.0.0:7004 [cluster]
root 3535 0.2 0.6 149508 11900 ? Ssl 21:53 0:06 redis-server 0.0.0.0:7005 [cluster]
root 3540 0.2 0.6 149508 11900 ? Ssl 21:53 0:06 redis-server 0.0.0.0:7006 [cluster]
root 3980 0.0 0.2 16320 5420 pts/1 S+ 22:23 0:00 redis-cli -p 7001 -c
root 4058 0.0 0.2 16320 5408 pts/2 S+ 22:26 0:00 redis-cli -p 7003 -c
root 4117 0.0 0.0 112828 980 pts/0 S+ 22:30 0:00 grep --color=auto redis
##杀死主节点后立即查看集群的状态,可以发现当前集群不完整,出现了错误
[root@redis20 bin]# ./redis-trib.rb check 192.168.48.20:7002
[ERR] Sorry, can not connect to node 192.168.48.20:7001
*** WARNING: 192.168.48.20:7006 claims to be slave of unknown node ID 40fd06219a31bbdc4011c030bd2b54a4fc437867.
>>> Performing Cluster Check (using node 192.168.48.20:7002)
M: f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40 192.168.48.20:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: eedd603363a5965dec6950f27853604fcd3eee09 192.168.48.20:7004
slots: (0 slots) slave
replicates f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40
S: 91053d43d8888f95be8868f228cbbdfe518360c5 192.168.48.20:7005
slots: (0 slots) slave
replicates 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba
S: d62a0a1e55d73bd8836ff1c3303c6862528aecb0 192.168.48.20:7006
slots: (0 slots) slave
replicates 40fd06219a31bbdc4011c030bd2b54a4fc437867
M: 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba 192.168.48.20:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.
#当redis的心跳验证过后再次查看集群的状态
[root@redis20 bin]# ./redis-trib.rb check 192.168.48.20:7002
>>> Performing Cluster Check (using node 192.168.48.20:7002)
M: f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40 192.168.48.20:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: eedd603363a5965dec6950f27853604fcd3eee09 192.168.48.20:7004
slots: (0 slots) slave
replicates f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40
S: 91053d43d8888f95be8868f228cbbdfe518360c5 192.168.48.20:7005
slots: (0 slots) slave
replicates 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba
M: d62a0a1e55d73bd8836ff1c3303c6862528aecb0 192.168.48.20:7006
slots:0-5460 (5461 slots) master
0 additional replica(s)
M: 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba 192.168.48.20:7003
slots:10923-16383 (5461 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.
#此时7001的从节点7006变为了主节点,已成功转移故障
#重新启动7001
[root@redis20 bin]# ./redis-server redis7001.conf
4194:C 10 Aug 22:38:51.033 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4194:C 10 Aug 22:38:51.033 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=4194, just started
4194:C 10 Aug 22:38:51.033 # Configuration loaded
#查看集群状态
[root@redis20 bin]# ./redis-trib.rb check 192.168.48.20:7001
>>> Performing Cluster Check (using node 192.168.48.20:7001)
S: 40fd06219a31bbdc4011c030bd2b54a4fc437867 192.168.48.20:7001
slots: (0 slots) slave
replicates d62a0a1e55d73bd8836ff1c3303c6862528aecb0
M: d62a0a1e55d73bd8836ff1c3303c6862528aecb0 192.168.48.20:7006
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: eedd603363a5965dec6950f27853604fcd3eee09 192.168.48.20:7004
slots: (0 slots) slave
replicates f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40
S: 91053d43d8888f95be8868f228cbbdfe518360c5 192.168.48.20:7005
slots: (0 slots) slave
replicates 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba
M: 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba 192.168.48.20:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40 192.168.48.20:7002
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.
#7001此时已经变成了7006的从机了
添加主节点
命令:redis-trib.rb add-node 新节点 集群中的任意一个节点
。
默认添加的节点就为主节点,不过没有分配哈希槽。
#以集群的方式启动这个节点
[root@redis20 bin]# redis-server redis7007.conf
4398:C 10 Aug 22:53:43.258 oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4398:C 10 Aug 22:53:43.258 Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=4398, just started
4398:C 10 Aug 22:53:43.258 Configuration loaded
#将节点添加至集群中
[root@redis20 bin] redis-trib.rb add-node 192.168.48.20:7007 192.168.48.20:7001
>>> Adding node 192.168.48.20:7007 to cluster 192.168.48.20:7001
>>> Performing Cluster Check (using node 192.168.48.20:7001)
S: 40fd06219a31bbdc4011c030bd2b54a4fc437867 192.168.48.20:7001
slots: (0 slots) slave
replicates d62a0a1e55d73bd8836ff1c3303c6862528aecb0
M: d62a0a1e55d73bd8836ff1c3303c6862528aecb0 192.168.48.20:7006
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: eedd603363a5965dec6950f27853604fcd3eee09 192.168.48.20:7004
slots: (0 slots) slave
replicates f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40
S: 91053d43d8888f95be8868f228cbbdfe518360c5 192.168.48.20:7005
slots: (0 slots) slave
replicates 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba
M: 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba 192.168.48.20:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40 192.168.48.20:7002
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.48.20:7007 to make it join the cluster.
[OK] New node added correctly.
#查看此时集群的状态
[root@redis20 bin] ./redis-trib.rb check 192.168.48.20:7001
>>> Performing Cluster Check (using node 192.168.48.20:7001)
S: 40fd06219a31bbdc4011c030bd2b54a4fc437867 192.168.48.20:7001
slots: (0 slots) slave
replicates d62a0a1e55d73bd8836ff1c3303c6862528aecb0
M: d62a0a1e55d73bd8836ff1c3303c6862528aecb0 192.168.48.20:7006
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: eedd603363a5965dec6950f27853604fcd3eee09 192.168.48.20:7004
slots: (0 slots) slave
replicates f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40
S: 91053d43d8888f95be8868f228cbbdfe518360c5 192.168.48.20:7005
slots: (0 slots) slave
replicates 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba
M: 9413faa55049f0cf48d4ceab96de116f90f21a2a 192.168.48.20:7007
slots: (0 slots) master
0 additional replica(s)
M: 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba 192.168.48.20:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40 192.168.48.20:7002
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.
#发现新增的主节点7007没有哈希槽。
添加从节点
随机添加从节点命令:redis-trib.rb add-node --slave 新加入的节点 原集群中的任意一个节点
注:当添加从节点没有指定主节点时,redis会将这个从节点添加到从节点比较少的主节点下。
为指定的主机添加从节点命令:redis-trib.rb add-node --slave --master-id master节点id 新加入的节点 原集群中的任意一个节点
#添加一个新节点7008,将7008节点加入到7007主机。
#首先依旧是复制一份配置文件命名为redis7008.conf,修改其中的端口号等信息。启动7008节点
[root@redis20 bin] cp redis7007.conf redis7008.conf
[root@redis20 bin] vim redis7008.conf
[root@redis20 bin] redis-server redis7008.conf
#将7008节点加入到7007主机中作为从节点。
redis-trib.rb add-node --slave --master-id 9413faa55049f0cf48d4ceab96de116f90f21a2a 192.168.48.20:7008 192.168.48.20:7007
#查看当前集群状态
[root@redis20 bin]# redis-trib.rb check 192.168.48.20:7001
>>> Performing Cluster Check (using node 192.168.48.20:7001)
S: 40fd06219a31bbdc4011c030bd2b54a4fc437867 192.168.48.20:7001
slots: (0 slots) slave
replicates d62a0a1e55d73bd8836ff1c3303c6862528aecb0
M: f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40 192.168.48.20:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 91053d43d8888f95be8868f228cbbdfe518360c5 192.168.48.20:7005
slots: (0 slots) slave
replicates 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba
M: 9413faa55049f0cf48d4ceab96de116f90f21a2a 192.168.48.20:7007
slots: (0 slots) master
1 additional replica(s)
S: cd8ede488190cbcaf67bd9a94384fbd6c2a29dd2 192.168.48.20:7008
slots: (0 slots) slave
replicates 9413faa55049f0cf48d4ceab96de116f90f21a2a
M: d62a0a1e55d73bd8836ff1c3303c6862528aecb0 192.168.48.20:7006
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba 192.168.48.20:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: eedd603363a5965dec6950f27853604fcd3eee09 192.168.48.20:7004
slots: (0 slots) slave
replicates f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#添加成功!
删除从节点
命令:redis-trib.rb del-node 集群中任意节点 被删除节点的id
注:被删除的节点必须是没有哈希槽的节点
#删除刚刚添加的7008节点
[root@redis20 bin]# redis-trib.rb del-node 192.168.48.20:7008 cd8ede488190cbcaf67bd9a94384fbd6c2a29dd2
>>> Removing node cd8ede488190cbcaf67bd9a94384fbd6c2a29dd2 from cluster 192.168.48.20:7008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
#查看当前集群状态
[root@redis20 bin]# redis-trib.rb check 192.168.48.20:7001
>>> Performing Cluster Check (using node 192.168.48.20:7001)
S: 40fd06219a31bbdc4011c030bd2b54a4fc437867 192.168.48.20:7001
slots: (0 slots) slave
replicates d62a0a1e55d73bd8836ff1c3303c6862528aecb0
M: f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40 192.168.48.20:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 91053d43d8888f95be8868f228cbbdfe518360c5 192.168.48.20:7005
slots: (0 slots) slave
replicates 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba
M: 9413faa55049f0cf48d4ceab96de116f90f21a2a 192.168.48.20:7007
slots: (0 slots) master
0 additional replica(s)
M: d62a0a1e55d73bd8836ff1c3303c6862528aecb0 192.168.48.20:7006
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba 192.168.48.20:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: eedd603363a5965dec6950f27853604fcd3eee09 192.168.48.20:7004
slots: (0 slots) slave
replicates f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#删除成功!
集群分片
命令:redis-trib.rb reshard 集群中任意节点
#为7007主机分配哈希槽
[root@redis20 bin] redis-trib.rb reshard 192.168.48.20:7007
#分配多少个哈希槽
How many slots do you want to move (from 1 to 16384)? 500
#分配给哪个主节点,通过id指明
What is the receiving node ID? 9413faa55049f0cf48d4ceab96de116f90f21a2a
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.
#分配策略,'all'为三个主节点每个分一点,'done'为指定某个主节点分配
Source node #1:all
#分配完成,查看集群状态
[root@redis20 bin]# redis-trib.rb check 192.168.48.20:7001
>>> Performing Cluster Check (using node 192.168.48.20:7001)
S: 40fd06219a31bbdc4011c030bd2b54a4fc437867 192.168.48.20:7001
slots: (0 slots) slave
replicates d62a0a1e55d73bd8836ff1c3303c6862528aecb0
M: f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40 192.168.48.20:7002
slots:5628-10922 (5295 slots) master
1 additional replica(s)
S: 91053d43d8888f95be8868f228cbbdfe518360c5 192.168.48.20:7005
slots: (0 slots) slave
replicates 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba
M: 9413faa55049f0cf48d4ceab96de116f90f21a2a 192.168.48.20:7007
slots:0-165,5461-5627,10923-11088 (499 slots) master
0 additional replica(s)
M: d62a0a1e55d73bd8836ff1c3303c6862528aecb0 192.168.48.20:7006
slots:166-5460 (5295 slots) master
1 additional replica(s)
M: 68b9865aeb8cbef02ed146ddc3a643ff86ff23ba 192.168.48.20:7003
slots:11089-16383 (5295 slots) master
1 additional replica(s)
S: eedd603363a5965dec6950f27853604fcd3eee09 192.168.48.20:7004
slots: (0 slots) slave
replicates f8f25c6aefaf4db3eda8b6ff6e60733d1a14bd40
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#7007的slots:0-165,5461-5627,10923-11088 (499 slots) master,已经分配成功!
springboot连接集群
只需要修改以下yml配置文件即可。
spring:
redis:
cluster:
nodes: 192.168.48.20:7001,192.168.48.20:7002,192.168.48.20:7003,192.168.48.20:7004,192.168.48.20:7005,192.168.48.20:7006