数据库
首页 > 数据库> > keepalived+redis实现一主一从高可用

keepalived+redis实现一主一从高可用

作者:互联网

keepalived+redis实现一主一从高可用

安装

编译安装
刚开始进行编译的时候,出现了fatal error: linux/mpls_iptunnel.h: No such file or directory的异常;未解决,直接使用YUM进行了安装。
如果有方法解决,请留言告知,十分感谢。

使用yum直接安装:

yum install keepalived

配置

在主机和从机安装keepalived之后,默认配置文件的位置在:

/etc/keepalived/keepalived.conf

主机配置

keepalived.conf
需要修改的地方是.sh脚本的位置,改为自己脚本文件的位置即可。
virtual_ipaddress改为自己的即可。
interface修改为自己的。
state标识主从。

script "/etc/keepalived/scripts3/redis_check.sh"
interval 1
}
vrrp_instance v_r1 {
state MASTER
interface ens33
#garp_master_delay 10
virtual_router_id 18
priority 110
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.131.18
}
track_script {
chk_redis
}
notify_master /etc/keepalived/scripts3/redis_master.sh
notify_backup /etc/keepalived/scripts3/redis_slave.sh
notify_fault /etc/keepalived/scripts3/redis_fault.sh
notify_stop /etc/keepalived/scripts3/redis_stop.sh
}

接下来需要创建脚本文件
redis_check.sh redis_fault.sh redis_master.sh redis_slave.sh redis_stop.sh
脚本文件中需要修改的是redis的位置,和自己机器的IP地址,和redis端口。
redis_check.sh

#!/bin/bash
ALIVE=`/opt/redis-4.0.11/src/redis-cli -h 192.168.131.130 -p 6302 PING`

if [ "$ALIVE" == "PONG" ]; then

echo $ALIVE

exit 0

else

echo $ALIVE

exit 1

fi

redis_fault.sh

#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE

redis_master.sh

#!/bin/bash
REDISCLI=/opt/redis-4.0.11/src/redis-cli
LOGFILE=/var/log/keepalived-redis-state.log
LOCALIP="192.168.131.130"
REMOTEIP="192.168.131.131"
PORT="6302"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Beging master..." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p 6302 SLAVEOF $REMOTEIP 6302 >> $LOGFILE 2>&1
sleep 10   
echo "Run SLAVEOF NO ONE cmd..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p 6302 SLAVEOF NO ONE >> $LOGFILE 2>&1
 

redis_slave.sh

#!/bin/bash
REDISCLI="/opt/redis-4.0.11/src/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
LOCALIP="192.168.131.130"
REMOTEIP="192.168.131.131"
PORT="6302"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Begingslave..." >> $LOGFILE 2>&1
sleep15
echo "Run SLAVEOF cmd...">> $LOGFILE
$REDISCLI -h $LOCALIP -p 6302 SLAVEOF $REMOTEIP 6302 >> $LOGFILE 2>&1


redis_stop.sh

#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE

从机配置

从机要做的一样
keepalived.conf

vrrp_script chk_redis {
script "/etc/keepalived/scripts3/redis_check.sh"
interval 1
}
vrrp_instance v_r1 {
state BACKUP
interface ens33
#garp_master_delay 10
virtual_router_id 18
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.131.18
}
track_script {
chk_redis
}
notify_master /etc/keepalived/scripts3/redis_master.sh
notify_backup /etc/keepalived/scripts3/redis_slave.sh
notify_fault /etc/keepalived/scripts3/redis_fault.sh
notify_stop /etc/keepalived/scripts3/redis_stop.sh

redis_check.sh

#!/bin/bash
ALIVE=`/opt/redis-4.0.11/src/redis-cli -h 192.168.131.131 -p 6302 PING`

if [ "$ALIVE" == "PONG" ]; then

echo $ALIVE

exit 0

else

echo $ALIVE

exit 1

fi

redis_fault.sh

#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE

redis_master.sh

#!/bin/bash
REDISCLI=/opt/redis-4.0.11/src/redis-cli
LOGFILE=/var/log/keepalived-redis-state.log
LOCALIP="192.168.131.131"
REMOTEIP="192.168.131.130"
PORT="6302"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Beging master..." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p 6302 SLAVEOF $REMOTEIP 6302 >> $LOGFILE 2>&1
sleep 10   
echo "Run SLAVEOF NO ONE cmd..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p 6302 SLAVEOF NO ONE >> $LOGFILE 2>&1

redis_slave.sh

#!/bin/bash
REDISCLI="/opt/redis-4.0.11/src/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
LOCALIP="192.168.131.131"
REMOTEIP="192.168.131.130"
PORT="6302"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Begingslave..." >> $LOGFILE 2>&1
sleep15
echo "Run SLAVEOF cmd...">> $LOGFILE
$REDISCLI -h $LOCALIP -p 6302 SLAVEOF $REMOTEIP 6302 >> $LOGFILE 2>&1

redis_stop.sh

#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE

redis主从配置文件

从 slave of 主即可。

启动

#关闭防火墙
systemctl stop firewalld
#关闭selinux
vi /etc/selinux/config
SELINUX=disabled
#给脚本都加上可执行权限:
sudo chmod +x /etc/keepalived/scripts/*.sh

启动redis

启动keepalived
systemctl start keepalived.service
#查看状态
systemctl status keepalived.service
#关闭keepalived
systemctl stop keepalived.service

测试

ip a

主机会多一个
在这里插入图片描述
关闭主机redis。
虚拟ip会飘到从机。
从机redis变为主机。

标签:redis,keepalived,echo,一主一,sh,LOGFILE,6302
来源: https://blog.csdn.net/qq_38850577/article/details/110622345