1、配置(使用三台主机进行配置):
主机 | redis | 哨兵 |
---|---|---|
redis主机 | 192.168.183.138 6379 | 192.168.183.138 26379 |
redis从1 | 192.168.183.139 6379 | 192.168.183.139 26379 |
redis从2 | 192.168.183.140 6379 | 192.168.183.140 26379 |
sentinel端口是26379,sentinel主机既能监控又能提供配置功能,向sentinel指明主redis服务器(仅监控主服务器),sentinel可以从主服务中获取主从架信息,并分辨从节点,sentinel可以监控当前整个主从服务器架构的工作状态,一旦发现master离线的情况,sentinel会从多个从服务器中选择并提升一个从节点成为主节点,当主节点被从节点取代以后,那么IP地址则发生了,客户所连接之前的主节点IP则不无法连接,此时可以向sentinel发起查询请求,sentinel会告知客户端新的主节点的IP,所以sentinel是redis在主从架构中实现高可用的解决方。sentinel为了误判和单点故障,sentinel也应该组织为集群,sentinel多个节点同时监控redis主从架构,一旦有一个sentinel节点发现redis的主节点不在线时,sentinel会与其他的sentinel节点协商其他的sentinel节点是否也为同样发现redis的主节点不在线的情况,如果sentinel的多个点节点都发现redis的主节点都为离线的情况,那么则判定redis主节点为离线状态,以此方式避免误判,同样也避免了单点故障。
(1) # sentinel monitor <master-name> <ip> <redis-port> <quorum> //此项可以出现多次,可以监控多组redis主从架构,此项用于监控主节点 <master-name> 自定义的主节点名称,<ip> 主节点的IP地址,<redis-port>主节点的端口号,<quorum>主节点对应的quorum法定数量,用于定义sentinel的数量,是一个大于值尽量使用奇数,如果sentinel有3个,则指定为2即可,如果有4个,不能够指定为2,避免导致集群分裂,注意,<master-name>为集群名称,可以自定义,如果同时监控有多组redis集群时,<master-name>不能同样
(2) sentinel down-after-milliseconds <master-name> <milliseconds> //sentinel连接其他节点超时时间,单位为毫秒(默认为30秒)
(3)sentinel parallel-syncs <master-name> <numslaves> //提升主服务器时,允许多少个从服务向新的主服务器发起同步请求
(4)sentinel failover-timeout <master-name> <milliseconds> //故障转移超时时间,在指定时间没能完成则判定为失败,单位为毫秒(默认为180秒)
SENTINEL masters //列出所有监控的主服务器
SENTINEL slaves <master name> //获取指定redis集群的从节点
SENTINEL get-master-addr-by-name <master name> //根据指定master的名称获取其IP
SENTINEL reset //用于重置,包括名称,清除服务器所有运行状态,故障转移、等等
SENTINEL failover <master name> //手动向某一组redis集群发起执行故障转移
redis:
vim redis.conf
--------------------------------编辑内容--------------------------------
#端口
port 6383
#绑定IP
bind 192.168.183.138
#关闭保护模式,允许外部访问
protected-mode yes
#授权密码
requirepass xxx
#守护进程,即后台启动
daemonize yes
#进程ID,默认/var/run/redis.pid
pidfile /var/run/redis-6379.pid
#日志
loglevel notice
logfile "/usr/local/src/redis-6.2.4/logs/log-6383.log"
#数据文件
dir /usr/local/src/redis-6.2.4/db/
dbfilename dump-6379.rdb
#数据同步
save 900 1
save 300 10
save 60 10000
#master
masterauth xxxx #密码
vim sentinel.conf
-----------------------------------编辑内容-------------------------------
#端口
port 26379
#绑定IP
bind 192.168.183.138
#关闭保护模式
protected-mode no
#守护进程,后台启动
daemonize yes
#进程ID
pidfile "/var/run/redis-sentinel-26379.pid"
#日志文件
logfile "/usr/local/src/redis-6.2.4/logs/log-26379.log"
dir "/tmp"
#哨兵
#主节点 2表示判断主节点下线需要2个哨兵决定
sentinel monitor mymaster 192.168.183.138 6379 2
#主节点密码
in xxx
[root@localhost redis-6.2.4]# redis-server redis.conf
[root@localhost redis-6.2.4]# redis-sentinel sentinel.conf
[root@localhost redis-6.2.4]# ps -ef | grep redis
root 30889 1 0 15:30 ? 00:00:00 redis-server 192.168.183.138:6379
root 30921 1 0 15:32 ? 00:00:00 redis-sentinel 192.168.183.138:26379 [sentinel]
root 30927 30899 0 15:32 pts/2 00:00:00 grep --color=auto redis
[root@localhost redis-6.2.4]# redis-cli -h 192.168.183.138 -p 26379
192.168.183.138:26379> 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.183.138:6379,slaves=0,sentinels=1
说明:
master0:name=mymaster,status=ok,address=192.168.183.138:6379,slaves=0,sentinels=1
status=ok #主节点已启动
address=192.168.192.128:6383 #主节点地址
slaves=0 #slave服务个数
sentinels=1 #哨兵个数
从1:bind 192.168.183.139
从2:bind 192.168.183.140
分别配置vim redis.conf
--------------------------编辑内容------------------------------------
#端口
port 6379
#绑定IP
bind 192.168.183.139/140
#关闭保护模式,允许外部访问
protected-mode yes
#授权密码
requirepass xxx
#守护进程,即后台启动
daemonize yes
#进程ID,默认/var/run/redis.pid
pidfile /var/run/redis-6379.pid
#日志
loglevel notice
logfile "/usr/local/src/redis-6.2.4/logs/log-6379.log"
#数据文件
dir /usr/local/src/redis-6.2.4/db/
dbfilename dump-6379.rdb
#数据同步
save 900 1
save 300 10
save 60 10000
#master
replicaof 192.168.183.138 6379
masterauth xxx
从1:bind 192.168.183.139
从2:bind 192.168.183.140
分贝配置哨兵配置文件vim sentinel.conf
-----------------------------------编辑内容-------------------------------
#端口
port 26379
#绑定IP
bind 192.168.183.139/140
#关闭保护模式
protected-mode no
#守护进程,后台启动
daemonize yes
#进程ID
pidfile "/var/run/redis-sentinel-26379.pid"
#日志文件
logfile "/usr/local/src/redis-6.2.4/logs/log-26383.log"
dir "/tmp"
#哨兵
#主节点 2表示判断主节点下线需要2个哨兵决定
sentinel monitor mymaster 192.168.192.128 6379 2
#主节点密码
sentinel auth-pass mymaster xxx
从1和从2分别执行下面操作
[root@localhost redis-6.2.4]# redis-server redis.conf
[root@localhost redis-6.2.4]# ps -ef | grep redis
root 30735 1 0 16:02 ? 00:00:00 redis-server 192.168.183.139:6379
root 30741 10028 0 16:02 pts/1 00:00:00 grep --color=auto redis
[root@localhost redis-6.2.4]# redis-sentinel sentinel.conf
[root@localhost redis-6.2.4]# ps -ef | grep redis
root 30735 1 0 16:02 ? 00:00:00 redis-server 192.168.183.139:6379
root 30743 1 0 16:03 ? 00:00:00 redis-sentinel 192.168.183.139:26379 [sentinel]
root 30749 10028 0 16:03 pts/1 00:00:00 grep --color=auto redis
登陆主节点:若slave=0,sentinels=1。说明没有成功
关闭主节点防火墙,重启服务。
[root@localhost ~]# systemctl stop firewalld #关闭防火墙
[root@localhost ~]# systemctl disable firewalld #永久关闭
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
从节点个数变成2,哨兵个数变成3,这样哨兵模式就搭建完成了。
(1)主机主节点下线
主机上查看:主节点ip变成从机1的IP,说明主机已经切换
查看三台服务器的哨兵配置文件。主机都自动变成192.168.183.139。这些修改变化是通过哨兵实现的,不需要人工维护
(2)重启主机redis
在192.168.183.139。上查看以下信息说明主机节点以上线并加入集群,配置成功。
(1)redis开机自启动
[root@localhost bin]# vi /etc/systemd/system/redis.service
[Unit]
# 描述服务
Description=redis-server
# 描述服务类别
After=network.target
# 服务运行参数的设置
[Service]
# 后台运行的形式
Type=forking
# 服务的具体运行命令,服务的启动、重启、停止命令全部要求使用绝对路径且正确
# ExecReload为重启命令,ExecStop为停止命令
ExecStart=/usr/local/bin/redis-server /usr/local/src/redis-6.2.4/redis.conf
# 表示给服务分配独立的临时空间
PrivateTmp=true
# 运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
[Install]
WantedBy=multi-user.target
[root@localhost bin]# systemctl daemon-reload
[root@localhost bin]# systemctl start redis
[root@localhost bin]# systemctl enable redis
[root@localhost bin]# vi /etc/systemd/system/sentinel.service
[Unit]
# 描述服务
Description=Redis-Sentinel
# 描述服务类别
After=network.target
# 服务运行参数的设置
[Service]
# 后台运行的形式
Type=forking
# 服务的具体运行命令,服务的启动、重启、停止命令全部要求使用绝对路径且正确
# ExecReload为重启命令,ExecStop为停止命令
ExecStart=/usr/local/bin/redis-sentinel /usr/local/src/redis-6.2.4/sentinel.conf
# 表示给服务分配独立的临时空间
PrivateTmp=true
# 运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
[Install]
WantedBy=multi-user.target
[root@localhost bin]# systemctl daemon-reload
[root@localhost bin]# systemctl start sentinel
[root@localhost bin]# systemctl enable sentinel
Created symlink from /etc/systemd/system/multi-user.target.wants/sentinel.service to /etc/systemd/system/sentinel.service.
重启后 查看redis和sentinel都已启动说明自启动配置成功。
Redis Sentinel 是用于监控和管理Redis主从复制环境的工具。它自动检测主服务器故障并执行故障转移。
优点:1、自动故障检测和转移。2、高可用性。
缺点:1、需要配置和管理多个哨兵。2、主master故障,切换master响应时间可能较长。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。