基于Keepalived对Redis做高可用配置

2022-07-03 17:11:21 浏览数 (1)

基于Keepalived对Redis做高可用配置

关于Keepalived的详细介绍,请移步本人相关文章:http://www.linuxidc.com/Linux/2014-12/110815.htm

功能

ip地址

安装软件

主redis

10.13.6.13

redis,keepalived

从redis

10.13.6.16

redis,keepalived

VIP

10.13.6.17

一、redis主从搭建 1.redis安装

本文通过yum源的安装方式来安装(需要配置epel源),也可以通过源码编译的方式来安装 

[root@P2Pp_Red01 ~]# yum install -y redis

[root@P2Pp_Red02 ~]# yum install -y redis

2.修改配置文件

 ①、主redis需要修改的文件及内容

[root@P2Pp_Red01 ~]# vi /etc/redis.conf

bind 0.0.0.0

其余都可以安装默认状态

②、从redis需要修改的文件及内容

[root@P2Pp_Red02 ~]# vi /etc/redis.conf

bind 0.0.0.0

slaveof 10.13.6.13 6379 //指定主redis的地址与端口

3.查看主从状态

[root@P2Pp_Red01 ~]# redis-cli INFO |grep role -A 3

role:master

slave0:10.13.6.16,45270,online

db0:keys=6,expires=0

[root@P2Pp_Red02 ~]#  redis-cli INFO |grep role -A 3

role:slave

master_host:10.13.6.13

master_port:6379

master_link_status:up

二、使用keepalived对redis做高可用

1.安装keepalived,两台机器都需要安装

[root@P2Pp_Red01 ~]# yum install -y keepalived

[root@P2Pp_Red02 ~]# yum install -y keepalived

2.准备配置文件

①、主redis的配置文件,关于各行代表的意思,请移步我的另外一篇文档,链接见文章开头

[root@P2Pp_Red01 scripts]# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

  notification_email {

    acassen@firewall.loc

    failover@firewall.loc

    sysadmin@firewall.loc

  }

  notification_email_from Alexandre.Cassen@firewall.loc

  smtp_server 10.13.4.17

  smtp_connect_timeout 30

  router_id LVS_DEVEL

}

vrrp_script chk_redis {

    script "killall -0 redis-server"

    interval 2

}

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id  100

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        10.13.6.17

    }

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

}

~[root@P2Pp_Red01 ~]# vi /etc/keepalived/scripts/redis_master.sh

#!/bin/bash

REDISCLI="/usr/bin/redis-cli"

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 10.13.4.13 6379 >> $LOGFILE  2>&1

sleep 10

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

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

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

~

[root@P2Pp_Red01 ~]# vi /etc/keepalived/scripts/redis_backup.sh

#!/bin/bash

REDISCLI="/usr/bin/redis-cli"

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 10.13.4.13 6379 >> $LOGFILE  2>&1

[root@P2Pp_Red01 scripts]# vi /etc/keepalived/scripts/redis_stop.sh

#!/bin/bash

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

echo "[stop]" >> $LOGFILE

date >> $LOGFILE

[root@P2Pp_Red01 scripts]# vi /etc/keepalived/scripts/redis_fault.sh

#!/bin/bash

GFILE=/var/log/keepalived-redis-state.log

echo "[fault]" >> $LOGFILE

date >> $LOGFILE

~

②、准备从redis的配置文件

[root@P2Pp_Red02 ~]# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

  notification_email {

    acassen@firewall.loc

    failover@firewall.loc

    sysadmin@firewall.loc

  }

  notification_email_from Alexandre.Cassen@firewall.loc

  smtp_server 10.13.4.13

  smtp_connect_timeout 30

  router_id LVS_DEVEL

}

vrrp_script chk_redis {

    script "killall -0 redis-server"

    interval 1

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 100

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        10.13.6.17

    }

#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

[root@P2Pp_Red02 ~]# vi /etc/keepalived/scripts/redis_master.sh

#!/bin/bash

REDISCLI="/usr/bin/redis-cli"

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 10.13.6.13 6379 >> $LOGFILE  2>&1

sleep 10

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

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

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

~

[root@P2Pp_Red02 ~]# vi /etc/keepalived/scripts/redis_backup.sh

#!/bin/bash

REDISCLI="/usr/bin/redis-cli"

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

echo "[backup]" >> $LOGFILE

date >> $LOGFILE

echo "Being slave....

sleep 15

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

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

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

其余的两个文件跟主redis是一样的

本处将不再给模拟故障,但是测试时通过的,现在已经在生产环境中使用

0 人点赞