Redis——Cluster集群

Redis集群

为了解决主从复制中单节点的写入负载和存储问题,需要通过搭建Cluster集群来解决这些问题。最简单的Cluster集群有三个主机和三个从机,一共六个节点组成。

架构图

cluster.png

特点

  • 所有的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
复制多个配置文件.png

第三步:启动六台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
启动6台redis.png

第四步:复制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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,794评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,050评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,587评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,861评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,901评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,898评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,832评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,617评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,077评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,349评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,483评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,199评论 5 341
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,824评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,442评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,632评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,474评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,393评论 2 352