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

keepalived + redis 主备容灾部署

程序员文章站 2024-03-21 09:37:58
...

方案图

keepalived + redis 主备容灾部署

现网环境中有两个数据库主机,master, 和slave。以及一个vip

Master: 192.168.11.149

Slave:192.168.11.150

Vip:192.168.200.200

节点A Redis-Master 主节点,节点B Redis-Slave 备用节点,虚拟IP是对外提供访问的IP。虚拟IP在某个时刻只能属于某一个节点,另一个节点作为备用节点存在。当主节点不可用时,备用节点接管虚拟IP,提供正常服务。

安装redis

redis master安装

下载redis-3.0.3.tar.gz 创建redis 账户

#useradd  -d /home/cin_redis –s /bin/bash –m cin_redis
# passwd cin_redis

上传redis-3.0.3.tar.gz 解压

# tar –xzvf redis-3.0.3.tar.gz
#cd rediis-3.0.3/src
#mak MALLOC=libc
#make PREFIX=$HOME install

在redis的解压目录下有一个redis.conf 文件,编辑redis.conf文件

daemonize yes	后台模式运行
port 15093		端口
logfile “/home/cin_redis/redis-15093.log”
pidfile “/home/cin_redis/redis-15093.pid”

可以把该配置文件拷贝到bin目录下修改;启动redis

# redis-server redis.conf

redis slave 安装

redis slave机器上同样安装redis 数据库,安装步骤同2.1安装,修改redis.conf 文件

daemonize yes	后台模式运行
port 15093		端口
logfile “/home/cin_redis/redis-15093.log”
pidfile “/home/cin_redis/redis-15093.pid”

slaveof 192.168.11.149 15093 

可以把该配置文件拷贝到bin目录下修改;启动redis

#redis-server redis.conf

用redis-cli 客户端连接redis-master数据库,查看replication

keepalived + redis 主备容灾部署

用redis-cli客户端连接redis-slave数据库,查看replication

keepalived + redis 主备容灾部署

安装keepalived

下载keepalived-1.2.23(https://codeload.github.com/acassen/keepalived/tar.gz/v1.2.23) 创建keepalived 账户

#useradd –m keepalived –d /home/keepalived –s /bin/bash
#passwd keepalived

上传keepalived-1.2.23.tar.gz

#tar –xzvf keepalived-1.2.23.tar.gz
#cd keepalived-1.2.23
#./configure –prefix=$HOME/keepalive
# make &make install
# cp  -r  /home/keepalived/keepalive/etc/keepalived  /etc 
# cp  /home/keepalived/keepalive/etc/rc.d/init.d/keepalived  /etc/rc.d/init.d
#cp /home/keepalived/keepalive/etc/sysconfig/keepalived  /etc/sysconfig
#ln –s /home/keepalived/keepalive/sbin/keepalived  /usr/sbin/
#chkconfig –add keepalived

按照以上步骤分别在redis-master 和 redis-slave 主机上安装keepalived工具

配置keepalived

redis master 配置

按照上面的步骤安装完keepalived,vim 打开/etc/keepalived下面的keepalived.conf 文件配置

! Configuration File for keepalived

vrrp_script chk_redis {
    script "/etc/keepalived/scripts/redis_check.sh"
    interval 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.200
    }
    track_script {
        chk_redis
    }
    notify_master "/etc/keepalived/scripts/redis_master.sh"
    notify_backup "/etc/keepalived/scripts/redis_backup.sh"
    notify_fault  "/etc/keepalived/scripts/redis_fault.sh"
    notify_stop   "/etc/keepalived/scripts/redis_stop.sh"
}

在/etc/keepalived 目录下创建scripts,

#cd /etc/keepalived/scripts
#touch redis_master.sh
#touch redis_backup.sh
#touch redis_fault.sh]
#touch redis_stop.sh
#touch redis_check.sh

redis_master.sh : 当keepalived切换成master时,会触发执行此脚本

redis_backuo.sh : 当keepalived 切换成slave时,会触发执行此脚本

redis_fault.sh : 当keepalived 出错时,会触发执行此脚本

redis_stop.sh : 当keepalived 停止时,会触发执行次脚本

redis_check.sh : keepalived定时调用此脚本检测redis 数据库是否连接正常

redis_master.sh文件内容:

#!/bin/bash

KPATH=/home/keepalived
RPATH=/home/cin_redis

REDISCLI=$RPATH/bin/redis-cli

LOGFILE=$KPATH/log/keepalived-redis-state.log

LOCALIP="192.168.11.149"
REMOTEIP="192.168.11.150"
PORT="15093"

echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Beging master..." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p $PORT SLAVEOF $REMOTEIP $PORT >> $LOGFILE 2>&1
sleep 10
echo "Run SLAVEOF NO ONE cmd..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p $PORT SLAVEOF NO ONE >> $LOGFILE 2>&1

redis_backup.sh文件内容

#!/bin/bash

KPATH=/home/keepalived
RPATH=/home/cin_redis

REDISCLI=$RPATH/bin/redis-cli

LOGFILE=$KPATH/log/keepalived-redis-state.log

LOCALIP="192.168.11.149"
REMOTEIP="192.168.11.150"
PORT="15093"

echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Beging slave..." >> $LOGFILE 2>&1
sleep 15
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p $PROT  SLAVEOF $REMOTEIP $PORT >> $LOGFILE 2>&1

redis_fault.sh 文件内容

#!/bin/bash

KPATH=/home/keepalived
RPATH=/home/cin_redis

REDISCLI=$RPATH/bin/redis-cli

LOGFILE=$KPATH/log/keepalived-redis-state.log

LOCALIP="192.168.11.149"
REMOTEIP="192.168.11.150"
PORT="15093"

echo "[ERROR]-----keepalived is fault,change local redis to slave-----" >> $LOGFILE
date >> $LOGFILE
echo "Beging slave..." >> $LOGFILE 2>&1
sleep 10
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p $PORT SLAVEOF $REMOTEIP $PORT >> $LOGFILE 2>&1

redis_stop.sh 文件内容

#!/bin/bash

KPATH=/home/keepalived
RPATH=/home/cin_redis

REDISCLI=$RPATH/bin/redis-cli

LOGFILE=$KPATH/log/keepalived-redis-state.log

LOCALIP="192.168.11.149"
REMOTEIP="192.168.11.150"
PORT="15093"

echo "[ERROR]-----keepalived is stop,change local redis to slave-----" >> $LOGFILE
date >> $LOGFILE
echo "Beging slave..." >> $LOGFILE 2>&1
sleep 10
echo "Run SLAVEOF cmd ..." >> $LOGFILE

$REDISCLI -h $LOCALIP -p $PORT SLAVEOF $REMOTEIP $PORT >> $LOGFILE 2>&1

redis_check.sh 文件内容

#!/bin/bash

KPATH=/home/keepalived
RPATH=/home/cin_redis

REDISCLI=$RPATH/bin/redis-cli

LOGFILE=$KPATH/log/keepalived-redis-state.log

LOCALIP="192.168.11.149"
REMOTEIP="192.168.11.150"
PORT="15093"

ALIVE=`$REDISCLI -h $LOCALIP -p $PORT PING`

if [ "$ALIVE" == "PONG" ];then
   echo "[INFO] `date +'%Y-%m-%d:%H:%M:%S'` :$LOCALIP local redis is health." >> $LOGFILE
   exit 0
else
   service keepalived stop
   echo "[ERROR] `date +'%Y-%m-%d:%H:%M:%S'` :$LOCALIP local redis is not health." >> $LOGFILE
   exit 1
fi

redis slave 配置

按照上面的步骤安装完keepalived,vim 打开/etc/keepalived下面的keepalived.conf 文件配置

! Configuration File for keepalived

vrrp_script chk_redis {
    script "/etc/keepalived/scripts/redis_check.sh"
    interval 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.200
    }
    track_script {
        chk_redis
    }
    notify_master /etc/keepalived/scripts/redis_master.sh
    notify_backup /etc/keepalived/scripts/redis_backup.sh
    notify_fault  /etc/keepalived/scripts/redis_fault.sh
    notify_stop   /etc/keepalived/scripts/redis_stop.sh
}

在/etc/keepalived 目录下创建scripts,

#cd /etc/keepalived/scripts
#touch redis_master.sh
#touch redis_backup.sh
#touch redis_fault.sh
#touch redis_stop.sh
#touch redis_check.sh

redis_master.sh : 当keepalived切换成master时,会触发执行此脚本

redis_backuo.sh : 当keepalived 切换成slave时,会触发执行此脚本

redis_fault.sh : 当keepalived 出错时,会触发执行此脚本

redis_stop.sh : 当keepalived 停止时,会触发执行次脚本

redis_check.sh : keepalived定时调用此脚本检测redis 数据库是否连接正常

redis_master.sh 文件内容

#!/bin/bash

KPATH=/home/keepalived
RPATH=/home/cin_redis

REDISCLI=$RPATH/bin/redis-cli

LOGFILE=$KPATH/log/keepalived-redis-state.log

LOCALIP="192.168.11.150"
REMOTEIP="192.168.11.149"
PORT="15093"

echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Beging master..." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p $PORT SLAVEOF $REMOTEIP $PORT >> $LOGFILE 2>&1
sleep 10
echo "Run SLAVEOF NO ONE cmd..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p $PORT SLAVEOF NO ONE >> $LOGFILE 2>&1

redis_backup.sh 文件内容

#!/bin/bash

KPATH=/home/keepalived
RPATH=/home/cin_redis

REDISCLI=$RPATH/bin/redis-cli

LOGFILE=$KPATH/log/keepalived-redis-state.log

LOCALIP="192.168.11.150"
REMOTEIP="192.168.11.149"
PORT="15093"

echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Beging slave..." >> $LOGFILE 2>&1
sleep 15
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p $PORT SLAVEOF $REMOTEIP $PORT >> $LOGFILE 2>&1

redis_fault.sh 文件内容

#!/bin/bash

KPATH=/home/keepalived
RPATH=/home/cin_redis

REDISCLI=$RPATH/bin/redis-cli

LOGFILE=$KPATH/log/keepalived-redis-state.log

LOCALIP="192.168.11.150"
REMOTEIP="192.168.11.149"
PORT="15093"

echo "[ERROR]-----keepalived is fault,change local redis to slave-----" >> $LOGFILE
date >> $LOGFILE
echo "Beging slave..." >> $LOGFILE 2>&1
sleep 10
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p $PORT SLAVEOF $REMOTEIP $PORT >> $LOGFILE 2>&1

redis_stop.sh 文件内容

#!/bin/bash

KPATH=/home/keepalived
RPATH=/home/cin_redis

REDISCLI=$RPATH/bin/redis-cli

LOGFILE=$KPATH/log/keepalived-redis-state.log

LOCALIP="192.168.11.150"
REMOTEIP="192.168.11.149"
PORT="15093"

echo "[ERROR]-----keepalived is stop,change local redis to slave-----" >> $LOGFILE
date >> $LOGFILE
echo "Beging slave..." >> $LOGFILE 2>&1
sleep 10
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -h $LOCALIP -p $PORT SLAVEOF $REMOTEIP $PORT >> $LOGFILE 2>&1

redis_check.sh 文件内容

#!/bin/bash

KPATH=/home/keepalived
RPATH=/home/cin_redis

REDISCLI=$RPATH/bin/redis-cli

LOGFILE=$KPATH/log/keepalived-redis-state.log

LOCALIP="192.168.11.150"
REMOTEIP="192.168.11.149"
PORT="15093"

ALIVE=`$REDISCLI -h $LOCALIP -p $PORT PING`

if [ "$ALIVE" == "PONG" ];then
   echo "[INFO] `date +'%Y-%m-%d:%H:%M:%S'` :$LOCALIP local redis is health." >> $LOGFILE
   exit 0
else
   service keepalived stop
   echo "[ERROR] `date +'%Y-%m-%d:%H:%M:%S'` :$LOCALIP local redis is not health." >> $LOGFILE
   exit 1
fi

转载于:https://my.oschina.net/mjRao/blog/734129