欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Redis双机热备(keepalived实现)

程序员文章站 2022-05-22 21:17:46
...

一 :场景:

server1:192.168.1.142  redis,keepalived

server2: 192.168.1.143  redis,keepalived

VIP:192.168.1.42

 

二: 目标

用户只要访问通过集群VIP访问redis服务。要求这个架构在服务端保证HA

 

三:原理

两个redis server主从备份。提供redis 服务HA;

两个keepalived 服务主从备份,提供VIP 服务的HA;

 

1)每台redis server分别有主,从两个配置文件(redis.conf, redis_slave.conf),通过启动脚本启动服务,启动脚本会检测这个redis集群中的其他服务器的角色,如果有master 服务存在,则以slave角色启动,否则将自己以master服务器启动;

2) keepalived 监控脚本,定时(频率:每秒一次)检测当前服务器是否获取集群VIP,如果获取集群VIP,则将本服务器上的redis服务器设置为master。同时将远端的其他redis服务器设置为slave;保证获取集群VIP的redis 服务器角色为master,其他的设置为slave。

3) keepalived 监控脚本,还会自动检测当前redis服务器是否正常,如果连续两次检测异常,则停掉本本机的keepalived服务,释放集群VIP,让去漂移到其他可以提供redis 服务的服务器上;

 

 

 

 

四:redis 安装配置

1. redis的安装目录

# /usr/local/redis-2.8.3

 

2:redis 启动脚本配置

 

#!/bin/bash
REDISPATH=/usr/local/redis-2.8.3
REDISCLI=$REDISPATH/bin/redis-cli
LOGFILE=$REDISPATH/logs/redis-state.log
LOCALIP=192.168.1.142
REMOTEIP=192.168.1.152


REMOTEREDISROLE=`$REDISCLI -h $REMOTEIP info | grep "role"`
if grep "role:master" <<< $REMOTEREDISROLE ; then
	#start as slave
	echo "slave" >> $LOGFILE
	$REDISPATH/bin/redis-server $REDISPATH/conf/redis_slave.conf
else
	#start as master
	echo "master" >> $LOGFILE
	$REDISPATH/bin/redis-server $REDISPATH/conf/redis.conf
fi

 这里是142的配置,另外一台服务器只要修改IP地址,其他不变

 

 

LOCALIP=192.168.1.152
REMOTEIP=192.168.1.142

 

 

3:redis 配置文件,按照正常的主从配置。

 

五: keepalived安装配置

1.  keepalived的安装目录

# /usr/local/keepalived-1.2.9

 

2. keepalived.conf 配置

! Configuration File for keepalived
vrrp_script chk_redis {
	script "/usr/local/keepalived-1.2.9/etc/keepalived/chk_redis.sh"
	interval 3
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 2
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.42
    }
    track_script {
			chk_redis
    }
}

 

3:chk_redis.sh 监控脚本配置

#!/bin/bash
REDISPATH=/usr/local/redis-2.8.3
REDISCLI=$REDISPATH/bin/redis-cli
LOGFILE=$REDISPATH/logs/redis-state.log
LOCALIP=192.168.1.152
REMOTEIP=192.168.1.142
VIP=192.168.1.42

VIPALIVE=`ip a | grep "$VIP"`
if [ "$VIPALIVE" == "" ]; then
	echo "[info]:"`date`" keepalived server is pengding or stop" >> $LOGFILE
else
	echo "bbb" >> $LOGFILE
	#check local service is running
	if [ "`$REDISCLI PING`" == "PONG" ]; then
		# check local redis server role.
		REDISROLE=`$REDISCLI info | grep "role"`
		if grep "role:slave" <<< $REDISROLE ; then
			#change local redis server as master 
			echo "[info1]:"`date`" Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
			$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

			#change remoting redis server as slave
			REMOTEREDISROLE=`$REDISCLI -h $REMOTEIP info | grep "role"`
			if grep "role:master" <<< $REMOTEREDISROLE ; then
				echo "[info2]:"`date`" Run remote server SLAVEOF cmd ..." >> $LOGFILE
				$REDISCLI -h $REMOTEIP SLAVEOF $LOCALIP 6379 >> $LOGFILE  2>&1
			fi
		else
			REMOTEREDISROLE=`$REDISCLI -h $REMOTEIP info | grep "role"`
			if grep "role:master" <<< $REMOTEREDISROLE ; then
				echo "[info3]:"`date`" Run remote server SLAVEOF cmd ..." >> $LOGFILE
				$REDISCLI -h $REMOTEIP SLAVEOF $LOCALIP 6379 >> $LOGFILE  2>&1
			fi
		fi	
	else
		echo "[warn]:"`date`"  redis server($LOCALIP) is not health..." >> $LOGFILE
		sleep 1
		if [ "`$REDISCLI PING`" != "PONG" ]; then
			echo "[error]:"`date`"  redis server($LOCALIP) will be stop..." >> $LOGFILE
			service keepalived stop
		fi
	fi
fi

 这里是152的配置,另外一台服务器只要修改IP地址,其他不变。如上代码,这里不贴出来了。

 

六: 测试

只要任何一台keepalived正常,就可以提供vip服务。 redis的master会随着vip的漂移而改变,谁获得了vip,谁就具有redis 的master角色,其他都会置为slave角色

相关标签: keepalived redis