keepalive与redis主从

Posted w329636271

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalive与redis主从相关的知识,希望对你有一定的参考价值。

keepalive与redis主从

本文只讲述keepalived如何配置才能保证redis的高可用;

安装与防火墙策略修改

yum install -y keepalived  #安装命令
systemctl enable keepalived  #配置开启自启动
#防火墙策略修改
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
 firewall-cmd --reload

关闭selinux

vim/etc/selinux/config

selinux = disable (修改成disable)

部署策略

192.168.28.133

redis 端口 6379 主

192.168.28.134

redis 端口 6379 从

192.168.28.110

虚拟端口

192.168.28.133配置

[zwy@localhost keepalived]$ cat 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 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
#   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0



#vrrp_script chk_mysql_port      #Check if mysql service is running. There are many ways, such as process, script detection, etc
#    script "/etc/keepalived/chk_mysql.sh"   #Script monitoring here
#    interval 2                   #Script execution interval, every 2s
#    weight -5                    #Priority change caused by script result. If the detection fails (the script returns non-0), the priority is - 5
#    fall 2                    #If the detection fails twice in a row, it is determined to be a true failure. Reduce priority with weight (between 1-255)
#    rise 1                    #A successful test is a success. But do not change priority
#


vrrp_script chk_redis 
    script "/etc/keepalived/script/redis_check.sh"
    interval 3
    fall 3
    rise 1



vrrp_instance VI_1 
    state MASTER
    interface ens33 
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        192.168.28.110
    


    track_script 
        chk_redis
    
    notify_master /etc/keepalived/script/redis_master.sh
    notify_backup /etc/keepalived/script/redis_backup.sh
    notify_fault  /etc/keepalived/script/redis_fault.sh  
    notify_stop   /etc/keepalived/script/redis_stop.sh



脚本修改:

需要注意的是看redis-cli的安装路径,以及需要配置redis_master.sh和redis_back.sh的中的ip的修改;

script/redis_backup.sh

#!/bin/bash 

REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
IP="192.168.28.134"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >>$LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色 
echo "backup Run SLAVEOF cmd ...">> $LOGFILE


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


script/redis_check.sh

#!/bin/bash 
ALIVE=`/usr/local/bin/redis-cli  PING` 
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE 
exit 0 
else
echo $ALIVE 
exit 1 
fi

script/redis_fault.sh

#!/bin/bash 


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


script/redis_master.sh

#!/bin/bash 
REDISCLI="/usr/local/bin/redis-cli "
LOGFILE="/var/log/keepalived-redis-state.log"
#另外一台redis的ip
IP="192.168.28.134"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >>$LOGFILE 2>&1
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF $IP  6379  >>$LOGFILE  2>&1
sleep 20 #延迟10秒以后待数据同步完成后再取消同步状态 
echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1


script/redis_stop.sh

#!/bin/bash 


LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
# 添加可执行权限
chmod +x script/*.sh
#重启keepalived
service keepalived restart

192.168.28.134配置

/etc/keepalived

! 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 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
#   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0





vrrp_script chk_redis 
    script "/etc/keepalived/script/redis_check.sh"
    interval 3
    fall 3
    rise 1



vrrp_instance VI_1 
    state BACKUP
    interface ens33 
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        192.168.28.110
    


    track_script 
        chk_redis
    
    notify_master /etc/keepalived/script/redis_master.sh
    notify_backup /etc/keepalived/script/redis_backup.sh
    notify_fault  /etc/keepalived/script/redis_fault.sh  
    notify_stop   /etc/keepalived/script/redis_stop.sh

脚本修改:

需要注意的是看redis-cli的安装路径,以及需要配置redis_master.sh和redis_back.sh的中的ip的修改;

script/redis_backup.sh

#!/bin/bash 
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
IP="192.168.28.133"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >>$LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色 
echo "backup Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF $IP  6379 >>$LOGFILE  2>&1


script/redis_check.sh

#!/bin/bash 
ALIVE=`/usr/local/bin/redis-cli  PING` 
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE 
exit 0 
else
echo $ALIVE 
exit 1 
fi

script/redis_fault.sh

#!/bin/bash 


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


script/redis_master.sh

#!/bin/bash 
REDISCLI="/usr/local/bin/redis-cli "
LOGFILE="/var/log/keepalived-redis-state.log"
#另外一台redis的ip
IP="192.168.28.133"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >>$LOGFILE 2>&1
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF $IP  6379  >>$LOGFILE  2>&1
sleep 20 #延迟10秒以后待数据同步完成后再取消同步状态 
echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1


script/redis_stop.sh

#!/bin/bash 


LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
# 添加可执行权限
chmod +x script/*.sh
#重启keepalived
service keepalived restart

redis 虚拟ip访问

2022.06.15

如果需要redis需要密码,可以redis.cnf配置对应的同步ip端口以及密码,

另外注意script中的脚本权限; 可能会导致keepalive检测问题;

资源脚本:https://download.csdn.net/download/w329636271/84385594

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

以上是关于keepalive与redis主从的主要内容,如果未能解决你的问题,请参考以下文章

keepalive与redis主从

Redis主从复制

Redis数据库系列Redis主从复制

Redis 7.0 共享复制缓冲区的设计与实现

Redis 主从+哨兵安装与部署

⭐Redis分布式——主从复制Sentinel集群彻底吃透⭐(看完这篇万字长文,你的Redis水平将会上升一个层次)