CentOS使用Sentinel和VIP漂移实现Redis主从高可用

NoSQL笔记 2020年05月25日

本篇笔记记录了在CentOS7下,使用Sentinel和VIP漂移实现Redis主从高可用的过程

Redis主从搭建请移步 CentOS安装Redis并配置主从同步
VIP创建请移步 CentOS配置虚拟IP(VIP)

工作准备

Redis服务器

192.168.75.240:6379	主节点
192.168.75.241:6379	从节点1
192.168.75.242:6379	从节点2

Sentinel服务器

192.168.75.240:26379	哨兵1
192.168.75.241:26379	哨兵2
192.168.75.242:26379	哨兵3

VIP

192.168.75.100

配置sentinel,3个哨兵配置如下

复制sentinel.conf至安装目录下

cp /usr/local/src/redis-5.0.5/sentinel.conf /usr/local/redis/sentinel.conf

编辑配置

vim /usr/local/redis/sentinel.conf

配置如下

#ip绑定
bind 0.0.0.0
#保护模式,未设置密码写no
protected-mode no
#端口绑定
port 26379
#守护进程
daemonize yes
#日志文件
logfile "/var/log/redis/sentinel.log"
#哨兵监听的主节点ip和端口号,最后的2代表主节点客观下线的哨兵数量,一般设置哨兵数的一半+1,一个哨兵认为主节点下线叫主观下线,超过一半的哨兵认为主节点下线叫客观下线,才会触发选举
sentinel monitor mymaster 192.168.75.240 6379 2
#主节点在设置的时间内无响应,哨兵将向其他哨兵发出主观下线消息
sentinel down-after-milliseconds mymaster 5000
#故障转移时做主从同步的从节点数
sentinel parallel-syncs mymaster 1
#故障转移超时时间
sentinel failover-timeout mymaster 15000
#主从切换后通知更改配置脚本
sentinel client-reconfig-script mymaster /usr/local/redis/notify_master.sh

通知更改配置脚本,实现VIP漂移

[root@localhost ~]# cat /usr/local/redis/notify_master.sh
#!/bin/bash
 
MASTER_IP=$6
LOCAL_IP='192.168.75.240'
NETMASK='24'
INTERFACE='ens33'
VIP='192.168.75.100'
 
if [ ${MASTER_IP} = ${LOCAL_IP} ];then
    ip addr add ${VIP}/${NETMASK} dev ${INTERFACE}
    arping -c 1 -I ${INTERFACE} -U ${VIP}
    exit 0
else
    ip addr del ${VIP}/${NETMASK} dev ${INTERFACE}
    exit 0
fi
exit 1

MASTER_IP=$6表示第6个参数为新的主节点IP
LOCAL_IP表示当前节点的IP,3个节点各配各的
设置脚本权限

chmod 755 /usr/local/redis/notify_master.sh

创建log目录

mkdir /var/log/redis

启动3个哨兵

/usr/local/redis/bin/redis-sentinel /usr/local/redis/sentinel.conf

查看哨兵状态

[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.75.240 -p 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.75.240:6379,slaves=2,sentinels=3

主节点为192.168.75.240

通过VIP连接进行测试

查看redis主从状态

[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.75.100 -p 6379 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.75.241,port=6379,state=online,offset=159050,lag=1
slave1:ip=192.168.75.242,port=6379,state=online,offset=159050,lag=1
master_replid:1c717f2c828513bf495037e40d6383cb1c0c7563
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:159193
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:159193
[root@localhost ~]# 

查看sentinel状态

[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.75.100 -p 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.75.240:6379,slaves=2,sentinels=3

测试主从复制
通过VIP设置key

[root@localhost redis]# /usr/local/redis/bin/redis-cli -h 192.168.75.100 -p 6379 set jmsite jiaming
OK
[root@localhost redis]#

从节点获取key

[root@localhost redis]# /usr/local/redis/bin/redis-cli -h 192.168.75.241 -p 6379 get jmsite
"jiaming"
[root@localhost redis]#

测试故障转移

关闭主节点

/usr/local/redis/bin/redis-cli -h 192.168.75.240 -p 6379 shutdown 

通过VIP查看哨兵状态

[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.75.100 -p 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.75.241:6379,slaves=2,sentinels=3

主节点变更为192.168.75.241
进入192.168.75.241,查看网卡信息

[root@localhost redis]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:99:34:af brd ff:ff:ff:ff:ff:ff
    inet 192.168.75.241/24 brd 192.168.75.255 scope global noprefixroute dynamic ens33
       valid_lft 1057sec preferred_lft 1057sec
    inet 192.168.75.100/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
[root@localhost redis]# 

我们看到,VIP已经漂移过来了,再次通过VIP连接redis查看集群信息

[root@localhost redis]# /usr/local/redis/bin/redis-cli -h 192.168.75.100 -p 6379 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.75.242,port=6379,state=online,offset=317133,lag=1
master_replid:2d7881bc986b0edff1e3ea605bf8b17602f54c45
master_replid2:1c717f2c828513bf495037e40d6383cb1c0c7563
master_repl_offset:317276
second_repl_offset:255086
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:317276
[root@localhost redis]# 

我们看到,当前从节点为192.168.75.242
再次启动192.168.75.240

/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf

再次查看集群信息

[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.75.100 -p 6379 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.75.242,port=6379,state=online,offset=362052,lag=0
slave1:ip=192.168.75.240,port=6379,state=online,offset=362052,lag=0
master_replid:2d7881bc986b0edff1e3ea605bf8b17602f54c45
master_replid2:1c717f2c828513bf495037e40d6383cb1c0c7563
master_repl_offset:362338
second_repl_offset:255086
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:362338
[root@localhost ~]#

我们看到192.168.75.240以从库的身份重新加入了主从集群当中,完毕!