前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Redis高可用集群

Redis高可用集群

作者头像
Se7en258
发布2021-05-18 10:47:43
9780
发布2021-05-18 10:47:43
举报
文章被收录于专栏:Se7en的架构笔记

Redis集群模式和哨兵模式比较

在redis3.0以前的版本要实现集群一般是借助哨兵sentinel工具来监控master节点的状态,如果master节点异常,则会做主从切换,将某一台slave作为master,哨兵的配置略微复杂,并且性能和高可用性等各方面表现一般,特别是在主从切换的瞬间存在访问瞬断的情况,而且哨兵模式只有一个主节点对外提供服务,没法支持很高的并发,且单个主节点内存也不宜设置得过大,否则会导致持久化文件过大,影响数据恢复或主从同步的效率。

redis集群是一个由多个主从节点群组成的分布式服务器群,它具有复制、高可用和分片特性。Redis集群不需要sentinel哨兵也能完成节点移除和故障转移的功能,需要将每个节点设置成集群模式,这种集群模式没有中心节点,可水平扩展,据官方文档称可以线性扩展到上万个节点(官方推荐不超过1000个节点)。redis集群的性能和高可用性均优于之前版本的哨兵模式,且集群配置非常简单。

Redis高可用集群搭建

redis集群需要至少三个master节点,我们这里搭建三个master节点,并且给每个master再搭建一个slave节点,总共6个redis节点,这里用三台机器部署6个redis实例,每台机器一主一从。

IP地址

端口号

角色

主机名

192.168.1.156

8001

master

redis1

192.168.1.156

8004

slave

redis1

192.168.1.157

8002

master

redis2

192.168.1.157

8005

slave

redis2

192.168.1.158

8003

master

redis3

192.168.1.158

8006

slave

redis3

1.创建相关文件夹

redis1:

代码语言:javascript
复制
/usr/local/redis-cluster/
├── 8001
└── 8004

redis2:

代码语言:javascript
复制
/usr/local/redis-cluster/
├── 8002
└── 8005

redis3:

代码语言:javascript
复制
/usr/local/redis-cluster/
├── 8003
└── 8006

2.修改配置文件

代码语言:javascript
复制
daemonize yes #代表开启守护进程模式,在该模式下,redis会在后台运行
port 8001 #监听端口号
pidfile /var/run/redis_8001.pid #把pid进程号写入pidfile配置的文件
dir /usr/local/redis‐cluster/8001/  #指定数据文件存放位置,,必须要指定不同的目录位置,不然会丢失数据
bind 0.0.0.0  #监听本机所有IP地址
protected‐mode no #关闭保护模式,允许远程连接

#持久化
appendonly yes
appendfilename "appendonly.aof"

#集群相关配置
cluster-enabled yes #启动集群模式
cluster-config-file clusterinfo-8001.conf #集群节点信息文件
cluster-node-timeout 15000  #节点超时时间,当节点持续15000毫秒失联时,才认定该节点出现故障

#设置密码
requirepass Root@123 #设置redis访问密码
masterauth Root@123  #设置集群节点间访问密码,跟requirepass一致

把修改后的配置文件拷贝到其他几个redis目录(8001-8006)下,修改含有端口号信息的值即可。

3.创建集群

执行这条命令需要确认三台机器之间的redis实例要能相互访问,可以先简单把所有机器防火墙关掉,如果不关闭防火墙则需要打开redis服务端口和集群节点gossip通信端口(默认是在redis端口号上加10000)

代码语言:javascript
复制
[root@redis1 redis-cluster]# redis-cli  -a Root@123 --cluster create --cluster-replicas 1 192.168.1.156:8001 192.168.1.157:8002 192.168.1.158:8003 192.168.1.156:8004 192.168.1.157:8005 192.168.1.158:8006 

#输入以上命令后,会计算出集群中的节点信息和slot信息
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> 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.1.157:8005 to 192.168.1.156:8001
Adding replica 192.168.1.158:8006 to 192.168.1.157:8002
Adding replica 192.168.1.156:8004 to 192.168.1.158:8003
M: 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001
   slots:[0-5460] (5461 slots) master
M: 3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002
   slots:[5461-10922] (5462 slots) master
M: 242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003
   slots:[10923-16383] (5461 slots) master
S: 0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004
   replicates 242b7c122850cc358348fba56de9d685eb2d708d
S: 3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192.168.1.157:8005
   replicates 3e92d8cbd797437eba0a5644b11a993c4bcc8f95
S: 2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006
   replicates 3a0df944fae9502fb3ec70c19913c842f9fab5a9
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.1.156:8001)
M: 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006
   slots: (0 slots) slave
   replicates 3a0df944fae9502fb3ec70c19913c842f9fab5a9
S: 0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004
   slots: (0 slots) slave
   replicates 242b7c122850cc358348fba56de9d685eb2d708d
S: 3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192.168.1.157:8005
   slots: (0 slots) slave
   replicates 3e92d8cbd797437eba0a5644b11a993c4bcc8f95
M: 3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003
   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.

4.验证集群

集群状态验证

连接任意一个redis节点即可:-a:redis密码 -c:表示集群模式 -h:指定ip地址 -p:指定端口号

代码语言:javascript
复制
[root@redis1 redis-cluster]# redis-cli -a Root@123 -c -h 192.168.1.156 -p 8001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

#查看集群信息
192.168.1.156:8001> 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
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:182
cluster_stats_messages_pong_sent:192
cluster_stats_messages_sent:374
cluster_stats_messages_ping_received:187
cluster_stats_messages_pong_received:182
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:374

#查看集群节点列表
192.168.1.156:8001> cluster nodes
2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006@18006 slave 3a0df944fae9502fb3ec70c19913c842f9fab5a9 0 1608444303981 6 connected
0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004@18004 slave 242b7c122850cc358348fba56de9d685eb2d708d 0 1608444302978 4 connected
3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192.168.1.157:8005@18005 slave 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 0 1608444303000 5 connected
3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002@18002 master - 0 1608444302000 2 connected 5461-10922
3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001@18001 myself,master - 0 1608444304000 1 connected 0-5460
242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003@18003 master - 0 1608444304983 3 connected 10923-16383

此时redis节点的目录文件为:

redis1:

代码语言:javascript
复制
[root@redis1 redis-cluster]# tree /usr/local/redis-cluster/
/usr/local/redis-cluster/
├── 8001
│   ├── appendonly.aof
│   ├── clusterinfo-8001.conf
│   ├── dump.rdb
│   └── redis-8001.conf
└── 8004
    ├── appendonly.aof
    ├── clusterinfo-8004.conf
    ├── dump.rdb
    └── redis-8004.conf

redis2:

代码语言:javascript
复制
[root@redis2 redis-cluster]# tree /usr/local/redis-cluster/
/usr/local/redis-cluster/
├── 8002
│   ├── appendonly.aof
│   ├── clusterinfo-8002.conf
│   ├── dump.rdb
│   └── redis-8002.conf
└── 8005
    ├── appendonly.aof
    ├── clusterinfo-8005.conf
    ├── dump.rdb
    └── redis-8005.conf

2 directories, 8 files

redis3:

代码语言:javascript
复制
[root@redis3 redis-cluster]# tree /usr/local/redis-cluster/
/usr/local/redis-cluster/
├── 8003
│   ├── appendonly.aof
│   ├── clusterinfo-8003.conf
│   ├── dump.rdb
│   └── redis-8003.conf
└── 8006
    ├── appendonly.aof
    ├── clusterinfo-8006.conf
    ├── dump.rdb
    └── redis-8006.conf

2 directories, 8 files

其中clusterinfo-800*.conf文件中存放了集群的信息:

代码语言:javascript
复制
[root@redis1 redis-cluster]# cat /usr/local/redis-cluster/8001/clusterinfo-8001.conf 
2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006@18006 slave 3a0df944fae9502fb3ec70c19913c842f9fab5a9 0 1608444046183 6 connected
0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004@18004 slave 242b7c122850cc358348fba56de9d685eb2d708d 0 1608444045181 4 connected
3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192.168.1.157:8005@18005 slave 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 0 1608444047186 5 connected
3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002@18002 master - 0 1608444044000 2 connected 5461-10922
3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001@18001 myself,master - 0 1608444047000 1 connected 0-5460
242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003@18003 master - 0 1608444048190 3 connected 10923-16383
vars currentEpoch 6 lastVoteEpoch 0

数据操作验证

可以看到可以成功插入新的key,并且会将数据重定向到新的槽位。

代码语言:javascript
复制
[root@redis1 redis-cluster]# redis-cli -p 8001 -c -a Root@123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:8001> keys *
(empty list or set)
127.0.0.1:8001> set k1 v1
-> Redirected to slot [12706] located at 192.168.1.158:8003
OK
192.168.1.158:8003> keys *
1) "k1"

5.关闭集群

关闭集群需要逐个进行关闭,开启也是逐个进行开启,开启的时候会读取clusterinfo-800*.conf中的集群信息,重新恢复集群。

关闭集群

代码语言:javascript
复制
#redis1:
redis-cli -a Root@123 -p 8001 shutdown
redis-cli -a Root@123 -p 8004 shutdown

#redis2:
redis-cli -a Root@123 -p 8002 shutdown
redis-cli -a Root@123 -p 8005 shutdown

#redis3:
redis-cli -a Root@123 -p 8003 shutdown
redis-cli -a Root@123 -p 8006 shutdown

重新开启集群

代码语言:javascript
复制
#redis1:
redis-server /usr/local/redis-cluster/8001/redis-8001.conf
redis-server /usr/local/redis-cluster/8004/redis-8004.conf

#redis2:
redis-server /usr/local/redis-cluster/8002/redis-8002.conf
redis-server /usr/local/redis-cluster/8005/redis-8005.conf

#redis3:
redis-server /usr/local/redis-cluster/8003/redis-8003.conf
redis-server /usr/local/redis-cluster/8006/redis-8006.conf

Redis高可用集群水平扩展

Redis3.0以后的版本虽然有了集群功能,提供了比之前版本的哨兵模式更高的性能与可用性,但是集群的水平扩展却比较麻烦,今天就来带大家看看redis高可用集群如何做水平扩展,原始集群(见下图)由6个节点组成,6个节点分布在三台机器上,采用三主三从的模式:

1.查看扩容前面集群状态

可以看出,整个集群运行正常,三个master节点和三个slave节点,8001端口的实例节点存储0-5460这些hash槽,8002端口的实例节点存储5461-10922这些hash槽,8003端口的实例节点存储10923-16383这些hash槽,这三个master节点存储的所有hash槽组成 redis集群的存储槽位,slave点是每个主节点的备份从节点,不显示存储槽位

代码语言:javascript
复制
[root@redis1 redis-cluster]# redis-cli -p 8001 -a Root@123 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:8001> 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
cluster_current_epoch:9
cluster_my_epoch:9
cluster_stats_messages_ping_sent:19548
cluster_stats_messages_pong_sent:17722
cluster_stats_messages_auth-req_sent:5
cluster_stats_messages_sent:37275
cluster_stats_messages_ping_received:17718
cluster_stats_messages_pong_received:18806
cluster_stats_messages_fail_received:2
cluster_stats_messages_auth-ack_received:2
cluster_stats_messages_received:36528
127.0.0.1:8001> CLUSTER NODES
2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006@18006 slave 3a0df944fae9502fb3ec70c19913c842f9fab5a9 0 1608464464000 6 connected
3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001@18001 myself,master - 0 1608464465000 9 connected 0-5460
0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004@18004 slave 242b7c122850cc358348fba56de9d685eb2d708d 0 1608464465854 4 connected
242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003@18003 master - 0 1608464463849 3 connected 10923-16383
3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002@18002 master - 0 1608464464852 2 connected 5461-10922
3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192.168.1.157:8005@18005 slave 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 0 1608464463000 9 connected

2.新增两个Redis实例

我们在原始集群基础上再增加一主(8007)一从(8008),增加节点后的集群参见下图,新增节点用红色方框表示:

IP地址

端口号

角色

主机名

192.168.1.156

8001

master

redis1

192.168.1.156

8004

slave

redis1

192.168.1.157

8002

master

redis2

192.168.1.157

8005

slave

redis2

192.168.1.158

8003

master

redis3

192.168.1.158

8006

slave

redis3

192.168.1.162

8007

master

redis4

192.168.1.162

8008

slave

redis4

在/usr/local/redis-cluster下创建8007和8008文件夹,并拷贝8001文件夹下的redis-8001.conf文件到8007和8008这两个文件夹下:然后修改关于和端口号有关的配置项目:

代码语言:javascript
复制
#先查看有哪些内容会被替换
sed -n "s/8001/8007/p" redis-8007.conf 

port 8007
pidfile /var/run/redis_8007.pid
dir /usr/local/redis-cluster/8007/
cluster-config-file clusterinfo-8007.conf

#替换
sed -i "s/8001/8007/" /usr/local/redis-cluster/8007/redis-8007.conf 
sed -i "s/8001/8008/" /usr/local/redis-cluster/8008/redis-8008.conf 

启动8007和8008俩个服务并查看服务状态:

代码语言:javascript
复制
redis-server /usr/local/redis-cluster/8007/redis-8007.conf
redis-server /usr/local/redis-cluster/8008/redis-8008.conf 

[root@redis4 redis-5.0.7]# ps -ef | grep redis
root      7422     1  0 19:57 ?        00:00:00 redis-server 0.0.0.0:8007 [cluster]
root      7427     1  0 19:57 ?        00:00:00 redis-server 0.0.0.0:8008 [cluster]
root      7548  2845  0 20:02 pts/1    00:00:00 grep --color=auto redis

3.Redis集群新增8007为master节点

redis集群的命令:

  • 1.create:创建一个集群环境host1:port1 ... hostN:portN
  • 2.call:可以执行redis命令
  • 3.add-node:将一个节点添加到集群里,第一个参数为新节点的ip:port,第二个参数为集群中任意一个已经存在的节点的ip:port
  • 4.del-node:移除一个节点
  • 5.reshard:重新分片
  • 6.check:检查集群状态

使用add-node命令新增一个主节点8007(master),前面的ip:port为新增节点,后面的ip:port为已知存在节点,看到日志最后有"[OK] New node added correctly"提示代表新节点加入成功:

代码语言:javascript
复制
[root@redis1 redis-cluster]# redis-cli -a Root@123 --cluster add-node 192.168.1.162:8007 192.168.1.156:8001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.1.162:8007 to cluster 192.168.1.156:8001
>>> Performing Cluster Check (using node 192.168.1.156:8001)
M: 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006
   slots: (0 slots) slave
   replicates 3a0df944fae9502fb3ec70c19913c842f9fab5a9
S: 0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004
   slots: (0 slots) slave
   replicates 242b7c122850cc358348fba56de9d685eb2d708d
M: 242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192.168.1.157:8005
   slots: (0 slots) slave
   replicates 3e92d8cbd797437eba0a5644b11a993c4bcc8f95
[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.1.162:8007 to make it join the cluster.
[OK] New node added correctly.

查看集群状态:

代码语言:javascript
复制
[root@redis1 redis-cluster]# redis-cli -p 8001 -a Root@123 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:8001> CLUSTER NODES
93fc44270a07f3ace5dcbdb959ec8272accb0f9d 192.168.1.162:8007@18007 master - 0 1608465961464 0 connected
2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006@18006 slave 3a0df944fae9502fb3ec70c19913c842f9fab5a9 0 1608465960000 6 connected
3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001@18001 myself,master - 0 1608465959000 9 connected 0-5460
0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004@18004 slave 242b7c122850cc358348fba56de9d685eb2d708d 0 1608465962000 4 connected
242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003@18003 master - 0 1608465958455 3 connected 10923-16383
3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002@18002 master - 0 1608465962467 2 connected 5461-10922
3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192.168.1.157:8005@18005 slave 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 0 1608465961000 9 connected

注意:当添加节点成功以后,新增的节点不会有任何数据,也不能写入数据,因为它还没有分配任何的slot(hash槽),我们需要为新节点手工分配hash槽:

使用redis-cli命令为8007分配hash槽,找到集群中的任意一个主节点,对其进行重新分片工作。

代码语言:javascript
复制
[root@redis1 redis-cluster]#redis-cli -a Root@123 --cluster reshard 192.168.1.156:8001
输出如下:
... ...

#需要多少个槽移动到新的节点上,自己设置,比如600个hash槽
How many slots do you want to move (from 1 to 16384)? 600 
#把这600个hash槽移动到新增加的节点上去,需要指定节点id
What is the receiving node ID? 93fc44270a07f3ace5dcbdb959ec8272accb0f9d 
#输入all为从所有主节点(8001,8002,8003)中分别抽取相应的槽数指定到新节点中,抽取的总槽数为600个
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:all
#输入yes确认开始执行分片任务
Do you want to proceed with the proposed reshard plan (yes/no)? yes 

查看最新的集群状态:现在我们的8007已经有hash槽了(0-198 5461-5661 10923-11121),也就是说可以在8007上进行读写数据啦!到此为止我们的8007已经加入到集群中,并且 是主节点(Master)

代码语言:javascript
复制
[root@redis1 redis-cluster]# redis-cli -p 8001 -a Root@123 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:8001> CLUSTER NODES
93fc44270a07f3ace5dcbdb959ec8272accb0f9d 192.168.1.162:8007@18007 master - 0 1608466270000 10 connected 0-198 5461-5661 10923-11121
2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006@18006 slave 3a0df944fae9502fb3ec70c19913c842f9fab5a9 0 1608466269390 6 connected
3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001@18001 myself,master - 0 1608466269000 9 connected 199-5460
0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004@18004 slave 242b7c122850cc358348fba56de9d685eb2d708d 0 1608466265000 4 connected
242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003@18003 master - 0 1608466268000 3 connected 11122-16383
3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002@18002 master - 0 1608466271397 2 connected 5662-10922
3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192.168.1.157:8005@18005 slave 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 0 1608466270393 9 connected

4.添加从节点8008到集群

添加从节点8008到集群中去并查看集群状态:

代码语言:javascript
复制
[root@redis1 redis-cluster]#redis-cli -a Root@123 --cluster add-node 192.168.1.162:8008 192.168.1.156:8001

可以看到此时新加入的8008节点也是master,并且没有被分配任何的hash槽。

代码语言:javascript
复制
127.0.0.1:8001> CLUSTER NODES
93fc44270a07f3ace5dcbdb959ec8272accb0f9d 192.168.1.162:8007@18007 master - 0 1608466486027 10 connected 0-198 5461-5661 10923-11121
2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006@18006 slave 3a0df944fae9502fb3ec70c19913c842f9fab5a9 0 1608466483018 6 connected
3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001@18001 myself,master - 0 1608466480000 9 connected 199-5460
0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004@18004 slave 242b7c122850cc358348fba56de9d685eb2d708d 0 1608466485023 4 connected
242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003@18003 master - 0 1608466484000 3 connected 11122-16383
3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002@18002 master - 0 1608466484021 2 connected 5662-10922
a9a008c49990397fccf796d5713c4df1ebf51bca 192.168.1.162:8008@18008 master - 0 1608466487029 0 connected
3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192.168.1.157:8005@18005 slave 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 0 1608466483000 9 connected

我们需要执行replicate命令来指定当前节点(从节点)的主节点id为哪个,首先需要连接新加的8008节点的客户端,然后使用集群命令进行操作,把当前的8008(slave)节点指定到一个主节点下(这里使用之前创建的8007主节点)

代码语言:javascript
复制
[root@redis4 ~]# redis-cli -a Root@123 -c -h 192.168.1.162 -p 8008
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
#后面这串id为8007的节点id
192.168.1.162:8008> CLUSTER REPLICATE 93fc44270a07f3ace5dcbdb959ec8272accb0f9d  
OK

查看集群状态,8008节点已成功添加为8007节点的从节点:

代码语言:javascript
复制
127.0.0.1:8001> CLUSTER NODES
93fc44270a07f3ace5dcbdb959ec8272accb0f9d 192.168.1.162:8007@18007 master - 0 1608466652517 10 connected 0-198 5461-5661 10923-11121
2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006@18006 slave 3a0df944fae9502fb3ec70c19913c842f9fab5a9 0 1608466651000 6 connected
3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001@18001 myself,master - 0 1608466650000 9 connected 199-5460
0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004@18004 slave 242b7c122850cc358348fba56de9d685eb2d708d 0 1608466651514 4 connected
242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003@18003 master - 0 1608466649507 3 connected 11122-16383
3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002@18002 master - 0 1608466651515 2 connected 5662-10922
a9a008c49990397fccf796d5713c4df1ebf51bca 192.168.1.162:8008@18008 slave 93fc44270a07f3ace5dcbdb959ec8272accb0f9d 0 1608466653519 10 connected
3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192.168.1.157:8005@18005 slave 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 0 1608466650512 9 connected

Redis高可用集群缩容

1.删除8008从节点

用del-node删除从节点8008,指定删除节点ip和端口,以及节点id

代码语言:javascript
复制
[root@redis1 redis-cluster]# redis-cli -a Root@123 --cluster del-node 192.168.1.162:8008 a9a008c49990397fccf796d5713c4df1ebf51bca
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node a9a008c49990397fccf796d5713c4df1ebf51bca from cluster 192.168.1.162:8008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

再次查看集群状态,如下图所示,8008这个slave节点已经移除,并且该节点的redis服务也已被停止:

代码语言:javascript
复制
127.0.0.1:8001> CLUSTER NODES
93fc44270a07f3ace5dcbdb959ec8272accb0f9d 192.168.1.162:8007@18007 master - 0 1608466920292 10 connected 0-198 5461-5661 10923-11121
2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006@18006 slave 3a0df944fae9502fb3ec70c19913c842f9fab5a9 0 1608466922298 6 connected
3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001@18001 myself,master - 0 1608466918000 9 connected 199-5460
0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004@18004 slave 242b7c122850cc358348fba56de9d685eb2d708d 0 1608466923300 4 connected
242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003@18003 master - 0 1608466921000 3 connected 11122-16383
3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002@18002 master - 0 1608466921297 2 connected 5662-10922
3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192.168.1.157:8005@18005 slave 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 0 1608466924303 9 connected

#8008已经停止
[root@redis4 ~]# ps -ef | grep redis
root      7422     1  0 19:57 ?        00:00:04 redis-server 0.0.0.0:8007 [cluster]
root      7746  2845  0 20:24 pts/1    00:00:00 grep --color=auto redis

2.删除8007主节点

最后,我们尝试删除之前加入的主节点8007,这个步骤相对比较麻烦一些,因为主节点的里面是有分配了hash槽的,所以我们这里必须 先把8007里的hash槽放入到其他的可用主节点中去,然后再进行移除节点操作,不然会出现数据丢失问题(目前只能把master的数据迁移到一个节点上,暂时做不了平均分配功能),执行命令如下:

代码语言:javascript
复制
[root@redis1 redis-cluster]# redis-cli -a Root@123 --cluster reshard 192.168.1.162:8007

输出如下:
... ...
#将8007的600个slot移给8001
How many slots do you want to move (from 1 to 16384)? 600
#这里是需要把数据移动到哪?8001的主节点id
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: 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 
#这里是需要数据源,也就是我们的8007节点id
Source node 1: 93fc44270a07f3ace5dcbdb959ec8272accb0f9d
#这里直接输入done 开始生成迁移计划
Source node 2:done
#这里输入yes开始迁移
Do you want to proceed with the proposed reshard plan (yes/no)? Yes

至此,我们已经成功的把8007主节点的数据迁移到8001上去了,我们可以看一下现在的集群状态如下图,你会发现8007下面已经没有任 何hash槽了,证明迁移成功:

代码语言:javascript
复制
127.0.0.1:8001> CLUSTER NODES
93fc44270a07f3ace5dcbdb959ec8272accb0f9d 192.168.1.162:8007@18007 master - 0 1608467363000 10 connected
2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006@18006 slave 3a0df944fae9502fb3ec70c19913c842f9fab5a9 0 1608467366594 6 connected
3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001@18001 myself,master - 0 1608467362000 11 connected 0-5661 10923-11121
0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004@18004 slave 242b7c122850cc358348fba56de9d685eb2d708d 0 1608467365592 4 connected
242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003@18003 master - 0 1608467365000 3 connected 11122-16383
3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002@18002 master - 0 1608467367597 2 connected 5662-10922
3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192.168.1.157:8005@18005 slave 3e92d8cbd797437eba0a5644b11a993c4bcc8f95 0 1608467364000 11 connected

最后我们直接使用del-node命令删除8007主节点即可:

代码语言:javascript
复制
[root@redis1 redis-cluster]# redis-cli -a Root@123 --cluster del-node 192.168.1.162:8007 93fc44270a07f3ace5dcbdb959ec8272accb0f9d
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 93fc44270a07f3ace5dcbdb959ec8272accb0f9d from cluster 192.168.1.162:8007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

查看集群状态,一切还原为最初始状态啦:

代码语言:javascript
复制
127.0.0.1:8001> CLUSTER NODES
2cc4af205b6406a95721ea586510b824737965bc 192.168.1.158:8006@18006 slave 3a0df944fae9502fb3ec70c19913c842f9fab5a9 0 1608467437804 6 connected
3e92d8cbd797437eba0a5644b11a993c4bcc8f95 192.168.1.156:8001@18001 myself,master - 0 1608467435000 11 connected 0-5661 10923-11121
0dacc43e6066387db96776128826871bb94a7c05 192.168.1.156:8004@18004 slave 242b7c122850cc358348fba56de9d685eb2d708d 0 1608467436000 4 connected
242b7c122850cc358348fba56de9d685eb2d708d 192.168.1.158:8003@18003 master - 0 1608467438808 3 connected 11122-16383
3a0df944fae9502fb3ec70c19913c842f9fab5a9 192.168.1.157:8002@18002 master - 0 1608467437000 2 connected 5662-10922
3f1bc963a1494e1e497371d5e5e4ca45b23c6cea 192
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-02-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Se7en的架构笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Redis集群模式和哨兵模式比较
  • Redis高可用集群搭建
  • 1.创建相关文件夹
  • 2.修改配置文件
  • 3.创建集群
  • 4.验证集群
    • 集群状态验证
      • 数据操作验证
      • 5.关闭集群
        • 关闭集群
          • 重新开启集群
          • Redis高可用集群水平扩展
            • 1.查看扩容前面集群状态
              • 2.新增两个Redis实例
                • 3.Redis集群新增8007为master节点
                  • 4.添加从节点8008到集群
                  • Redis高可用集群缩容
                    • 1.删除8008从节点
                      • 2.删除8007主节点
                      相关产品与服务
                      云数据库 Redis®
                      腾讯云数据库 Redis®(TencentDB for Redis®)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档