redis一主两从三哨兵

该文详细介绍了在CentOS7环境下,如何安装Redis5.0.4,包括下载编译、修改配置、启动服务,并设置了主从复制。随后,搭建了一主两从的Redis集群,确保数据的冗余和故障转移。最后,配置了哨兵模式,实现对Redis集群的监控和自动故障恢复,增强了系统的高可用性。
摘要由CSDN通过智能技术生成

一、环境

yum源:阿里源
系统:centos7
CPU:1核
内存:1G
redis版本:5.0.4
主:192.168.2.44
从:192.168.2.45
从:192.168.2.46

二、安装

2.1下载安装包

首先下载软件包,把软件包上传到机器

[root@localhost ~]# wget https://download.redis.io/releases/redis-5.0.4.tar.gz

或者从官网直接下载,然后上传到机器上
redis下载地址:https://download.redis.io/releases/
在这里插入图片描述

2.2编译安装

tar -xf redis-5.0.4.tar.gz          
mv redis-5.0.4 /opt/                                 #解压缩该压缩包并将其移动到 /opt 目录下。
cd /opt/redis-5.0.4
make											     #编译 Redis 5.0.4。

2.3修改配置文件

[root@localhost redis-5.0.4]# sed -i s'/127.0.0.1/0.0.0.0/g' redis.conf 
[root@localhost redis-5.0.4]# sed -i s'/daemonize no/daemonize yes/g' redis.conf  
[root@localhost redis-5.0.4]# sed -i s'/appendonly no/appendonly yes/g' redis.conf
[root@localhost redis-5.0.4]# sed -i s'/# requirepass foobared/requirepass 123456/g' redis.conf 

这些代码使用 sed 命令修改 Redis 配置文件 redis.conf 中的参数。分别将绑定地址(bind address)改为 0.0.0.0,启用守护进程模式(daemonize),启用只追加模式(append-only),以及将密码设置为 “123456”.

2.4启动redis

[root@localhost redis-5.0.4]# /opt/redis-5.0.4/src/redis-server /opt/redis-5.0.4/redis.conf
5758:C 24 May 2023 01:12:59.890 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5758:C 24 May 2023 01:12:59.891 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5758, just started
5758:C 24 May 2023 01:12:59.891 # Configuration loaded
[root@localhost redis-5.0.4]# echo "/opt/redis-5.0.4/src/redis-server /opt/redis-5.0.4/redis.conf" >>/etc/rc.local
[root@localhost redis-5.0.4]# ps aux|grep redis
root       5759  0.0  0.7 153988  7624 ?        Ssl  01:12   0:00 /opt/redis-5.0.4/src/redis-server 0.0.0.0:6379
root       5764  0.0  0.0 112808   964 pts/0    R+   01:13   0:00 grep --color=auto redis
[root@localhost redis-5.0.4]# 

启动redis并且把redis启动命令添加到 /etc/rc.local 文件中,以便在系统重启后自动启动 Redis 服务器。

2.5登录redis

[root@localhost redis-5.0.4]# /opt/redis-5.0.4/src/redis-cli -h 127.0.0.1 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> 

连接到本地主机(127.0.0.1)的 Redis 服务器端口(6379),并使用密码 “123456” 进行身份验证。
三台机器都按照上面的方式部署好以后,接着就是搭建主从。

三、搭建一主两从

3.1修改配置文件

主库:192.168.2.44
vim /opt/redis-5.0.4/redis.conf
port 16379
logfile "/opt/redis-5.0.4/redis.log"

从库:192.168.2.45
vim /opt/redis-5.0.4/redis.conf
port 16379
logfile "/opt/redis-5.0.4/redis.log"
slaveof 192.168.2.44 16379			    #主要就是从库添加这个参数,指定主库的ip和端口
masterauth 123456								#主库上有密码的话,需要在从库添加这个参数
	
从库:192.168.2.46
vim /opt/redis-5.0.4/redis.conf
port 16379
logfile "/opt/redis-5.0.4/redis.log"
slaveof 192.168.2.44 16379			    #主要就是从库添加这个参数,指定主库的ip和端口
masterauth 123456	

3.2重启redis

[root@localhost ~]# /opt/redis-5.0.4/src/redis-cli -h 127.0.0.1 -p 6379 -a 123456 shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[root@localhost ~]# /opt/redis-5.0.4/src/redis-server /opt/redis-5.0.4/redis.conf

3.3测试主从

主库192.168.2.44上执行:
[root@localhost redis-5.0.4]# /opt/redis-5.0.4/src/redis-cli -h 127.0.0.1 -p 16379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:16379> get name
(nil)
127.0.0.1:16379> set name hw
OK
127.0.0.1:16379> get name
"hw"

192.168.2.45上执行:
[root@localhost ~]# /opt/redis-5.0.4/src/redis-cli -h 127.0.0.1 -p 16379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:16379> get name 
(nil)
127.0.0.1:16379> get name 
"hw"

192.168.2.46上执行:
[root@localhost ~]# /opt/redis-5.0.4/src/redis-cli -h 127.0.0.1 -p 16379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:16379> get name
(nil)
127.0.0.1:16379> get name
"hw"

这里可以看到,从库上第一次没有获取到name的值,但是第二次获取到了值跟主库上的一样。

[root@localhost ~]# /opt/redis-5.0.4/src/redis-cli -h 127.0.0.1 -p 16379 -a 123456
127.0.0.1:16379> info replication
#Replication
role:master
connected_slaves:2
slave0:ip=192.168.2.45,port=16379,state=online,offset=84,lag=0
slave1:ip=192.168.2.46,port=16379,state=online,offset=84,lag=0
master_replid:d4dc8b307660346b3135181048dff74a47331a27
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:84
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:84

参数含义:
role: master:表示当前服务器的角色是主服务器。
connected_slaves: 2:表示当前主服务器连接了两个从服务器。
slave0:ip=192.168.2.45,port=16379,state=online,offset=84,lag=0:表示从服务器0的IP地址是192.168.2.45,端口号是16379,状态为在线,复制偏移量为84,延迟为0。
slave1:ip=192.168.2.46,port=16379,state=online,offset=84,lag=0:表示从服务器1的IP地址是192.168.2.46,端口号是16379,状态为在线,复制偏移量为84,延迟为0。
master_replid: d4dc8b307660346b3135181048dff74a47331a27:表示主服务器的复制ID。
master_replid2: 0000000000000000000000000000000000000000:表示主服务器的第二复制ID。
master_repl_offset: 84:表示主服务器的复制偏移量。
second_repl_offset: -1:表示第二复制偏移量。
repl_backlog_active: 1:表示复制日志缓冲区处于活动状态。
repl_backlog_size: 1048576:表示复制日志缓冲区的大小。
repl_backlog_first_byte_offset: 1:表示复制日志缓冲区的第一个字节偏移量。
repl_backlog_histlen: 84:表示复制日志缓冲区的历史长度。

所以至此redis一主两从搭建完成。

四、搭建三哨兵

4.1哨兵简介

Redis哨兵模式是一种用于实现Redis高可用性的解决方案。在Redis集群中,通常会有一个或多个主节点和多个从节点。当主节点发生故障时,需要手动将某个从节点升级为新的主节点,以确保服务的持续可用性。这种手动操作显然不够理想,因为需要人工干预,而且容易出错。

为了解决这个问题,Redis提供了哨兵模式。该模式引入了一组哨兵进程,这些进程会自动地监控Redis集群的状态,并在主节点出现故障时自动进行故障转移。具体来说,哨兵进程会定期向Redis集群中的主节点发送PING命令,如果主节点在指定时间内没有回复PING命令,哨兵会将其标记为下线状态。当一个哨兵检测到主节点下线后,它会向其他哨兵发送消息,询问其他哨兵是否也发现了主节点下线。如果大多数哨兵都确认主节点下线,那么其中一个哨兵就会开始进行故障转移操作。

故障转移的具体步骤如下:

哨兵选举:由于哨兵可能存在网络延迟等问题,所以在进行故障转移前,需要进行哨兵选举,确保选出的哨兵具有最新的信息。

主从切换:被选中的哨兵会向所有从节点发送SLAVEOF命令,要求它们成为新的主节点的从节点。同时,被选中的哨兵还会将自己的配置文件中的主节点地址改为新的主节点地址,并通过CONFIG REWRITE命令将其保存到磁盘上。

客户端重定向:当故障转移完成后,客户端可能还会继续向旧的主节点发送请求。为了避免这种情况,被选中的哨兵会向客户端发送一个MOVED错误,告诉客户端新的主节点地址。

总的来说,Redis哨兵模式可以自动地监控Redis集群的状态,并在必要时进行故障转移操作,以确保服务的持续可用性。同时,哨兵还提供了一些其他功能,如配置重写、通知等,使得Redis集群更加健壮和灵活。

4.2哨兵部署

redis-sentinel:
10.0.2.44:9000
10.0.2.45:9000
10.0.2.46:9000

[root@localhost ~]# cd /opt/redis-5.0.4/
[root@localhost redis-5.0.4]# ls
00-RELEASENOTES  CONTRIBUTING  dump.rdb  MANIFESTO   redis.log        runtest-sentinel  tests
appendonly.aof   COPYING       INSTALL   README.md   runtest          sentinel.conf     utils
BUGS             deps          Makefile  redis.conf  runtest-cluster  src

可以看到redis目录下面有一个sentinel.conf,然后删除里面之前的配置,把下面的配置参数粘贴进去

[root@localhost redis-5.0.4]# vim sentinel.conf
port 9000																					#监听端口9000上的请求
daemonize yes																		#将Redis Sentinel进程作为守护进程运行,以提高稳定性和安全性
bind 0.0.0.0																				#监听所有网络接口上的请求
dir /opt/redis-5.0.4/																	#指定Redis Sentinel运行时所需的工作目录
logfile "sentinel-9000.log"														#指定Redis Sentinel运行时所需的工作目录
sentinel monitor mymaster 192.168.2.44 16379 2					#监控名为mymaster的Redis实例,IP地址为192.168.2.44,端口号为16379,其中2表示该实例连续两次无响应时,认为该实例不可用。
sentinel down-after-milliseconds mymaster 30000					#当mymaster实例在30s内没有回复Sentinel的请求时,判断该实例已经下线
sentinel parallel-syncs mymaster 1											#在进行故障转移时,同时同步数据的从节点数量
sentinel auth-pass mymaster 123456								#连接到mymaster实例需要使用的密码
sentinel failover-timeout mymaster 180000							#进行故障转移的超时时间为180s。
sentinel deny-scripts-reconfig yes											#禁止运行时动态重载Lua脚本

启动方式:

/opt/redis-5.0.4/src/redis-sentinel /opt/redis-5.0.4/sentinel.conf

登录方式:

/opt/redis-5.0.4/src/redis-cli -h 192.168.2.44 -p 9000

关闭方式

/opt/redis-5.0.4/src/redis-cli -h 192.168.2.44 -p 9000 shutdown

192.168.2.45:9000配置文件如下:

port 9000
daemonize yes
bind 0.0.0.0
dir /opt/redis-5.0.4/
logfile "sentinel-9000.log"
sentinel monitor mymaster 192.168.2.44 16379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster 123456
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

192.168.2.46:9000配置文件如下:

port 9000
daemonize yes
bind 0.0.0.0
dir /opt/redis-5.0.4/
logfile "sentinel-9000.log"
sentinel monitor mymaster 192.168.2.44 16379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster 123456
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

在这里插入图片描述
可以看到redis的哨兵模式已经启动

4.3查看哨兵

[root@localhost ~]# /opt/redis-5.0.4/src/redis-cli -h 127.0.0.1 -p 9000
127.0.0.1:9000> info sentinel
#Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.2.44:16379,slaves=2,sentinels=3

参数解释:
sentinel_masters: 1:表示当前Sentinel监控的主服务器数量为1。
sentinel_tilt: 0:表示当前Sentinel没有发生故障。
sentinel_running_scripts: 0:表示当前没有正在运行的Sentinel脚本。
sentinel_scripts_queue_length: 0:表示当前Sentinel脚本队列中没有等待执行的脚本。
sentinel_simulate_failure_flags: 0:表示当前没有模拟故障的标志位。
master0:name=mymaster,status=ok,address=192.168.2.44:16379,slaves=2,sentinels=3:表示监控的主服务器的信息。主服务器的名称是mymaster,状态为正常,地址为192.168.2.44:16379,有2个从服务器和3个Sentinel。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值