keepalived 实现redis 高可用

1、安装keepalived

yum -y install keepalived

配置keepalived, 主节点和从节点都是BACKUP

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
    router_id redis
}
# keepalived监控脚本
vrrp_script chk_redis {
    #keepalived 健康检测执行脚本
    script /usr/local/redis/keepalived/scripts/redis-check.sh    
    #每隔几秒发一次健康检测请求
    interval 2  
    #自我确定健康异常, 优先级加多少, priority += weight
    #当脚本返回非0, 则认为健康异常, 优先级 + -10 
    weight -10  
    #检测失败几次,认为是redis 服务器挂了
    fall 3      
}
# 实例
vrrp_instance redis {
    # 主和从都是BACKUP
    state BACKUP 
    #eth0 表示监听哪块儿网卡    
    interface eth0
    #主从一致   
    virtual_router_id 51
    #优先级, backup1和backup2都设为100, 重要, 关系到vip漂移问题 
    priority 100
    # 发送vrrp通告间隔, 对比优先级     
    advert_int 1
    virtual_ipaddress {
        #虚拟的ip 是多少
       192.168.203.240  
    }
    # 健康检查脚本
    track_script {
       chk_redis
    }
    #keepalived 内部通信,本机ip 地址
    unicast_src_ip  192.168.203.129 
    unicast_peer {
       #指定其它keepalived 地址,如果这个不指定,可能出现,主从都虚拟出了192.168.203.240 ip地址
       192.168.203.130  
    }
    #keepalived 被推选为主服务器器时执行的脚本
    notify_master /usr/local/redis/keepalived/scripts/redis-master.sh  
    #keepalived 被降级为从服务器时执行的脚本
    notify_backup /usr/local/redis/keepalived/scripts/redis-backup.sh  
    #keepalived 运行出现错误的时候执行的脚本
    notify_fault /usr/local/redis/keepalived/scripts/redis-fault.sh    
    #keepalived 服务停止时执行脚本
    notify_stop /usr/local/redis/keepalived/scripts/redis-stop.sh      
}

backup2 keepalived配置和backup1区别如下:

vrrp_instance redis {
    unicast_src_ip  192.168.203.130
    unicast_peer {
       192.168.203.129
    }
}

 2、创建监控脚本

除redis-backup.sh外, 其他脚本在backup1 和2 上都保持一致

创建脚本保存目录, 以及日志保存目录

mkdir -p /usr/local/redis/keepalived/scripts
mkdir -p /usr/local/redis/keepalived/logs

redis-check.sh 脚本: 用于检测redis 服务健康状态

vim /usr/local/redis/keepalived/scripts/redis-check.sh

#!/bin/bash   

#日志文件位置  
logFile=/usr/local/redis/keepalived/logs/check.log

#ping 本机redis服务  
pingRS=`/usr/local/redis/bin/redis-cli PING`

#如果ping 的结果为PONG,那么返回0 ,否则返回1  
if [ "$pingRS"x == "PONG"x ]  ; then  
   exit 0
else
   echo "[`date`] ping is error !"  >> $logFile
   exit 1
fi

redis-master.sh 脚本: keepalived 被推选为主服务器时执行

vim /usr/local/redis/keepalived/scripts/redis-master.sh

#!/bin/bash  

# redis-cli 命令绝对路径  
cliCmd=/usr/local/redis/bin/redis-cli

# keepalived 日志文件位置  
logFile=/usr/local/redis/keepalived/logs/master.log

echo "`[date]` master " >> $logFile

# 成为主节点则redis需要取消复制, 也成为主节点  
$cliCmd  SLAVEOF NO ONE &>>$logFile

redis-backup.sh 脚本: keepalived 被降级为从服务器时执行

backup1 和 backup2 中$cliCmd SLAVEOF 192.168.203.130 6379不一致, 需要修改
vim /usr/local/redis/keepalived/scripts/redis-backup.sh

#!/bin/bash  

#日志文件  
logFile=/usr/local/redis/keepalived/logs/backup.log

# redis-cli 命令绝对路径  
cliCmd=/usr/local/redis/bin/redis-cli
  
echo "[`date`] begin to slave ..." >> $logFile


# 成为从节点, 需要检测redis是否启动, 没有启动, 则启动redis
service redis-server start
# 设置0,5s睡眠, 重要, 不要大 , 也不要小, 
# 太小, 下一步设置主从不成功(原因还未知)
# 太大, 如5s, 在5s期间, 节点会由backup转成master, 5s后才会执行slaveof, redis出现问题
sleep 0.5

# 设置主从关系
# keepalived成为从节点后, redis需要后成为兄弟节点的从节点
# backup2 此行为$cliCmd  SLAVEOF 192.168.203.129 6379
$cliCmd  SLAVEOF 192.168.203.130 6379

echo  "[`date`] slave done !" $ >> $logFile

redis-fault.sh 脚本: keepalived 执行出现错误时执行
vim /usr/local/redis/keepalived/scripts/redis-fault.sh

#!/bin/bash  
#Desc keepalived 发生错误时执行脚本   

# keepalived 日志文件位置  
logFile=/usr/local/redis/keepalived/logs/fault.log

# 向日志输出错误信息  
echo "[$(date)] *****  redis falut  ***" >> $logFile

edis-stop.sh 脚本: keepavlied 服务停止时执行
vim /usr/local/redis/keepalived/scripts/redis-stop.sh

#!/bin/bash  
#Desc keepalived 停止时执行脚本  

#日志文件  
logFile=/usr/local/redis/keepalived/logs/stop.log

#输出日志信息  
echo "[`date`] stop ..." >> $logFile

脚本添加可执行权限

cd /usr/local/redis/keepalived/scripts
chmod u+x *.sh

在/etc/rc.local中添加启动

vim /etc/rc.local

service redis-server start
service keepalived start

上面的 keepalived、redis 启动方式需要自己设置;redis的service启动方式,复制 utils/redis**-scripts 到etc/init.d/ 下面并且修改对应参数,再添加启动方式即可

  • 启动backup1(主节点)和backup2(从节点)的redis, 并通过slave of设置redis主从
  • 启动主节点的keepalived, 抢占vip
  • 启动从节点的keepalived, 成为从节点
  • ip a查看各机器ip, 是否绑定了vip

最后运行前需要关闭 关闭selinux ,不然脚本不会运行

临时关闭:

setenforce 0

永久关闭:

sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config

 

你可能感兴趣的:(redis,redis,keepalived,高可用,主从切换,master,slave)