文章目录
- Redis 哨兵机制介绍与完整部署验证
- 1.1 介绍
- 1.2 哨兵进程的作用
- 2 哨兵机制部署
- 2.1 主从复制配置
- 2.1.1 单机版安装
- 2.1.2 目录层级配置
- 2.1.5 主机不需要做任何配置
- 2.1.4 从机配置redis.conf
- 2.1.5 验证
- 2.2 sentinel配置
- 2.2.1 目录层级配置
- 2.2.2 sentinel.conf文件配置
- 2.2.3 复制2个sentinel文件
- 2.3.4 修改其他两个sentinel.conf
- 2.3.5 启动关闭哨兵
- 2.3.6 哨兵默认日志输出
- 2.3.7 校验
- 2.3.8 其他哨兵操作指令
- 2.3.9 主从切换操作
- 2.3.9.1 关闭主机6379
- 2.3.9.2 等待1min,在哨兵客户端查看主机信息
- 3 故障判断原理
Redis 哨兵机制介绍与完整部署验证
1.1 介绍
- Sentinel (哨兵)进程是用于监控 Redis 集群中 Master 主服务器工作的状态
- 在 Master 主服务器发生故障的时候,可以实现 Master 和 Slave 服务器的切换,保证系统的高可用。
- 其已经被集成在 redis2.6+ 的版本中, Redis 的哨兵模式到了 2.8 版本之后就稳定了下来。
1.2 哨兵进程的作用
- 监控: 哨兵( sentinel ) 会不断地检查你的 Master 和 Slave 是否运作正常。
- 提醒: 当被监控的某个 Redis 节点出现问题时, 哨兵( sentinel ) 可以通过 API 向管理员或者其他应用程序发送通知。
- 自动故障迁移:当一个 Master 不能正常工作时,哨兵( sentinel ) 会开始一次自动故障迁移操作,具体操作如下:
- 它会将失效 Master 的其中一个 Slave 升级为新的 Master , 并让失效 Master 的其他 Slave 改为复制新
的 Master ; - 当客户端试图连接失效的 Master 时,集群也会向客户端返回新 Master 的地址,使得集群可以使用现在
的 Master 替换失效 Master 。 - Master 和 Slave 服务器切换后, Master 的 redis.conf 、 Slave 的 redis.conf 和 sentinel.conf 的
配置文件的内容都会发生相应的改变,即, Master 主服务器的 redis.conf 配置文件中会多一行
slaveof 的配置, sentinel.conf 的监控目标会随之调换。
2 哨兵机制部署
操作环境:centos7.6 Linux
本次集群:一主 一从 三哨兵
主:6379 从机:6370 哨兵:26379,26380,26381
注意:在规范的集群中,哨兵至少需要三个并且是基数倍,避免集群造成脑裂的情况。
2.1 主从复制配置
哨兵配置之前,需要进行主从复制配置
2.1.1 单机版安装
首先单机版安装参考博客:
2.1.2 目录层级配置
复制master文件夹到slave1中
[root@3b74f16b816c redis]# ls
master slave1
说明:
master:主
slave1:从
2.1.5 主机不需要做任何配置
2.1.4 从机配置redis.conf
注意:master不需要做任何配置
# 修改从机运行端口
port 6370
# profile后缀配置一般与端口一致
pidfile /var/run/redis_6370.pid
# 配置主机 slaveof/replicaof <masterip> <masterport>
slaveof 127.0.0.1 6379
# masterauth主机如果有配置连接密码 需要配置,本次主机有配置密码 需要配置
# 注意 配置哨兵模式之前,如果需要密码,主机需要配置该项
masterauth 123456
2.1.5 验证
- 分别启动master/slave1
./redis-server redis.conf
- 查看主机日志
说明从机数据备份成功
134:M 11 Jan 2021 08:29:40.801 * Ready to accept connections
134:M 11 Jan 2021 08:33:33.702 * Replica 127.0.0.1:6370 asks for synchronization
134:M 11 Jan 2021 08:33:33.702 * Full resync requested by replica 127.0.0.1:6370
134:M 11 Jan 2021 08:33:33.702 * Starting BGSAVE for SYNC with target: disk
134:M 11 Jan 2021 08:33:33.705 * Background saving started by pid 162
162:C 11 Jan 2021 08:33:33.775 * DB saved on disk
162:C 11 Jan 2021 08:33:33.775 * RDB: 6 MB of memory used by copy-on-write
134:M 11 Jan 2021 08:33:33.839 * Background saving terminated with success
134:M 11 Jan 2021 08:33:33.839 * Synchronization with replica 127.0.0.1:6370 succeeded
- 连接主机查看状态
...次数省略部分日志
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6370,state=online,offset=3514,lag=0
master_replid:87709b6a0d0127d2299e14d463ce98af9dfe11f7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3514
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3514
- 连接从机查看状态
[root@3b74f16b816c master]# ./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> info
...次数省略部分日志
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:3654
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:87709b6a0d0127d2299e14d463ce98af9dfe11f7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3654
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3654
2.2 sentinel配置
2.2.1 目录层级配置
[root@3b74f16b816c redis]# ls
redis-5.0.4 redis-5.0.4.tar.gz redis-sentinel redis-server
- 在redis-sentinel目录下创建程序
# 重新编译 并将bin改为sentienl(或者是从master拷贝过来在做更改)
make install PREFIX=/home/redis/redis-sentinel
- 复制原解压包的哨兵配置文件到当前目录下
2.2.2 sentinel.conf文件配置
#哨兵sentinel实例运行的端口
port 26379
# 修改为守护进程启动
daemonize yes
# 修改哨兵日志目录-一般记录报错日志信息
logfile "./logs"
# 后缀建议与端口一致
pidfile "/var/run/redis-sentinel_26379.pid"
# 哨兵sentinel的工作空间,建议填全路径,该目录下自动生成log文件,记录程序运行日志
dir "/home/redis/redis-sentinel/sentienl/tmp"
# 哨兵sentinel监控的redis主节点的 ip port
# master-name 可以自己命名的主节点名字 只能由字母A-z、数字0-9 、这三个字符".-_"组成。
# quorum 当这些quorum个数sentinel哨兵认为master主节点失联 那么这时 客观上认为主节点失联了
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster 127.0.0.1 6379 2
# 当在Redis实例中开启了requirepass foobared 授权密码 这样所有连接Redis实例的客户端都要提供密码
# 设置哨兵sentinel 连接主从的密码 注意必须为主从设置一样的验证密码
# sentinel auth-pass <master-name> <password>
sentinel auth-pass mymaster 123456
# 指定多少毫秒之后 主节点没有应答哨兵sentinel 此时 哨兵主观上认为主节点下线 默认30秒
# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds mymaster 30000
# 这个配置项指定了在发生failover主备切换时最多可以有多少个slave同时对新的master进行 同步,
# 这个数字越小,完成failover所需的时间就越长,
# 但是如果这个数字越大,就意味着越 多的slave因为replication而不可用。
# 可以通过将这个值设为 1 来保证每次只有一个slave 处于不能处理命令请求的状态。
# sentinel parallel-syncs <master-name> <numslaves>
sentinel parallel-syncs mymaster 1
# 故障转移的超时时间 failover-timeout 可以用在以下这些方面:
#1. 同一个sentinel对同一个master两次failover之间的间隔时间。
#2. 当一个slave从一个错误的master那里同步数据开始计算时间。直到slave被纠正为向正确的master那里同步数据时。
#3.当想要取消一个正在进行的failover所需要的时间。
#4.当进行failover时,配置所有slaves指向新的master所需的最大时间。不过,即使过了这个超时,slaves依
# 然会被正确配置为指向master,但是就不按parallel-syncs所配置的规则来了
# 默认三分钟 建议设置1min
# sentinel failover-timeout <master-name> <milliseconds>
sentinel failover-timeout mymaster 180000
# SCRIPTS EXECUTION
#配置当某一事件发生时所需要执行的脚本,可以通过脚本来通知管理员,例如当系统运行不正常时发邮件通知相关人员。
#对于脚本的运行结果有以下规则:
#若脚本执行后返回1,那么该脚本稍后将会被再次执行,重复次数目前默认为10
#若脚本执行后返回2,或者比2更高的一个返回值,脚本将不会重复执行。
#如果脚本在执行过程中由于收到系统中断信号被终止了,则同返回值为1时的行为相同。
#一个脚本的最大执行时间为60s,如果超过这个时间,脚本将会被一个SIGKILL信号终止,之后重新执行。
#通知型脚本:当sentinel有任何警告级别的事件发生时(比如说redis实例的主观失效和客观失效等等),将会去
#调用这个脚本,这时这个脚本应该通过邮件,SMS等方式去通知系统管理员关于系统不正常运行的信息。调用该脚本
#时,将传给脚本两个参数,一个是事件的类型,一个是事件的描述。
#如果sentinel.conf配置文件中配置了这个脚本路径,那么必须保证这个脚本存在于这个路径,并且是可执行的,
#否则sentinel无法正常启动成功。
#通知脚本,发生故障转移可以向管理员发送通知
# sentinel notification-script <master-name> <script-path> 默认不开启
sentinel notification-script mymaster /var/redis/notify.sh
# 客户端重新配置主节点参数脚本
# 当一个master由于failover而发生改变时,这个脚本将会被调用,通知相关的客户端关于master地址已经发生改变的信息。
# 以下参数将会在调用脚本时传给脚本:
# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>
# 目前<state>总是“failover”,
# <role>是“leader”或者“observer”中的一个。
# 参数 from-ip, from-port, to-ip, to-port是用来和旧的master和新的master(即旧的slave)通信的
# 这个脚本应该是通用的,能被多次调用,不是针对性的。
# sentinel client-reconfig-script <master-name> <script-path> 默认不开启
sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
注意:
- 以上的配置信息,在默认的 sentinel.conf文件均无配置,需要手动添加
- 在当前目录下需要创建tmp文件夹,避免哨兵启动报错
- 如果主机有配置密码,在任意一台主机与从机均要配置masterauth password,防止从机同步数据无权限失败
2.2.3 复制2个sentinel文件
[root@3b74f16b816c redis-sentinel]# cp sentienl/ sentienl1 -r
[root@3b74f16b816c redis-sentinel]# cp sentienl/ sentienl2 -r
[root@3b74f16b816c redis-sentinel]# ls
sentienl sentienl1 sentienl2
2.3.4 修改其他两个sentinel.conf
主要修改端口:26380,26381,其他信息已配置好,无需修改
#哨兵sentinel1实例修改
port 26380
pidfile "/var/run/redis-sentinel_26380.pid"
dir "/home/redis/redis-sentinel/sentienl1/tmp"
sentinel myid 0ae9f94b5c1353e47cd34854bda51a77ef0227c4
#哨兵sentinel2实例修改
port 26381
pidfile "/var/run/redis-sentinel_26381.pid"
dir "/home/redis/redis-sentinel/sentienl2/tmp"
sentinel myid 0ae9f94b5c1353e47cd34854bda51a77ef0227c5
2.3.5 启动关闭哨兵
分别启动三个哨兵
./redis-sentinel sentinel.conf
2.3.6 哨兵默认日志输出
日志目录:./tmp/log
387:X 12 Jan 2021 02:21:42.182 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
387:X 12 Jan 2021 02:21:42.182 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=387, just started
387:X 12 Jan 2021 02:21:42.182 # Configuration loaded
388:X 12 Jan 2021 02:21:42.315 * Running mode=sentinel, port=26379.
388:X 12 Jan 2021 02:21:42.315 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
388:X 12 Jan 2021 02:21:42.315 # Sentinel ID is 0ae9f94b5c1353e47cd34854bda51a77ef0227c3
388:X 12 Jan 2021 02:21:42.315 # +monitor master mymaster 127.0.0.1 6379 quorum 2
388:X 12 Jan 2021 02:21:42.317 * +slave slave 127.0.0.1:6370 127.0.0.1 6370 @ mymaster 127.0.0.1 6379
2.3.7 校验
# 连接哨兵
[root@3b74f16b816c sentienl]# ./redis-cli -h 127.0.0.1 -p 26379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# 列出所有主机信息 sentinel masters
127.0.0.1:26379> sentinel masters
1) "name"
2) "mymaster"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6379"
7) "runid"
...此处省略部分信息
# 列出指定从机的多有从机信息 sentinel slaves master-name
127.0.0.1:26379> sentinel slaves mymaster
1) "name"
2) "127.0.0.1:6370"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6370"
7) "runid"
2.3.8 其他哨兵操作指令
- sentinel master master-name
列出一个master相关的的信息 - sentinel sentinels master-name
列出master相关的sentinels组其他相关的信息 - sentinel get-master-addr-by-name master-name
获取master-name相关的 ip addr 的信息
2.3.9 主从切换操作
2.3.9.1 关闭主机6379
2.3.9.2 等待1min,在哨兵客户端查看主机信息
- 先查看哨兵的日志(./tmp/log)
- 连接从机6370,查看状态
[root@3b74f16b816c slave1]# ./redis-cli -h 127.0.0.1 -p 6370 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6370> info
...
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6379,state=online,offset=155155,lag=1
master_replid:6b4bd773eb906f2474476331bc826b15867df944
master_replid2:1a1c0c2a0f4534512f4cc55e0ee62ff54333552d
master_repl_offset:155155
至此,说明哨兵配置成功
3 故障判断原理
- 每个 Sentinel (哨兵)进程以每秒钟一次的频率向整个集群中的 Master 主服务器, Slave 从服务器以及其
他 Sentinel (哨兵)进程发送一个 PING 命令。 - 如果一个实例( instance )距离最后一次有效回复 PING 命令的时间超过 down-after-milliseconds 选
项所指定的值, 则这个实例会被 Sentinel (哨兵)进程标记为主观下线( SDOWN )。 - 如果一个 Master 主服务器被标记为主观下线( SDOWN ),则正在监视这个 Master 主服务器的所有
Sentinel (哨兵)进程要以每秒一次的频率确认 Master 主服务器的确进入了主观下线状态。 - 当有足够数量的 Sentinel (哨兵)进程(大于等于配置文件指定的值)在指定的时间范围内确认 Master 主
服务器进入了主观下线状态( SDOWN ), 则 Master 主服务器会被标记为客观下线( ODOWN )。 - 在一般情况下, 每个 Sentinel (哨兵)进程会以每 10 秒一次的频率向集群中的所有 Master 主服务器、
Slave 从服务器发送 INFO 命令。 - 当 Master 主服务器被 Sentinel (哨兵)进程标记为客观下线( ODOWN )时, Sentinel (哨兵)进程向下
线的 Master 主服务器的所有 Slave 从服务器发送 INFO 命令的频率会从 10 秒一次改为每秒一次。 - 若没有足够数量的 Sentinel (哨兵)进程同意 Master 主服务器下线, Master 主服务器的客观下线状态
就会被移除。若 Master 主服务器重新向 Sentinel (哨兵)进程发送 PING 命令返回有效回复, Master
主服务器的主观下线状态就会被移除。