您现在的位置是:首页 > 文章 > Redis 主从、哨兵、集群搭建 网站文章

Redis 主从、哨兵、集群搭建

孙玉超 2021-02-23 14:09:17 评论 457 浏览 收藏 0 赞


Redis主从搭建


其实对于一般公司来说单机版 Redis 已经足足够用,毕竟官方给出了单机 10w 的QPS。但是也要为以后做扩展,而且单机版 Redis 有单点故障问题,挂了之后所有请求都打到 DB 上,就完蛋了。所以可以选择主从模式。

主从模式:简单来说就是主库用来读写,从库用来读,主库写完或者更新完会同步到从库,一方面缓解并发请求压力,另一方面解决单机 Redis 挂掉服务停止不可用问题。


主从模式有两种类型,如下图。当 slave 第一次连接到 master 会进行全量同步,将 master 的数据同步到 slave,后续每一次 master 写入数据,会进行增量同步。



本次示例搭建一主两从(第一种类型),6379为主库,6380和6381端口为从库。首先在根目录下创建文件夹,下载并安装 redis

mkdir software
cd software
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
yum install -y gcc          #redis需要安装gcc依赖
tar -zxvf redis-5.0.5.tar.gz           #解压
cd redis-5.0.5/
make                #安装redis

Redis安装成功,创建文件夹存放主从库配置文件

mkdir redis
mkdir redis/log  #存放日志
mkdir redis/data #存放持久化数据
cp /software/redis-5.0.5/redis.conf /software/redis/redis-6379.conf
cp /software/redis-5.0.5/redis.conf /software/redis/redis-6380.conf
cp /software/redis-5.0.5/redis.conf /software/redis/redis-6381.conf

然后修改配置文件内容,6379主库的配置文件

bind 0.0.0.0 # 注释掉或配置成0.0.0.0表示任意IP均可访问。
protected-mode no # 关闭保护模式,使用密码访问。
port 6379  # 设置端口,6380、6381依次为6380、6381。
timeout 30 # 客户端连接空闲多久后断开连接,单位秒,0表示禁用
daemonize yes # 在后台运行
pidfile /var/run/redis_6379.pid  # pid进程文件名,6380、6381依次为redis_6380.pid、redis_6381.pid
logfile /root/reids/log/6379.log # 日志文件,6380、6381依次为6380.log、6381.log
save 900 1 # 900s内至少一次写操作则执行bgsave进行RDB持久化
save 300 10
save 60 10000 
rdbcompression yes #是否对RDB文件进行压缩,建议设置为no,以(磁盘)空间换(CPU)时间
dbfilename dump.rdb # RDB文件名称
dir /software/redis/data # RDB文件保存路径,AOF文件也保存在这里
appendonly yes # 表示使用AOF增量持久化的方式
appendfsync everysec # 可选值 always, everysec,no,建议设置为everysec
requirepass 123456 # 设置密码
masterauth 123456 #(注意这里requirepass 和 masterauth 主从配置文件都要配,不然哨兵模式 master 挂掉之后其他从节点认证会报错)

从库中除了修改上述注释部分之外,还要添加

slaveof 127.0.0.1 6379 
slave-read-only yes #从库只能读操作


注意以上配置后面的#注释不要一起写到配置文件,或者放在配置上面一行,不要紧跟在配置后面。然后依次启动 redis节点

./redis-server /software/redis/redis-6379.conf
./redis-server /software/redis/redis-6380.conf
./redis-server /software/redis/redis-6381.conf

使用 netstat -ntlup 查看是否启动成功,或者通过 redis 日志查看。

启动成功之后用可视化界面连接,如果连接不上检查安全组或者防火墙,端口是否开放。


链接主库添加 key-value ,发现两个从库都同步了数据,修改主库的 key-value,发现两个从库更新了数据。


缺点:当 master 挂掉之后服务不可用,slave 只会等待,并不能变为主节点。所以需要人工重启 master 或者将 slave 变为 master,总之需要人工干预。试想,如果在凌晨三点 master 宕机,那么运维人员要起来重新恢复,这很显然是不太友好的。


哨兵模式


哨兵模式在主从的基础上做了一次升级,当 master 节点挂掉,slave 节点中会选举一个重新作为 master 节点,新的 master 节点会与其他从节点重新建立主从复制关系。

sentinel 持续监控主从节点的健康,当主节点挂掉之后,新的 master 节点会与其他从节点重新建立主从复制关系。如果后续将已宕机的 master 节点重启,原先的 master 将变为 slave。

创建哨兵模式的配置文件

mkdir /software/redis/sentinel
touch /software/redis/sentinel/sentinel1.conf
touch /software/redis/sentinel/sentinel2.conf
touch /software/redis/sentinel/sentinel3.conf

分别添加配置

daemonize yes # 在后台运行
protect-mode no
sentinel monitor mymaster 127.0.0.1 6379 1 # 给master起一个名字mymaster,并且配置master的ip和端口
sentinel auth-pass mymaster 123456 # master的密码
port 26379 #另外两个配置26380,26381端口
sentinel down-after-milliseconds mymaster 3000 # 3s未回复PING就认为master主观下线
sentinel parallel-syncs mymaster 2  # 执行故障转移时,最多可以有2个slave实例在同步新的master实例
sentinel failover-timeout mymaster 100000 # 如果在10s内未能完成故障转移操作认为故障转移失败

启动哨兵

./redis-server /software/redis/sentinel/sentinel1.conf --sentinel
./redis-server /software/redis/sentinel/sentinel2.conf --sentinel
./redis-server /software/redis/sentinel/sentinel3.conf --sentinel

成功之后可以 kill 掉 6379 的 master 节点,然后用命令登录其他两个 slave 节点,会发现其中一个的角色已经变成了 master。

kill -9 17630
./redis-cli -p 6380 -a 123456
info replication

当哨兵模式主节点挂掉之后,重新启动并不会变成主节点, 而是变为从节点,如果自我学习搭建的时候想恢复 6379 为主节点,需要在 redis-6379.conf 和另一个从节点配置文件里面删掉以下配置,重新启动三个节点

replicaof 127.0.0.1 6381


集群模式


相对来说,Redis 集群搭建比较简单。该例重新配置文件不按照主从的配置文件做修改。先安装 Redis 新建文件夹,然后将 redis.conf 复制过来

mkdir /usr/local/software/redis
cd /usr/local/software/redis
用 mv 命令将 redis.conf 复制过来,并且改名为 redis-6379.conf

修改 redis-6379.conf 配置

#bind 127.0.0.1
port 6379# 本示例6个节点端口分别为6379、6380、6381、6382、6383、6384
daemonize yes # r后台运行 
protect-mode no
pidfile /var/run/redis_6379.pid # 分别对应6379、6380、6381、6382、6383、6384
cluster-enabled yes # 开启集群模式 
masterauth 123456# 如果设置了密码,需要指定master密码
requirepass 123456
save 900 1
save 300 10
save 60 10000
appendonly yes
cluster-config-file nodes_6379.conf # 集群的配置文件,同样对应6379、6380、6381、6382、6383、6384六个节点

将此文件复制 5 份放在同目录下

cp redis-6379.conf redis-6380.conf
cp redis-6379.conf redis-6381.conf
cp redis-6379.conf redis-6382.conf
cp redis-6379.conf redis-6383.conf
cp redis-6379.conf redis-6384.conf

按照上述注释,依次修改配置文件端口进程文件名等。

启动六个 Redis 节点

./redis-server /usr/local/software/redis/redis-6379.conf
./redis-server /usr/local/software/redis/redis-6380.conf
./redis-server /usr/local/software/redis/redis-6381.conf
./redis-server /usr/local/software/redis/redis-6382.conf
./redis-server /usr/local/software/redis/redis-6383.conf
./redis-server /usr/local/software/redis/redis-6384.conf

连接集群

./redis-cli --cluster create --cluster-replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381  127.0.0.1:6382  127.0.0.1:6383  127.0.0.1:6384  -a 123456

看到以下的图集群就成功了,然后用 RedisDesktop 连接集群节点,标志都变成了集群标志。可以用 redis-cli 命令进入节点查看,三个主节点分别挂着三个 slave 节点



文章参考 https://zhuanlan.zhihu.com/p/194143258


转载请注明出处:转载请注明出处

上一篇 : SpringCloud 组件之 Gateway 基础 下一篇 : 还在用 DateTime?我们已经用 LocalDateTime 啦!

暮色妖娆丶

96 年草根站长,2019 年 7 月接触互联网踏入 Java 开发岗位,喜欢前后端技术。对技术有强烈的渴望,2019 年 11 月正式上线自己的个人博客