当前位置:网站首页>Redis集群、一主二从三哨兵的搭建
Redis集群、一主二从三哨兵的搭建
2022-07-17 12:49:00 【努力敲键盘ing】
Redis集群至少需要3个master节点,1个master节点需要对应一个slave节点,所以redis集群至少需要6个节点
至少需要3个maser节点原因:master节点的选举需要大半数的集群master节点同意才能选举成功,如果只有2个master节点,当其中一个挂了,是达不到选举新master的条件的
推荐节点数为奇数的原因:举个例子:3个master节点与4个master节点,同时都有2个master节点宕机,那么都不能进行选举新的master节点,但是3个master节点
Redis集群的搭建
首先在自己的服务器上下载Redis
参考文档:
CentOS7(Linux)源码安装Redis - 腾讯云开发者社区-腾讯云
(最后可以先不用设置密码,如果设置了,进入redis时不要忘记输入密码)
环境搭建好了之后,就可以进行对redis单机集群的搭建了,因为是单机上,所以多创建几个redis节点,修改端口号(7001-7006),来充当集群节点
1.我这里都是在/usr/local目录下进行的,创建一个redis-cluster文件夹,为了方便区分创建相对应端口号的文件夹

2.将redis中bin目录复制到每个文件夹中

bin目录中上面aof、rdb是持久化文件,在集群搭建成功之前请先删除,nodes-7001.conf是集群搭建成功后自动生成的
3.然后进行对每个端口号里面的redis.conf配置文件进行修改
port:修改为相对应的端口号
cluster-enabled:修改为yes(redis服务器在启动时会根据cluster-enabled配置选项是否为yes来决定是否开启服务器的集群模式)
cluster-config-file:修改为相对应的端口号
pidfile:修改为相对应的端口号文件(服务器记录守护程序的进程id的文件)
protected-mode:修改为no(关闭protected-mode模式,此时外部网络可以直接访问;开启protected-mode保护模式后,需配置bind ip或者设置访问密码)
4.修改完配置文件后,启动7001-7006的redis节点:./redis-server redis.conf
这里写了一个sh脚本用于快捷启动多个redis,内容如下
cd 7001/bin
./redis-server redis.conf
cd ../../
cd 7002/bin
./redis-server redis.conf
cd ../../
cd 7003/bin
./redis-server redis.conf
cd ../../
cd 7004/bin
./redis-server redis.conf
cd ../../
cd 7005/bin
./redis-server redis.conf
cd ../../
cd 7006/bin
./redis-server redis.conf
cd ../../
别忘了给脚本执行权限
chmod +x start-all.sh解读:就是cd切换到不同的端口号目录下,然后使用命令启动redis
5.可以使用 ps aux | grep redis来查看redis是否启动

6.以上就是对redis节点的创建,然后切换到redis目录中bin文件下进行集群的搭建
执行命令:
./redis-cli --cluster create --cluster-replicas 1 110.40.154.201:7001 110.40.154.201:7002 110.40.154.201:7003 110.40.154.201:7004 110.40.154.201:7005 110.40.154.201:7006
中间的1表示比例,这样创建成功后,会默认是一主一从的形式

执行完之后,中间会让你输入yes

最后集群搭建成功
如果输入完yes后中间一直是等待,则可能你的端口号没有全部开启,再开启7001-7006的同时也需要将17001-17006开启:redis集群不仅需要开通redis客户端连接的端口,而且需要开通集群总线端口,集群总线端口为redis客户端连接的端口 + 10000
redis集群 Waiting for the cluster to join 一直等待_海拉姆的博客-CSDN博客
以上就是对集群搭建的过程,最后可以测试一下,在7001的redis上存储,看是否能在其他节点上获取
一主二从三哨兵搭建

直接在redis的bin目录下进行配置,其中8001是主节点,8002、8003是从节点
redis.conf的修改port、dbfilename、pidfile为对应的端口号
启动三个redis服务
./redis-server redis8001.conf &
可以开启三个客户端分别连接不同的redis
./redis-cli -h 127.0.0.1 -p 8001
(如果设置了密码,别忘了输入密码: auth 密码)
在8002、8003中输入
slaveof 127.0.0.1 8001 //将8002、8003设置为8001的从库
回到8001中输入
info replication //查看主从关系

slaveof no one //表示断开与主节点的主从关系
以上就搭建好了一主二从,接下来是三哨兵
也是在redis的bin目录下进行配置

分别修改红框里的文件,redis自带有一个哨兵文件redis-sentinel,然后复制出来三个就好
port 26379 //一次加一 别忘了开端口
protected-mode no
daemonize no
sentinel monitor mymaster 127.0.0.1 8001 2
哨兵的配置文件可以参考这个博客
Redis学习之哨兵模式配置文件详解_倪家李子的博客-CSDN博客_redis哨兵模式配置文件
然后分别开启这三个哨兵
./redis-sentinel sentinel1.conf
到此三哨兵就搭建完毕
最后进行测试
8001正常时:
8001的主从信息:

8002的主从信息:

8003的主从信息

模拟8001宕机
./redis-cli -h 127.0.0.1 -p 8001 shutdown
8003的主从信息:

8002的主从信息:

三个哨兵通过选举,将8003选举成为新的master (一下是三个哨兵进行的操作,看不懂。。。)



边栏推荐
- Bidirectional NAT Technology
- Crud code practice of user management based on koa2 + MySQL
- C# SerialPort配置和属性了解
- 2022 Shaanxi secondary vocational group "Cyberspace Security" - packet analysis
- Figure an introduction to the interpretable method of neural network and a code example of gnnexplainer interpreting prediction
- NJCTF 2017messager
- koa2 连接 mysql 数据库实现增删改查操作
- Idea display service port --service
- [csp-j 2021] summary
- 选择比努力更重要
猜你喜欢

【PostgreSQL 】PostgreSQL 15对distinct的优化

Aike AI frontier promotion (7.17)
![[Niuke swipe questions] / *c language realizes left-hand rotation of strings*/](/img/74/681975d85a671b4f75f2b392264105.png)
[Niuke swipe questions] / *c language realizes left-hand rotation of strings*/

Know what it is, and know why, JS object creation and inheritance

Attachment handling of SAP Fiori

Analysis of Web Remote Code Execution Vulnerability of Zhongke panyun-d module

C语言自定义类型详解

NAT技术及NAT ALG

国产旗舰手机价格泡沫严重,二手比新机更划算,要不然就买iPhone

多元线性回归详解
随机推荐
How to solve the problem of cross domain access by Google browser
【华为云IoT】读书笔记之《万物互联:物联网核心技术与安全》第3章(下)
Convert excel table to word table, and keep the formula in Excel table unchanged
Job: enter an odd number of 1-100
C语言之构造类型细讲
Structure the combat battalion | module 7
Autojs learning - Dynamic decryption
R language uses the ordinal of epidisplay package or. The display function obtains the summary statistical information of the ordered logistic regression model (the odds ratio and its confidence inter
LVI-SAM:激光-IMU-相机紧耦合建图
idea展示服务端口--service
双向NAT技术
KunlunBase 线上Meetup等您来~
c# treeView 树形结构递归处理(企业集团型层次树形展示)
Hcip day 1 7.15
因果学习将开启下一代AI浪潮?九章云极DataCanvas正式发布YLearn因果学习开源项目
NAT技术及NAT ALG
开发第一个Flink应用
Design and Simulation of intelligent storage cabinet control system
爱可可AI前沿推介(7.17)
koa2 连接 mysql 数据库实现增删改查操作