keepalived redis主从双机热备部署

搭建环境看了很多帖子,因为诸多原因都有些原因导致,失败,因此记录一下自己的搭建步骤。


为了提高程序响应效率,用到了redis作为存放缓存数据的地方,但是只运行一台redis无疑对线上环境是一种挑战,万一服务器宕机,那生成环境就没法玩了,因此需要对redis做集群配置,由于公司业务量不是很大,所以采用主从双机热备的方案。

主要核心思想,如果master 和 salve 全部存活的情况,VIP就漂移到 master。读写都从master操作,如果master宕机,VIP就会漂移到salve,并将之前的salve切换为master,当宕机的master可以继续服务的时候,首先会从salve同步数据,然后VIP漂移到master服务器上面,持续提供服务。

一、准备环境

    1.两台服务器

        192.168.0.55 master

        192.168.0.56 slave

        192.168.0.99 VIP

    2.redis安装包

        下载地址:redis下载 (本人下载的稳定版 4.0.9)

    3.keeplived安装包

二、安装redis(两台服务器都按照此方式安装)

    1、解压安装包

        tar -zxvf redis-stable.tar.gz 

    2、将文件放于 /usr/local

        mv redis-stable /usr/local/

    3、进入redis目录并重命名

        cd /usr/local/

        mv redis-stable/ redis

    4、进入 redis目录,编译

        cd redis

        make 

        keepalived redis主从双机热备部署_第1张图片

        到这步就编译成功了、但是发现    有个错误,经检测发现是时间不对

        所以修改时间  

        date  -s "2018-3-31 15:24:00"  

        让系统时间和硬件时间同步:

        hwclock --systohc

        keepalived redis主从双机热备部署_第2张图片

        最后通过date 查看时间无误了。再编译一次 在 redis目录中再次make

        keepalived redis主从双机热备部署_第3张图片

       5、 然后执行 make test

        

        测试的时候报错了,需要安装 tcl8.5

        5.1 、去下载tcl8.5

        5.2、解压并放入 /usr/local

        5.3、进入tcl 下的unix 编译安装

            cd tcl8.6.1/unix/

            ./configure

            make && make install

            keepalived redis主从双机热备部署_第4张图片

        6、进入 /usr/local/redis/src 执行 make install

            keepalived redis主从双机热备部署_第5张图片

        7、新建 redis 下新建 bin 及 etc目录 存放redis 服务相关文件和配置文件

            mkdir /usr/local/redis/etc /usr/local/redis/bin

            

        8、移动相关文件

            mv /usr/local/redis/redis.conf /usr/local/redis/etc/

            cd /usr/local/redis/src

           mv  mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server /usr/local/redis/bin/

        9、修改配置文件、让服务可以后台启动

            vim /usr/local/redis/etc/redis.conf

            daemonize  yes

            requirepass 123456 (连接密码)

        10、启动 redis(后台启动)

            [root@jonyLinux1 redis]# ./bin/redis-server ./etc/redis.conf

        11、测试是否成功(保存、查找)

            

        12、配置服务

            12.1、创建文件

                touch /etc/init.d/redis

            12.2、编写启动文件

                vim /etc/init.d/redis

            12.3、文件内容

#!/bin/sh
# chkconfig: 2345 10 90  
# description: Start and Stop redis   

REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/etc/redis.conf"
REDISPASS=123456

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF &
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT -a $REDISPASS shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    restart)
        "$0" stop
        sleep 3
        "$0" start
        ;;
    *)
        echo "Please use start or stop or restart as first argument"
        ;;
esac

            12.4、配置开机启动

                chmod +x /etc/init.d/redis

                chkconfig --add redis

                chkconfig redis on

    三、redis双机热备配置

            1、相关配置

                192.168.0.55 master中配置    

                    daemonize  yes

                    requirepass 123456

                    bind 0.0.0.0

                    slave-serve-stale-data yes

                    slave-read-only no

                192.168.0.56 slave中配置

                    daemonize  yes

                    slaveof 192.168.0.55 6379

                    masterauth 123456

                    slave-serve-stale-data yes

                    slave-read-only no

            2、验证是否成功

                    192.168.0.55 master 中

                        

                    192.168.0.56 slave 中(先get weater 无值、master set 成功之后 slave 中 有值)

                        keepalived redis主从双机热备部署_第6张图片

    四、keepalived安装

        1、解压压缩包

                tar -zxvf keepalived-1.4.0.tar.gz

        2、安装所需镜像

                yum install -y openssl openssl-devel

        3、编译安装

                cd keepalived-1.4.0

                ./configure -prefix=/usr/local/keepalived/

                make && make install

        4、拷贝keepalived配置文件

            cp /opt/keepalived-1.4.0/keepalived/etc/init.d/keepalived /etc/init.d/

            cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived

            cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived

            mkdir -p /etc/keepalived/scripts/

            cp /usr/local/keepalived/etc/keepalived/samples/keepalived.conf.virtualhost

/etc/keepalived/keepalived.conf

        5、修改配置文件及脚本

               5.1、修改master keepalived配置文件

              vim /etc/keepalived/keepalived.conf      

vrrp_script chk_redis {
    script "killall -0 redis-server"
    interval 2
}
#网卡需要注意,使用ifconfig查看一下当前活动网卡
vrrp_instance VI_1 {
    state MASTER
    interface eth0   #监控网卡(一定要设置正确)
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    track_script {
        chk_redis
    }
    virtual_ipaddress {
        192.168.0.99  #VIP地址
    }

    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
}

             5.2、修改salve keepalived配置文件

                  vim /etc/keepalived/keepalived.conf

vrrp_script chk_redis {
   # script "killall -0 redis-server"
    interval 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    track_script {
        chk_redis
    }
    virtual_ipaddress {
        192.168.0.99
    }

    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
}

    6、编写监控脚本

        6.1、master中脚本

            6.1.1 redis_master.sh文件

                touch /etc/keepalived/scripts/redis_master.sh

                vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli -a 123456"
LOGFILE="/var/log/keepalived-redis-state.log"

sleep 15

echo "[master]" >> $LOGFILE

date >> $LOGFILE

echo "Being master...." >>$LOGFILE 2>&1


echo "Run SLAVEOF cmd ...">> $LOGFILE

$REDISCLI SLAVEOF 192.168.0.58 6379 >>$LOGFILE  2>&1
if [ $? -ne 0 ];then
    echo "data rsync fail." >>$LOGFILE 2>&1
else
    echo "data rsync OK." >> $LOGFILE  2>&1
fi

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态 

echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE

$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
if [ $? -ne 0 ];then
    echo "Run SLAVEOF NO ONE cmd fail." >>$LOGFILE 2>&1
else
    echo "Run SLAVEOF NO ONE cmd OK." >> $LOGFILE  2>&1
fi
            6.1.2 redis_backup.sh文件

                    touch /etc/keepalived/scripts/redis_backup.sh

                    vim /etc/keepalived/scripts/redis_backup.sh

#!/bin/bash 

REDISCLI="/usr/local/redis/bin/redis-cli -a 123456"

LOGFILE="/var/log/keepalived-redis-state.log"



echo "[backup]" >> $LOGFILE

date >> $LOGFILE

echo "Being slave...." >>$LOGFILE 2>&1


sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色 

echo "Run SLAVEOF cmd ...">> $LOGFILE

$REDISCLI SLAVEOF 192.168.0.56 6379 >>$LOGFILE  2>&1

        6.2、slave中脚本

            6.2.1、redis_master.sh 脚本

                touch /etc/keepalived/scripts/redis_master.sh

                vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash 

REDISCLI="/usr/local/redis/bin/redis-cli -a 123456"

LOGFILE="/var/log/keepalived-redis-state.log"



echo "[master]" >> $LOGFILE

date >> $LOGFILE

echo "Being master...." >>$LOGFILE 2>&1



echo "Run SLAVEOF cmd ...">> $LOGFILE

$REDISCLI SLAVEOF 192.168.0.55 6379 >>$LOGFILE  2>&1

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态 



echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE

$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

             6.2.2、redis_backup.sh 脚本

                    touch /etc/keepalived/scripts/redis_backup.sh

                    vim /etc/keepalived/scripts/redis_backup.sh

#!/bin/bash 

REDISCLI="/usr/local/redis/bin/redis-cli -a 123456"

LOGFILE="/var/log/keepalived-redis-state.log"


echo "[backup]" >> $LOGFILE

date >> $LOGFILE

echo "Being slave...." >>$LOGFILE 2>&1


sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色 

echo "Run SLAVEOF cmd ...">> $LOGFILE

$REDISCLI SLAVEOF 192.168.0.55 6379 >>$LOGFILE  2>&1
                                                           

           6.3、master 和 salve 中同时创建脚本

                6.3.1 创建 redis_fault.sh

                    touch /etc/keepalived/scripts/redis_fault.sh

                    vim /etc/keepalived/scripts/redis_fault.sh

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

                 6.3.2、创建 redis_stop.sh

                    touch /etc/keepalived/scripts/redis_stop.sh

                    vim /etc/keepalived/scripts/redis_stop.sh

lOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE

      7、keepalived 最终脚本目录如下

        master(55) 中

        keepalived redis主从双机热备部署_第7张图片

        salve(56) 中

         keepalived redis主从双机热备部署_第8张图片

到此所有配置文件全部设置完毕

然后再 maseter和 salve 中 执行:

chmod +x /etc/keepalived/scripts/*.sh

五、验证成功

    1、启动master 和 salve中 的redis

        service redis restart

    2、启动master 和 salve中的 keepalived

        service keepalived restart

    3、用VIP连接redis

        redis-cli -h 192.168.0.99 -a 123456

        

        3.1、输入 info查看redis信息

        keepalived redis主从双机热备部署_第9张图片

        可以看到当前状态为master 以及 salve (图中IP58,由于内网56被占用了,只好将salve的IP改为58)

        keepalived redis主从双机热备部署_第10张图片

     4、同步测试

            4.1、master服务器中

            keepalived redis主从双机热备部署_第11张图片

            salve 服务器中

            

            数据已同步。

            4.2、停掉master redis 服务,测试同步

            

            查看replication slaves 为0

            keepalived redis主从双机热备部署_第12张图片

            4.3、新增 redis key

            keepalived redis主从双机热备部署_第13张图片

            4.4、重启 master 中的redis,并查看redis 数据同步状态

            keepalived redis主从双机热备部署_第14张图片

            启动master中的redis,然后查看keys 还未同步,稍等片刻发现 刚刚再slave中新增的数据已同步。

        以上为本人 使用keepalived+redis 双机热备实际操作步骤,以留记录,方便以后再次使用,也方便看到此文的同学。


本环境搭建成功,要感谢诸多同学的贡献,以下为主要参考的一些帖子内容:

http://abcve.com/redis-keepalived/

http://blog.51cto.com/wangfeng7399/1591272

https://www.cnblogs.com/davidwang456/p/3525090.html

https://www.cnblogs.com/JeremyWYL/p/8562388.html

你可能感兴趣的:(中间件)