redis sentinel哨兵 实例

哨兵前提是创建主从
文件详解

#如果是编译安装,在源码目录有sentinel.conf,复制到安装目录即可,
如:/apps/redis/etc/sentinel.conf
[root@centos8 ~]#vim /etc/redis-sentinel.conf
bind 0.0.0.0
port 26379
daemonize yes
pidfile "redis-sentinel.pid"
logfile "sentinel_26379.log"
dir "/tmp"  #工作目录
sentinel myid 50547f34ed71fd48c197924969937e738a39975b
sentinel monitor mymaster 10.0.0.8 6379 2 #指定当前mymaster集群中master服务器的地址和端口
#2为法定人数限制(quorum),即有几个sentinel认为master down了就进行故障转移,一般此值是所有
sentinel节点(一般总数是>=3的 奇数,如:3,5,7等)的一半以上的整数值,比如,总数是3,即
3/2=1.5,取整为2,是master的ODOWN客观下线的依据
sentinel auth-pass mymaster 123456 #mymaster集群中master的密码,注意此行要在上面行的下面
sentinel down-after-milliseconds mymaster 30000 #(SDOWN)判断mymaster集群中所有节点的主观下线的时间,单位:毫秒,建议3000
sentinel parallel-syncs mymaster 1 #发生故障转移后,同时向新master同步数据的slave数量,数字越小总同步时间越长,但可以减轻新master的负载压力
sentinel failover-timeout mymaster 180000 #所有slaves指向新的master所需的超时时间,单位:毫秒
sentinel deny-scripts-reconfig yes #禁止修改脚本
logfile /var/log/redis/sentinel.log

配置哨兵文件
查看文件下的配置

grep -vE "^#|^$" /etc/redis-sentinel.conf


port 26379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/redis/sentinel.log"
dir "/tmp"
sentinel monitor mymaster 10.0.0.8 6379 2   #修改此行
sentinel auth-pass mymaster 123456 #增加此行
sentinel down-after-milliseconds mymaster 3000   #修改此行
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

#自动生成不需要创建
sentinel myid 50547f34ed71fd48c197924969937e738a39975b  #此行每个哨兵主机自动生成,且必须唯一

把配置好的文件发给其余两个slave主机

scp /etc/redis-sentinel.conf 10.0.0.18:/etc/
scp /etc/redis-sentinel.conf 10.0.0.28:/etc/

启动哨兵

[root@redis-master ~]#systemctl enable --now redis-sentinel.service
[root@redis-slave1 ~]#systemctl enable --now redis-sentinel.service
[root@redis-slave2 ~]#systemctl enable --now redis-sentinel.service

编译安装可以在所有哨兵服务器执行下面操作启动哨兵

#vim /apps/redis/etc/sentinel.conf
bind 0.0.0.0
port 26379
daemonize yes
pidfile "redis-sentinel.pid"
Logfile "sentinel_26379.log"
dir "/apps/redis/data"
sentinel monitor mymaster 10.0.0.8 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 15000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

#/apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf

验证哨兵端口

[root@master ~]# ss -ntl
State                          Recv-Q                          Send-Q                                                    Local Address:Port                                                     Peer Address:Port                          
LISTEN                         0                               128                                                             0.0.0.0:6379                                                          0.0.0.0:*                             
LISTEN                         0                               128                                                             0.0.0.0:26379                                                         0.0.0.0:*                             
LISTEN                         0                               128                                                             0.0.0.0:22                                                            0.0.0.0:*                             
LISTEN                         0                               128                                                                [::]:26379                                                            [::]:*                             
LISTEN                         0                               128                                                                [::]:22                                                               [::]:*                             

验证时候成功
停止master节点,master自动转移到其他slave节点

[root@master ~]# redis-cli -h 10.0.0.18
10.0.0.18:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.28,port=6379,state=online,offset=460072,lag=1
slave1:ip=10.0.0.8,port=6379,state=online,offset=460072,lag=1
master_replid:7d337f59b297cebee92d682a49911eddd6979f82
master_replid2:758cca9443d71103241bd51236d4066c7aa3c234
master_repl_offset:460205
second_repl_offset:236496
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:460205

[root@master ~]# killall redis-server

10.0.0.8:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.28,port=6379,state=online,offset=548703,lag=1
master_replid:252b4cde6c43a4c0a8967f3fd7510219696cee9b
master_replid2:7d337f59b297cebee92d682a49911eddd6979f82
master_repl_offset:548835
second_repl_offset:533558
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:297314
repl_backlog_histlen:251522
10.0.0.8:6379> 
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页