redis cluster使用

0. 架构简介

1. 架构类型

1.1 单机模式
优点:
1、部署简单,2、数据一致性
问题:
1、内存容量有限 2、处理能力有限 3、无法高可用
1.2 主从模式
流程:类似于mysql主从同步,通过RDB或者AOF方式,从库同步主库数据,主库出现故障时,手动切换到从库提供服务
优点:降低了主库的读压力
问题:主库写的压力,无法保证高可用
1.3 哨兵模式
流程:在主从模式中加入一个哨兵的角色,实时监控机器的状态,当主库出现问题时,自动切换至从库
优点:高可用,故障自动转移,服务监控
问题:主库写的压力,切换时数据一致性的偏差
1.4 cluster集群模式
流程:通过虚拟哈希槽,将数据分布在多个节点,客户端直接与节点交互,节点可动态扩展
优点:可扩展性,高可用,故障转移
问题:资源隔离性较差,数据的强一致性无法保证

2. cluster集群特点

1、所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽。
2、节点的fail是通过集群中超过半数的节点检测失效时才生效。
3、客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可。
4、redis-cluster把所有的物理节点映射到[0-16383]slot上(不一定是平均分配),cluster 负责维护node<->slot<->value。
5、Redis集群预分好16384个桶,当需要在 Redis 集群中放置一个 key-value 时,根据 CRC16(key) mod 16384的值,决定将一个key放到哪个桶中。

3.数据存取

存值:
redis cluster内置了13684个槽,每个节点对应自己所管理的槽数的区间;
当客户端向集群中加入一个key-values 数据时,redis先对key使用crc16算法(crc16(key)%16384),得出一个值,再除以13684取余,得到槽数;
每个key都会对应一个编号在 0-16383之间的哈希槽,通过这个槽数找到节点,将数据存到节点上

取值:通过key值,crc16算法,找到哈希槽值,跳转到对应的节点,取值

image

1. 部署使用

1.安装redis

1.wget http://download.redis.io/releases/redis-4.0.8.tar.gz
2.tar xzf redis-4.0.8.tar.gz
3.cd redis-4.0.8
4.make
5.make install PREFIX=/usr/redis-cluster
6. mv /usr/redis-cluster/bin redis01
7. [root@localhost redis-cluster]# ls /usr/redis-cluster/redis01/
redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-cluster  redis-sentinel  redis-server

2.ruby 安装

yum remove ruby
yum install ruby
yum install rubygems
gem install redis
[root@localhost redis-cluster]# ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
参考:
https://www.runoob.com/ruby/ruby-installation-unix.html

3.节点启动与配置文件

# http://redisdoc.com/topic/cluster-tutorial.html#id2
1. vim redis.conf

# 后台运行
daemonize yes
# 端口(7000至7005)
port 7000
# 开启cluster
cluster-enabled yes
# 指定cluster-config-file
cluster-config-file /usr/redis-cluster/redis-cluster/nodesfile/nodes-7000.conf
# 节点通信时间
cluster-node-timeout 15000
# 持久化方式 
appendonly yes

2. 创建节点目录
[root@localhost redis-cluster]# pwd
/usr/redis-cluster/redis-cluster
[root@localhost redis-cluster]# ls
7000  7001  7002  7003  7004  7005   nodesfile  redis-trib.rb  start-all.sh
# 注:redis-trib.rb是从redis解压文件夹中src目录中拷贝过来的

3. 启动脚本内容
[root@localhost redis-cluster]# cat start-all.sh 
/usr/redis-cluster/redis01/redis-server /usr/redis-cluster/redis-cluster/7000/redis.conf
/usr/redis-cluster/redis01/redis-server /usr/redis-cluster/redis-cluster/7001/redis.conf
/usr/redis-cluster/redis01/redis-server /usr/redis-cluster/redis-cluster/7002/redis.conf
/usr/redis-cluster/redis01/redis-server /usr/redis-cluster/redis-cluster/7003/redis.conf
/usr/redis-cluster/redis01/redis-server /usr/redis-cluster/redis-cluster/7004/redis.conf
/usr/redis-cluster/redis01/redis-server /usr/redis-cluster/redis-cluster/7005/redis.conf

4.启动这六个节点
[root@localhost redis-cluster]# /bin/bash start-all.sh
12321:C 14 Jan 19:50:58.592 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12321:C 14 Jan 19:50:58.593 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=12321, just started
12321:C 14 Jan 19:50:58.593 # Configuration loaded
12323:C 14 Jan 19:50:58.601 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12323:C 14 Jan 19:50:58.601 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=12323, just started
12323:C 14 Jan 19:50:58.601 # Configuration loaded
12325:C 14 Jan 19:50:58.606 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12325:C 14 Jan 19:50:58.606 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=12325, just started
12325:C 14 Jan 19:50:58.606 # Configuration loaded
12330:C 14 Jan 19:50:58.611 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12330:C 14 Jan 19:50:58.611 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=12330, just started
12330:C 14 Jan 19:50:58.611 # Configuration loaded
12332:C 14 Jan 19:50:58.620 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12332:C 14 Jan 19:50:58.620 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=12332, just started
12332:C 14 Jan 19:50:58.620 # Configuration loaded
12334:C 14 Jan 19:50:58.629 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12334:C 14 Jan 19:50:58.630 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=12334, just started
12334:C 14 Jan 19:50:58.630 # Configuration loaded
[root@localhost redis-cluster]# ps -aux|grep redis
root     12322  0.1  0.3 149352  7600 ?        Ssl  19:50   0:00 /usr/redis-cluster/redis01/redis-server *:7000 [cluster]
root     12324  0.0  0.3 145256  7568 ?        Ssl  19:50   0:00 /usr/redis-cluster/redis01/redis-server *:7001 [cluster]
root     12329  0.0  0.3 145256  7564 ?        Ssl  19:50   0:00 /usr/redis-cluster/redis01/redis-server *:7002 [cluster]
root     12331  0.1  0.3 145256  7564 ?        Ssl  19:50   0:00 /usr/redis-cluster/redis01/redis-server *:7003 [cluster]
root     12333  0.1  0.3 145256  7564 ?        Ssl  19:50   0:00 /usr/redis-cluster/redis01/redis-server *:7004 [cluster]
root     12335  0.0  0.3 145256  7568 ?        Ssl  19:50   0:00 /usr/redis-cluster/redis01/redis-server *:7005 [cluster]
root     12352  0.0  0.0 112668   972 pts/2    S+   19:51   0:00 grep --color=auto redis

4.集群创建与测试

1. 创建
[root@localhost redis-cluster]# /qqc_data/redis-4.0.8/src/redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7000
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7000 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 09eb1650031e93f2d4f14af5d3d76018a333fc27 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: 2c352068fa9ecadc261bb7a64eff6e8ba55bb449 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: fdfecc925783209c42a9cf16f12868f00e55f212 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
S: 1182df09b7658a5285f1c46c2409fd05da84d1cd 127.0.0.1:7004
   replicates fdfecc925783209c42a9cf16f12868f00e55f212
S: 0825e23f8386a2feccbee88ed48a598e7a00fca3 127.0.0.1:7005
   replicates 09eb1650031e93f2d4f14af5d3d76018a333fc27
S: 7feaf11eabcc0f6119526d188366b4b41e3fa545 127.0.0.1:7000
   replicates 2c352068fa9ecadc261bb7a64eff6e8ba55bb449
Can I set the above configuration? (type 'yes' to accept): 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 127.0.0.1:7001)
M: 09eb1650031e93f2d4f14af5d3d76018a333fc27 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 7feaf11eabcc0f6119526d188366b4b41e3fa545 127.0.0.1:7000
   slots: (0 slots) slave
   replicates 2c352068fa9ecadc261bb7a64eff6e8ba55bb449
S: 1182df09b7658a5285f1c46c2409fd05da84d1cd 127.0.0.1:7004
   slots: (0 slots) slave
   replicates fdfecc925783209c42a9cf16f12868f00e55f212
S: 0825e23f8386a2feccbee88ed48a598e7a00fca3 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 09eb1650031e93f2d4f14af5d3d76018a333fc27
M: fdfecc925783209c42a9cf16f12868f00e55f212 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 2c352068fa9ecadc261bb7a64eff6e8ba55bb449 127.0.0.1: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.
# 注:有3个主节点,3个从节点,每个节点都是成功连接状态

2.测试
# 7001节点存值
[root@localhost redis01]# /usr/redis-cluster/redis01/redis-cli -c -p 7001
127.0.0.1:7001> set name quqinchao
-> Redirected to slot [5798] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get name
"quqinchao"

# 7000从节点获取name值
[root@localhost redis-cluster]# /usr/redis-cluster/redis01/redis-cli -c -p 7000
127.0.0.1:7000> get name
-> Redirected to slot [5798] located at 127.0.0.1:7002
"quqinchao"

5.重新分片

流程:重新分片操作基本上就是将某些节点上的哈希槽移动到另外一些节点上面, 
和创建集群一样,重新分片也可以使用 redis-trib 程序来执行
实例:将7003节点的1000个哈希槽分配到7001节点中

1、查看主节点及哈希槽区间
[root@localhost redis-cluster]# redis-cli -p 7000 cluster nodes | grep master
09eb1650031e93f2d4f14af5d3d76018a333fc27 127.0.0.1:7001@17001 master - 0 1614176563000 1 connected 0-5460
fdfecc925783209c42a9cf16f12868f00e55f212 127.0.0.1:7003@17003 master - 0 1614176564581 3 connected 10923-16383
2c352068fa9ecadc261bb7a64eff6e8ba55bb449 127.0.0.1:7002@17002 master - 0 1614176562517 2 connected 5461-10922

2、指定7001节点分片
[root@localhost redis-cluster]# ./redis-trib.rb reshard 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 09eb1650031e93f2d4f14af5d3d76018a333fc27 127.0.0.1:7001
   slots:0-5460,10923-11922 (6461 slots) master
   1 additional replica(s)
S: 7feaf11eabcc0f6119526d188366b4b41e3fa545 127.0.0.1:7000
   slots: (0 slots) slave
   replicates 2c352068fa9ecadc261bb7a64eff6e8ba55bb449
S: 1182df09b7658a5285f1c46c2409fd05da84d1cd 127.0.0.1:7004
   slots: (0 slots) slave
   replicates fdfecc925783209c42a9cf16f12868f00e55f212
S: 0825e23f8386a2feccbee88ed48a598e7a00fca3 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 09eb1650031e93f2d4f14af5d3d76018a333fc27
M: fdfecc925783209c42a9cf16f12868f00e55f212 127.0.0.1:7003
   slots:11923-16383 (4461 slots) master
   1 additional replica(s)
M: 2c352068fa9ecadc261bb7a64eff6e8ba55bb449 127.0.0.1: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.
How many slots do you want to move (from 1 to 16384)? 1000
What is the receiving node ID? 09eb1650031e93f2d4f14af5d3d76018a333fc27
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:fdfecc925783209c42a9cf16f12868f00e55f212
Source node #2:done
# 从7003节点接收1000个哈希槽

3、最新的分配结果
[root@localhost redis-cluster]# redis-cli -p 7000 cluster nodes | grep master
09eb1650031e93f2d4f14af5d3d76018a333fc27 127.0.0.1:7001@17001 master - 0 1614178523000 7 connected 0-5797 10923-11922
fdfecc925783209c42a9cf16f12868f00e55f212 127.0.0.1:7003@17003 master - 0 1614178524842 3 connected 11923-16383
2c352068fa9ecadc261bb7a64eff6e8ba55bb449 127.0.0.1:7002@17002 master - 0 1614178521000 2 connected 5798-10922

6. 故障转移

集群中三个主节点,三个从节点,某个主节点挂了,对应的从节点会变成主节点;
某一组主从节点都挂了,集群则无法提供服务

1、cluster nodes 命令输出
[root@localhost redis-cluster]# redis-cli -p 7000 cluster nodes
09eb1650031e93f2d4f14af5d3d76018a333fc27 127.0.0.1:7001@17001 master - 0 1614179747726 7 connected 0-5797 10923-11922
fdfecc925783209c42a9cf16f12868f00e55f212 127.0.0.1:7003@17003 master - 0 1614179748782 3 connected 11923-16383
7feaf11eabcc0f6119526d188366b4b41e3fa545 127.0.0.1:7000@17000 myself,slave 2c352068fa9ecadc261bb7a64eff6e8ba55bb449 0 1614179746000 6 connected
2c352068fa9ecadc261bb7a64eff6e8ba55bb449 127.0.0.1:7002@17002 master - 0 1614179747000 2 connected 5798-10922
1182df09b7658a5285f1c46c2409fd05da84d1cd 127.0.0.1:7004@17004 slave fdfecc925783209c42a9cf16f12868f00e55f212 0 1614179748000 3 connected
0825e23f8386a2feccbee88ed48a598e7a00fca3 127.0.0.1:7005@17005 slave 09eb1650031e93f2d4f14af5d3d76018a333fc27 0 1614179747000 7 connected
# 注:节点id,ip地址,角色,网络情况,哈希槽区间
7001对应的从节点为7005

2、关闭7001节点
[root@localhost redis-cluster]# ps -ef |grep 7001
root      5284 32007  0 23:28 pts/3    00:00:00 grep --color=auto 7001
root     12324     1  0 01:03 ?        00:02:08 /usr/redis-cluster/redis01/redis-server *:7001 [cluster]
[root@localhost redis-cluster]# kill -9 12324
[root@localhost redis-cluster]# redis-cli -p 7000 cluster nodes
09eb1650031e93f2d4f14af5d3d76018a333fc27 127.0.0.1:7001@17001 master,fail - 1614180569130 1614180566000 7 disconnected
fdfecc925783209c42a9cf16f12868f00e55f212 127.0.0.1:7003@17003 master - 0 1614180583000 3 connected 11923-16383
7feaf11eabcc0f6119526d188366b4b41e3fa545 127.0.0.1:7000@17000 myself,slave 2c352068fa9ecadc261bb7a64eff6e8ba55bb449 0 1614180582000 6 connected
2c352068fa9ecadc261bb7a64eff6e8ba55bb449 127.0.0.1:7002@17002 master - 0 1614180585705 2 connected 5798-10922
1182df09b7658a5285f1c46c2409fd05da84d1cd 127.0.0.1:7004@17004 slave fdfecc925783209c42a9cf16f12868f00e55f212 0 1614180584000 3 connected
0825e23f8386a2feccbee88ed48a598e7a00fca3 127.0.0.1:7005@17005 master - 0 1614180584688 8 connected 0-5797 10923-11922
# 7005代替了7001变成了主节点

3、重启启动7001后,变成了从节点
[root@localhost redis-cluster]# /usr/redis-cluster/redis01/redis-server /usr/redis-cluster/redis-cluster/7001/redis.conf
5470:C 24 Feb 23:32:28.033 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5470:C 24 Feb 23:32:28.033 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=5470, just started
5470:C 24 Feb 23:32:28.033 # Configuration loaded
[root@localhost redis-cluster]# redis-cli -p 7000 cluster nodes
09eb1650031e93f2d4f14af5d3d76018a333fc27 127.0.0.1:7001@17001 slave 0825e23f8386a2feccbee88ed48a598e7a00fca3 0 1614180781337 8 connected
fdfecc925783209c42a9cf16f12868f00e55f212 127.0.0.1:7003@17003 master - 0 1614180778000 3 connected 11923-16383
7feaf11eabcc0f6119526d188366b4b41e3fa545 127.0.0.1:7000@17000 myself,slave 2c352068fa9ecadc261bb7a64eff6e8ba55bb449 0 1614180780000 6 connected
2c352068fa9ecadc261bb7a64eff6e8ba55bb449 127.0.0.1:7002@17002 master - 0 1614180780319 2 connected 5798-10922
1182df09b7658a5285f1c46c2409fd05da84d1cd 127.0.0.1:7004@17004 slave fdfecc925783209c42a9cf16f12868f00e55f212 0 1614180780000 3 connected
0825e23f8386a2feccbee88ed48a598e7a00fca3 127.0.0.1:7005@17005 master - 0 1614180779302 8 connected 0-5797 10923-11922

7.添加新节点

7.1 添加主节点
流程:创建一个空节点,将其他节点的哈希桶转移到新节点

1、启动新节点7006(重复之前的配置方式)
[root@localhost redis-cluster]# /usr/redis-cluster/redis01/redis-server /usr/redis-cluster/redis-cluster/7006/redis.conf
6759:C 24 Feb 23:58:01.633 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6759:C 24 Feb 23:58:01.634 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=6759, just started
6759:C 24 Feb 23:58:01.634 # Configuration loaded
[root@localhost redis-cluster]# ps -aux|grep 7006
root      6760  0.1  0.3 145416  7640 ?        Ssl  23:58   0:00 /usr/redis-cluster/redis01/redis-server *:7006 [cluster]
root      6779  0.0  0.0 112828   984 pts/5    S+   23:58   0:00 grep --color=auto 7006

2、添加7006节点到集群中
# 命令中的 add-node 表示我们要让 redis-trib 将一个节点添加到集群里面,add-node之后跟着 的是新节点的IP地址和端口号,
再之后跟着的集群中任意一个已存在节点的 IP 地址和端口号,这里我们使用的是127.0.0.1:7001
[root@localhost redis-cluster]# ./redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7001
>>> Adding node 127.0.0.1:7006 to cluster 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: d5023d5370c38138e62b07feb5415722bda3bdec 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 1e92c4c65a33ae2283f5d26b2699edaa78171518 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 138094074b4bf95695548fff68fceda69af0a6ea 127.0.0.1:7000
   slots: (0 slots) slave
   replicates d5023d5370c38138e62b07feb5415722bda3bdec
S: 7676627ae04a8da73969e0453e670bc803456d6b 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 1e92c4c65a33ae2283f5d26b2699edaa78171518
M: 1449630d89044a43af9ebc8d3a129c04552b96dc 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: ef2e79f0987e596979d83b080c6235a2f58ebc00 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 1449630d89044a43af9ebc8d3a129c04552b96dc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:7006 to make it join the cluster.
[OK] New node added correctly.

[root@localhost redis-cluster]# redis-cli -p 7000 cluster nodes | grep master
d33d2dd3c31758e595e51e5216ec507160a36475 127.0.0.1:7006@17006 master - 0 1614190181825 0 connected
1449630d89044a43af9ebc8d3a129c04552b96dc 127.0.0.1:7003@17003 master - 0 1614190179723 3 connected 10923-16383
d5023d5370c38138e62b07feb5415722bda3bdec 127.0.0.1:7001@17001 master - 0 1614190181000 1 connected 0-5460
1e92c4c65a33ae2283f5d26b2699edaa78171518 127.0.0.1:7002@17002 master - 0 1614190182863 2 connected 5461-10922
# 注:7006节点中无哈希桶,将集群中的某些哈希桶移动到新节点里面,新节点就会成为真正的主节点,进行重新分片的操作即可

3.给7006节点分配哈希桶(从7001节点中取出500个哈希桶分配给7006)
[root@localhost redis-cluster]# ./redis-trib.rb reshard 127.0.0.1:7006
>>> Performing Cluster Check (using node 127.0.0.1:7006)
M: d33d2dd3c31758e595e51e5216ec507160a36475 127.0.0.1:7006
   slots: (0 slots) master
   0 additional replica(s)
M: d5023d5370c38138e62b07feb5415722bda3bdec 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 138094074b4bf95695548fff68fceda69af0a6ea 127.0.0.1:7000
   slots: (0 slots) slave
   replicates d5023d5370c38138e62b07feb5415722bda3bdec
S: ef2e79f0987e596979d83b080c6235a2f58ebc00 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 1449630d89044a43af9ebc8d3a129c04552b96dc
M: 1e92c4c65a33ae2283f5d26b2699edaa78171518 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 1449630d89044a43af9ebc8d3a129c04552b96dc 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 7676627ae04a8da73969e0453e670bc803456d6b 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 1e92c4c65a33ae2283f5d26b2699edaa78171518
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 500
What is the receiving node ID? d33d2dd3c31758e595e51e5216ec507160a36475
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:d5023d5370c38138e62b07feb5415722bda3bdec
Source node #2:done

# 查结果
[root@localhost redis-cluster]# redis-cli -p 7000 cluster nodes | grep master
d33d2dd3c31758e595e51e5216ec507160a36475 127.0.0.1:7006@17006 master - 0 1614191098978 7 connected 0-499
1449630d89044a43af9ebc8d3a129c04552b96dc 127.0.0.1:7003@17003 master - 0 1614191098000 3 connected 10923-16383
d5023d5370c38138e62b07feb5415722bda3bdec 127.0.0.1:7001@17001 master - 0 1614191097000 1 connected 500-5460
1e92c4c65a33ae2283f5d26b2699edaa78171518 127.0.0.1:7002@17002 master - 0 1614191095925 2 connected 5461-10922

7.2 添加从节点

流程:创建一个空节点,设置为集群中某个主节点的复制品

1、启动新节点7007(重复之前的配置方式)
[root@localhost redis-cluster]# /usr/redis-cluster/redis01/redis-server /usr/redis-cluster/redis-cluster/7007/redis.conf
14736:C 25 Feb 02:30:09.224 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14736:C 25 Feb 02:30:09.225 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=14736, just started
14736:C 25 Feb 02:30:09.225 # Configuration loaded
[root@localhost redis-cluster]# ps -aux|grep 7007
root     14737  0.1  0.3 145416  7552 ?        Ssl  02:30   0:00 /usr/redis-cluster/redis01/redis-server *:7007 [cluster]
root     14766  0.0  0.0 112828   984 pts/0    S+   02:30   0:00 grep --color=auto 7007

2、添加7007为7002主节点的从节点
# --slave: 添加的为从节点
# --master-id: 7002主节点的id
[root@localhost redis-cluster]# ./redis-trib.rb add-node --slave --master-id 1e92c4c65a33ae2283f5d26b2699edaa78171518 127.0.0.1:7007 127.0.0.1:7001 
>>> Adding node 127.0.0.1:7007 to cluster 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: d5023d5370c38138e62b07feb5415722bda3bdec 127.0.0.1:7001
   slots:500-5460 (4961 slots) master
   1 additional replica(s)
M: 1e92c4c65a33ae2283f5d26b2699edaa78171518 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 138094074b4bf95695548fff68fceda69af0a6ea 127.0.0.1:7000
   slots: (0 slots) slave
   replicates d5023d5370c38138e62b07feb5415722bda3bdec
S: 7676627ae04a8da73969e0453e670bc803456d6b 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 1e92c4c65a33ae2283f5d26b2699edaa78171518
M: d33d2dd3c31758e595e51e5216ec507160a36475 127.0.0.1:7006
   slots:0-499 (500 slots) master
   0 additional replica(s)
M: 1449630d89044a43af9ebc8d3a129c04552b96dc 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: ef2e79f0987e596979d83b080c6235a2f58ebc00 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 1449630d89044a43af9ebc8d3a129c04552b96dc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:7007 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 127.0.0.1:7002.
[OK] New node added correctly.

# 之前7006主节点没有从节点,cluster 将7002之前的从节点分配给了7006
[root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
S: 138094074b4bf95695548fff68fceda69af0a6ea 127.0.0.1:7000
   slots: (0 slots) slave
   replicates d5023d5370c38138e62b07feb5415722bda3bdec
M: d33d2dd3c31758e595e51e5216ec507160a36475 127.0.0.1:7006
   slots:0-499 (500 slots) master
   1 additional replica(s)
S: 899de898b8bd423c33d207a8efbb04dab12e3ec8 127.0.0.1:7007
   slots: (0 slots) slave
   replicates 1e92c4c65a33ae2283f5d26b2699edaa78171518
S: 7676627ae04a8da73969e0453e670bc803456d6b 127.0.0.1:7004
   slots: (0 slots) slave
   replicates d33d2dd3c31758e595e51e5216ec507160a36475
M: 1449630d89044a43af9ebc8d3a129c04552b96dc 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: d5023d5370c38138e62b07feb5415722bda3bdec 127.0.0.1:7001
   slots:500-5460 (4961 slots) master
   1 additional replica(s)
M: 1e92c4c65a33ae2283f5d26b2699edaa78171518 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: ef2e79f0987e596979d83b080c6235a2f58ebc00 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 1449630d89044a43af9ebc8d3a129c04552b96dc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

参考:http://redisdoc.com/topic/cluster-spec.html

posted @ 2021-02-21 17:44  朝朝哥  阅读(164)  评论(0编辑  收藏  举报