CentOS7安装Redis-4哨兵模式

159 阅读4分钟

Configuring Sentinel

The Redis source distribution contains a file called sentinel.conf that is a self-documented example configuration file you can use to configure Sentinel, however a typical minimal configuration file looks like the following:

Redis 源代码发行版包含一个名为 Sentinel.conf 的文件,这是一个自我记录的示例配置文件,您可以用它来配置 Sentinel,但是一个典型的最小配置文件如下所示:

sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

sentinel monitor resque 192.168.1.3 6380 4
sentinel down-after-milliseconds resque 10000
sentinel failover-timeout resque 180000
sentinel parallel-syncs resque 5

安装Redis-4

搭建哨兵

配置sentinel.conf

[root@localhost bin]# vim sentinel.conf

#配置如下

#sentinel
#开启后台启动
daemonize yes
#哨兵日志文件
logfile "./sentinel.log"
#哨兵IP,分别对应各个节点IP
bind 192.168.0.5
#master节点IP
#您只需要指定要监视的主机,给每个分离的主机(可能有任意数量的副本)一个不同的名称。
#不需要指定副本,因为副本是自动发现的。Sentinel 将使用有关副本的附加信息自动更新配置(以便在重新启动时保留这些信息)。每次在故障转移期间将副本提升为主控以及每次发现新的 Sentinel 时,都会重写配置
sentinel monitor mymaster 192.168.0.5 6379 2
#如果在该时间(ms)内未能完成故障转移操作,则认为故障转移失败
sentinel failover-timeout mymaster 180000
#设置可以在同一时间进行故障转移后重新配置以使用新主机的副本的数量。数量越少,故障转移过程完成所需的时间就越长,设置为1,以确保一次只能访问一个副本
sentinel parallel-syncs mymaster 1
#6s未回复PING,则认为master主观下线
sentinel down-after-milliseconds mymaster 60000
#master密码
sentinel auth-pass mymaster 123456

#保存退出

启动sentinel

#各个节点分别启动
[root@localhost bin]# ./redis-sentinel sentinel.conf

查看状态

#查看各个节点状态
[root@localhost bin]# netstat -ntlp |grep redis
tcp        0      0 192.168.0.2:26379     0.0.0.0:*               LISTEN      12786/./redis-senti 
tcp        0      0 192.168.0.2:6379      0.0.0.0:*               LISTEN      25507/./redis-serve 

查看当前主从

#由此可见,5master,2和3slaver
[root@localhost bin]# ./redis-cli -h 192.168.0.5
192.168.0.5:6379> auth 123456
OK
192.168.0.5:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.0.3,port=6379,state=online,offset=15411,lag=1
slave1:ip=192.168.0.2,port=6379,state=online,offset=15552,lag=0
master_replid:ad23365e7a0fb1030e0a0052b8ba71d355f13416
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:15552
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:15552

测试容灾

#关闭5 master redis
[root@localhost bin]# netstat -ntlp |grep redis
tcp        0      0 127.0.0.1:9121          0.0.0.0:*               LISTEN      26133/redis_exporte 
tcp        0      0 192.168.0.5:26379     0.0.0.0:*               LISTEN      3675/./redis-sentin 
tcp        0      0 192.168.0.5:6379      0.0.0.0:*               LISTEN      16743/./redis-serve 
[root@localhost bin]# kill -9 16743

#任意节点查看sentinel.log
[root@localhost ~]# tail -f /usr/local/redis/bin/sentinel.log 
20625:X 01 Mar 16:15:27.855 # +promoted-slave slave 192.168.0.3:6379 192.168.0.3 6379 @ mymaster 192.168.0.5 6379
20625:X 01 Mar 16:15:27.855 # +failover-state-reconf-slaves master mymaster 192.168.0.5 6379
20625:X 01 Mar 16:15:27.906 * +slave-reconf-sent slave 192.168.0.2:6379 192.168.0.2 6379 @ mymaster 192.168.0.5 6379
20625:X 01 Mar 16:15:28.612 # -odown master mymaster 192.168.0.5 6379
20625:X 01 Mar 16:15:28.900 * +slave-reconf-inprog slave 192.168.0.2:6379 192.168.0.2 6379 @ mymaster 192.168.0.5 6379
20625:X 01 Mar 16:15:28.900 * +slave-reconf-done slave 192.168.0.2:6379 192.168.0.2 6379 @ mymaster 192.168.0.5 6379
20625:X 01 Mar 16:15:28.971 # +failover-end master mymaster 192.168.0.5 6379
20625:X 01 Mar 16:15:28.971 # +switch-master mymaster 192.168.0.5 6379 192.168.0.3 6379
20625:X 01 Mar 16:15:28.971 * +slave slave 192.168.0.2:6379 192.168.0.2 6379 @ mymaster 192.168.0.3 6379
20625:X 01 Mar 16:15:28.971 * +slave slave 192.168.0.5:6379 192.168.0.5 6379 @ mymaster 192.168.0.3 6379
20625:X 01 Mar 16:16:29.041 # +sdown slave 192.168.0.5:6379 192.168.0.5 6379 @ mymaster 192.168.0.3 6379


#+switch-master mymaster 192.168.0.5 6379 192.168.0.3 6379
#表示已成功切换


#再次查看主从状态
#登录3节点redis,发现已变成master
[root@localhost bin]# ./redis-cli -h 192.168.0.3
192.168.0.3:6379> auth 123456
OK
192.168.0.3:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.0.2,port=6379,state=online,offset=99889,lag=0
master_replid:359d8f9146942db96b66e1ac83ea61c50f3590b2
master_replid2:ad23365e7a0fb1030e0a0052b8ba71d355f13416
master_repl_offset:100030
second_repl_offset:42384
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:100030
#在3节点set key
192.168.0.3:6379> set 3 3
OK
192.168.0.3:6379> exit
#登录2节点,查询key,说明同步成功
[root@localhost bin]# ./redis-cli -h 192.168.0.2
192.168.0.2:6379> auth 123456
OK
192.168.0.2:6379> get 3
"3"