keepalive+redis 增量和全部同步

 

 

1 安装keepalived

wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz -P /export1/soft/
cd /export1/soft/
tar zxvf keepalived-1.2.24.tar.gz
cd keepalived-1.2.24
yum install ipvsadm kernel-devel popt-devel openssl-* -y
ln -s /usr/src/kernels/2.6.32-696.13.2.el6.x86_64/ /usr/src/linux
yum update glib* -y
./configure --prefix=/usr/local/keepalived
make
make install
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir -pv /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
chmod 755 /etc/init.d/keepalived
mkdir /etc/keepalived/scripts/
service keepalived start
service keepalived stop
>/etc/keepalived/keepalived.conf

###########################################################################

2 主上边:10.5.2.10 vim /etc/keepalived/keepalived.conf

global_defs {
router_id redis1101
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 1
}
vrrp_instance v_r1 {
state MASTER
interface eth0
#garp_master_delay 10
virtual_router_id 18
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.5.2.18
}
track_script {
chk_redis
}
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_slave.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}

###########################################################

vim  /etc/keepalived/scripts/redis_check.sh

#!/bin/bash

ALIVE=`/usr/local/bin/redis-cli -h 10.5.2.10 PING`

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

echo $ALIVE

exit 0

else

echo $ALIVE

exit 1

fi

###########################################################################

vim  /etc/keepalived/scripts/redis_fault.sh 

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

##########################################################

vim  /etc/keepalived/scripts/redis_stop.sh 

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

#############################################################

vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash
REDISCLI=/usr/local/bin/redis-cli
LOGFILE=/var/log/keepalived-redis-state.log
LOCALIP="10.5.2.10"
REMOTEIP="10.5.2.11"
PORT="6379"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Beging master..." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p 6379 SLAVEOF $REMOTEIP 6379 >> $LOGFILE 2>&1
sleep 10
echo "Run SLAVEOF NO ONE cmd..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p 6379 SLAVEOF NO ONE >> $LOGFILE 2>&1

######################################################################

vim /etc/keepalived/scripts/redis_slave.sh

#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
LOCALIP="10.5.2.10"
REMOTEIP="10.5.2.11"
PORT="6379"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Begingslave..." >> $LOGFILE 2>&1
sleep15
echo "Run SLAVEOF cmd...">> $LOGFILE
$REDISCLI -h $LOCALIP -p 6379 SLAVEOF $REMOTEIP 6379 >> $LOGFILE 2>&1

################################################################################

 

 

 

 

 #######################################################################################################

切换状态

 

 

posted @ 2017-10-30 15:37  晴天下大雨  阅读(985)  评论(0编辑  收藏  举报