keepalive+redis 主从高可用
Posted wolf12
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalive+redis 主从高可用相关的知识,希望对你有一定的参考价值。
准备
1.1 准备
keepalived-1.2.22.tar.gz
redis-3.2.1.tar.gz
1.2 说明
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时, Slave接管服务升级Master,有写权限,同时关闭主从复制功能;
当 Master 恢复正常, Master降级为Slave同步数据,开启主从复制。
1.3 测试服务器信息
Master redis: 192.168.0.4
Slave redis: 192.168.0.5
配置redis服务
- 解压缩
tar zxf redis-3.2.1.tar.gz
mv redis-3.2.1 /soft/server
cd /soft/server
mv redis-3.2.1 redis - 编译安装
cd /soft/server/redis
make && make install - redis配置文件修改
vim redis.conf
...
bind 0.0.0.0 # 修改支持网络访问
protected-mode no # 同样一个安全配置,默认yes是不能网络访问的
daemonize yes # 后台运行redis
slaveof 192.168.0.4 6379 # 指定Master IP,同步(仅从配置) - 配置redis服务启动脚本
复制代码
vim /etc/init.d/redis-server
!/usr/bin/env bash
redis start up the redis server daemon
chkconfig: 345 99 99
description: redis service in /etc/init.d/redis
chkconfig --add redis or chkconfig --list redis
service redis start or service redis stop
processname: redis-server
config: /etc/redis.conf
注意不同位置要修改变量
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/soft/server/redis/src/redis-server
REDIS_CLI=/soft/server/redis/src/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/soft/server/redis/redis.conf"
make sure some dir exist
if [ ! -d /var/lib/redis ] ;then
mkdir -p /var/lib/redis
mkdir -p /var/log/redis
fi
case "$1" in
status)
ps -A|grep redis
;;
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
复制代码
5. 配置开机自启
chkconfig --add redis-server
chkconfig redis-server on
配置keepalive服务
- 编译安装
tar zxf keepalived-1.2.22.tar.gz
cd keepalived-1.2.22
./configure --prefix=/usr/local/keepalived
make && make install - 配置文件设置
复制代码
拷贝keepalived服务
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
配置keepalived服务
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
mkdir /etc/keepalived
拷贝配置文件
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
复制代码
3. keepalive配置文件
Master节点
复制代码
! Configuration File for keepalived
vrrp_script chk_redis {
script "/root/scripts/redis_check.sh" ###监控脚本
interval 2 ###监控时间
}
网卡需要注意,使用ifconfig查看一下当前活动网卡
vrrp_instance VI_1 {
state BACKUP ###设置为BACKUP
nopreempt # 不抢占MASTER
interface eth0 ###监控网卡
virtual_router_id 51
priority 100 ###权重值
authentication {
auth_type PASS ###加密
auth_pass 1q2w3e ###密码
}
#设置额外的监控,里面的任意一个网卡出现问题,都会进入FAULT状态
track_interface {
eth0
}
track_script {
chk_redis ###执行上面定义的chk_redis
}
virtual_ipaddress {
192.168.0.99 ######VIP
}
notify_master /root/scripts/redis_master.sh
notify_backup /root/scripts/redis_backup.sh
notify_fault /root/scripts/redis_fault.sh
notify_stop /root/scripts/redis_stop.sh
}
复制代码
Slave节点
复制代码
! Configuration File for keepalived
vrrp_script chk_redis {
script "/root/scripts/redis_check.sh" ###监控脚本
interval 2 ###监控时间
}
同样要注意网卡
vrrp_instance VI_1 {
state BACKUP ###设置为BACKUP
interface eth0 ###监控网卡
virtual_router_id 51
priority 10 ###比MASTRE权重值低
authentication {
auth_type PASS
auth_pass 1q2w3e ###密码与MASTRE相同
}
#设置额外的监控,里面的任意一个网卡出现问题,都会进入FAULT状态
track_interface {
eth0
}
track_script {
chk_redis ###执行上面定义的chk_redis
}
virtual_ipaddress {
192.168.0.99 ####vip
}
notify_master /root/scripts/redis_master.sh
notify_backup /root/scripts/redis_backup.sh
notify_fault /root/scripts/redis_fault.sh
notify_stop /root/scripts/redis_stop.sh
}
复制代码
4. keepalive脚本
Keepalive 定义的四种状态脚本说明
当进入Master状态时会呼叫notify_master
当进入Backup状态时会呼叫notify_backup
当发现异常情况时进入Fault状态呼叫notify_fault
当Keepalived程序终止时则呼叫notify_stop
redis_check.sh 检测redis是否正常脚本,正常则返回PONG,返回代码0,否则返回代码1
复制代码
!/bin/bash
ALIVE=/usr/local/bin/redis-cli PING
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
复制代码
redis_master.sh 我被选入Master时,执行的脚本
复制代码
!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
复制代码
redis_fault.sh 当检测服务不正常是,执行fault脚本
!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
redis_stop.sh keepalive终止时写入日志
!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
redis_backup.sh 进入slave时执行的脚本
复制代码
!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.0.5 6379 >> $LOGFILE 2>&1 # IP为之前从redis的IP
复制代码
这里backup 脚本主要说明一下,我的keepalive配置了不去抢占Master,也就是当我Master 宕机后,我master再次起来就是slave,这时就是执行redis_backup的时候了,我去指定我的Master去同步
- 配置开机自启
chkconfig --add keepalived
chkconfig keepalived on
测试 vip 192.168.75.200 连接的是主节点128
Replication
role:master
connected_slaves:1
slave0:ip=192.168.75.131,port=6379,state=online,offset=1569,lag=1
master_repl_offset:1569
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1568
测试是否IP漂移到从节点,同时131从节点变主节点提供服务。
128 停止redis服务,模拟主节点挂掉。
systemctl stop redis
131 变主节点,提供读写服务
info
Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:513
开启128之前的主节点,这时128变为从节点提供读的服务。同时会把131的数据同步到128。
systemctl start redis
128变成了从节点了。
role:slave
master_host:192.168.75.131
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:43
slave_priority:100
slave_read_only:1
总结
如果服务器资源不够的话,用keepalived就行了。两个节点就行了。sentinel是redis官网推荐的方案,需要的服务器比较多,最少三台,因为它用了节点仲裁原理来选master。
以上是关于keepalive+redis 主从高可用的主要内容,如果未能解决你的问题,请参考以下文章