keepalived实现redis或其他服务高可用 - Choyeon(初雨博客)

/ 0评 / 0

keepalived redis

Master (主)Redis服务器 keepalived配置

安装keepalive

/usr/local/src/
1. wget https://www.keepalived.org/software/keepalived-2.1.5.tar.gz
2. tar zxf keepalived-2.1.5.tar.gz
3. cd keepalived-2.1.5
4. ./configure --prefix=/usr/local/keepalived/
5. make
6. make install

安装可能需要的库

yum -y install openssl-devel libnl3-devel ipset-devel iptables-devel libnfnetlink-devel net-snmp-devel

为了避免麻烦 建议测试请关闭防火墙

systemctl stop firewalld

并在生产环境把指定端口

主服务器配置

 vim /etc/keepalived/keepalived.conf
global_defs {
    router_id redis
    dynamic_interfaces
    script_user root
    enable_script_security
}

vrrp_script chk_redis {
        script "/etc/keepalived/scripts/check_redis.sh"
        interval 4
        weight -5
        fall 3
        rise 2
}
vrrp_instance VI_REDIS {
        state MASTER
        interface ens33
        virtual_router_id 51
        priority 100
        advert_int 1

        authentication {
                auth_type PASS
                auth_pass 1111
        }

        virtual_ipaddress {
                192.168.1.23
        }

        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/redsi_stop.sh
}
global_defs {
        router_id redis
    dynamic_interfaces
    script_user root
    enable_script_security
}

vrrp_script chk_redis {
        script "/etc/keepalived/scripts/check_redis.sh"
        interval 4
        weight -5
        fall 3
        rise 2
}

vrrp_instance VI_REDIS {
        state BACKUP
        interface ens33
        virtual_router_id 51
        priority 90
        advert_int 1
        nopreempt

        authentication {
                auth_type PASS
                auth_pass 1111
        }

        virtual_ipaddress {
                192.168.1.23
        }

        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/redsi_stop.sh
}
在Redis Master和Redis Backup上将keepalived启动

启动keepalived:

service keepalived start

或者

/etc/init.d/keepalived start

或者

/usr/local/keepalived/sbin/keepalived -f/etc/keepalived/keepalived.conf -D

主服务器脚本配置

在活跃状态

vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/usr/local/redis-4.0.10/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.1.22 6379 >> $LOGFILE  2>&1
sleep 13 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
redis-cli SLAVEOF NO ONE

在备用状态 本配置中不会生效 因为如果主服务器活跃就会抢回VIP

vim /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/usr/local/redis-4.0.10/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.1.22 6379 >> $LOGFILE  2>&1

停止时执行脚本

vim /etc/keepalived/scripts/redis_stop.sh

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

故障执行的脚本

vim /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE=/usr/local/redis-4.0.10/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE

备用服务器脚本配置

在活跃状态

vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/usr/local/redis-4.0.10/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.1.21 6379 >> $LOGFILE  2>&1
sleep 13 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
redis-cli SLAVEOF NO ONE

在备用状态 本配置中不会生效 因为如果主服务器活跃就会抢回VIP

vim /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/usr/local/redis-4.0.10/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.1.21 6379 >> $LOGFILE  2>&1

停止时执行脚本

vim /etc/keepalived/scripts/redis_stop.sh

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

故障执行的脚本

vim /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE=/usr/local/redis-4.0.10/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE

keepalived根据监控脚本的返回码调整优先级:☉如果脚本返回码为0,并且weight配置的值大于0,则优先级相应的增加;☉如果脚本返回码为非0,并且weight配置的值小于0,则优先级相应的减少;☉其他情况,原本配置的优先级不变,即配置文件中priority对应的值。提示:优先级不会不断的提高或者降低;可以编写多个检测脚本并为每个检测脚本设置不同的weight(在配置中列出就行);不管提高优先级还是降低优先级,最终优先级的范围是在[1,254],不会出现优先级小于等于0或者优先级大于等于255的情况;在MASTER节点的 vrrp_instance 中 配置 nopreempt ,当它异常恢复后,即使它 prio 更高也不会抢占,这样可以避免正常情况下做无谓的切换。以上可以做到利用脚本检测业务进程的状态,并动态调整优先级从而实现主备切换。

注意,在同一个网段内的,若为不同的应用做高可用,不同应用使用不同的VIP,那么vrrp_instance的名字(这里是VI_REDIS)、virtual_router_id在不同的高可用实例必须设置不同的值区分开

配置选项说明

global_defs

☉notification_email: keepalived在发生诸如切换操作时需要发送email通知地址,后面的 smtp_server 相比也都知道是邮件服务器地址。也可以通过其它方式报警,毕竟邮件不是实时通知的。

☉router_id: 机器标识,通常可设为hostname。故障发生时,邮件通知会用到

vrrp_instance

☉state : 指定instance(Initial)的初始状态,就是说在配置好后,这台服务器的初始状态就是这里指定的,但这里指定的不算,还是得要通过竞选通过优先级来确定。如果这里设置为MASTER,但如若他的优先级不及另外一台,那么这台在发送通告时,会发送自己的优先级,另外一台发现优先级不如自己的高,那么他会就回抢占为MASTER

☉interface: 实例绑定的网卡,因为在配置虚拟IP的时候必须是在已有的网卡上添加的,可以用ifconfig命令查看网卡。

☉mcast_src_ip: 发送多播数据包时的源IP地址,这里注意了,这里实际上就是在那个地址上发送VRRP通告,这个非常重要,一定要选择稳定的网卡端口来发送,这里相当于heartbeat的心跳端口,如果没有设置那么就用默认的绑定的网卡的IP,也就是interface指定的IP地址

☉virtual_router_id: 这里设置VRID,这里非常重要,相同的VRID为一个组,他将决定多播的MAC地址

☉priority: 设置本节点的优先级,优先级高的为master

☉advert_int: 检查间隔,默认为1秒。这就是VRRP的定时器,MASTER每隔这样一个时间间隔,就会发送一个advertisement报文以通知组内其他路由器自己工作正常

☉authentication: 定义认证方式和密码,主从必须一样,样例用的是密码方式。

☉virtual_ipaddress: 这里设置的就是VIP,也就是虚拟IP地址,他随着state的变化而增加删除,当state为master的时候就添加,当state为backup的时候删除,这里主要是有优先级来决定的,和state设置的值没有多大关系。这里可以设置多个虚拟IP地址,类似于一个域名可以解析对应多个IP地址。

☉track_script: 引用VRRP脚本,即在 vrrp_script 部分指定的名字。每隔vrrp_script->interval时间运行脚本,如果监控服务有异常则改变优先级,并最终引发主备切换。

vrrp_script

告诉 keepalived 在什么情况下切换,所以尤为重要。可以有多个 vrrp_script

☉script : 自己写的检测脚本。也可以是一行命令如killall-0 nginx

☉interval4 : 每4s检测一次,这里要大于监控脚本执行的时间,监控脚本会执行超时,☉keepalived会发送SIGTERM信号结束监控脚本的执行。

☉weight-5 : 检测失败(脚本返回非0)则优先级 -5

☉fall 2: 检测连续 2 次失败才算确定是真失败。会用weight减少优先级(1-255之间)

☉rise 1: 检测 1 次成功就算成功。但不修改优先级

启动Redis

确保服务器已经安装和配置好Redis

启动Redis Master和Redis Backup

先在Redis Backup配置 SLAVEOF 为 Redis Master 的ip和端口

SLAVEOF 192.168.1.21 6379

启动

redis-server # 如果不是默认配置 这里输入所用配置路径文件
redis-cli

启动keepalived

在Redis Master和Redis Backup上将keepalived启动

启动keepalived:

service keepalived start

或者

/etc/init.d/keepalived start

或者

/usr/local/keepalived/sbin/keepalived -f/etc/keepalived/keepalived.conf -D

发表评论

邮箱地址不会被公开。 必填项已用*标注