码迷,mamicode.com
首页 > 其他好文 > 详细

redis keepalive+redis 主从高可用

时间:2019-09-17 18:50:27      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:ice   执行   redis配置   nop   tar   starting   开机   des   running   

1 准备

1.1 准备
keepalived-2.0.18.tar.gz
redis-5.0.4.tar.gz
1.2 说明
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时, Slave接管服务升级Master,有写权限,同时关闭主从复制功能;
当 Master 恢复正常, Master降级为Slave同步数据,开启主从复制。

1.3 测试服务器信息

Master redis: 192.168.56.103

Slave redis: 192.168.56.104

2. 配置redis 服务

1. 解压缩

tar zxf redis-5.0.4.tar.gz
mv redis-5.0.4 /soft/server
cd /soft/server
mv redis-5.0.4 redis

2. 编译安装 

cd /soft/server/redis
make && make install

3. redis配置文件修改

vim redis.conf
...
bind 0.0.0.0   # 修改支持网络访问
protected-mode no   # 同样一个安全配置,默认yes是不能网络访问的
daemonize yes   # 后台运行redis
slaveof 192.168.56.103 6379   # 指定Master IP,同步(仅从配置)

4. 配置redis服务启动脚本

技术图片
vim /etc/init.d/redis-server

#!/usr/bin/env bash
#
# redis start up the redis server daemon
#
# chkconfig: 345 99 99
# description: redis service in /etc/init.d/redis 
#            chkconfig --add redis or chkconfig --list redis 
#            service redis start  or  service redis stop
# processname: redis-server
# config: /etc/redis.conf
# 注意不同位置要修改变量
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/paic/rd6379/bin/redis-server
REDIS_CLI=/paic/rd6379/redis-cli
 
PIDFILE=/var/run/redis.pid
CONF="/soft/server/redis/redis.conf"
#make sure some dir exist
if [ ! -d /var/lib/redis ] ;then
    mkdir -p /var/lib/redis
    mkdir -p /var/log/redis
 
fi
 
case "$1" in
    status)
        ps -A|grep redis
        ;;
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        if [ "$?"="0" ]
        then
              echo "Redis is running..."
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $REDIS_CLI -p $REDISPORT SHUTDOWN
                while [ -x ${PIDFILE} ]
              do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
  restart|force-reload)
        ${0} stop
        ${0} start
        ;;
  *)
    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
    exit 1
 
esac
技术图片

5. 配置开机自启

chkconfig --add redis-server
chkconfig redis-server on

配置keepalive服务

1. 编译安装

tar zxf keepalived-1.2.22.tar.gz
cd keepalived-1.2.22
./configure --prefix=/usr/local/keepalived
make && make install

2. 配置文件设置

技术图片
# 拷贝keepalived服务
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived

# 配置keepalived服务
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
mkdir /etc/keepalived

# 拷贝配置文件
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
技术图片

3. keepalive配置文件

Master节点
技术图片
! Configuration File for keepalived
vrrp_script chk_redis {
 
    script "/root/scripts/redis_check.sh"    ###监控脚本 
    interval 2                         ###监控时间
    }
 
#网卡需要注意,使用ifconfig查看一下当前活动网卡
 
vrrp_instance VI_1 {
    state BACKUP                ###设置为BACKUP
    nopreempt                   # 不抢占MASTER
    interface eth0                ###监控网卡   
    virtual_router_id 51
    priority 100            ###权重值 
    authentication {
        auth_type PASS        ###加密 
        auth_pass 1q2w3e        ###密码
    }
    #设置额外的监控,里面的任意一个网卡出现问题,都会进入FAULT状态
    track_interface {
       eth0
    }
    track_script {
         chk_redis            ###执行上面定义的chk_redis 
    }
    virtual_ipaddress {
       192.168.0.99        ######VIP
    }
        notify_master /root/scripts/redis_master.sh
        notify_backup /root/scripts/redis_backup.sh
        notify_fault  /root/scripts/redis_fault.sh
        notify_stop  /root/scripts/redis_stop.sh
 
}
技术图片
Slave节点
技术图片
! Configuration File for keepalived
 
vrrp_script chk_redis {
    script "/root/scripts/redis_check.sh"    ###监控脚本 
    interval 2                  ###监控时间
    }
 
 #同样要注意网卡
 
vrrp_instance VI_1 {
    state BACKUP        ###设置为BACKUP 
    interface eth0      ###监控网卡
    virtual_router_id 51
    priority 10          ###比MASTRE权重值低 
    authentication {
        auth_type PASS
        auth_pass 1q2w3e        ###密码与MASTRE相同 
    }
    #设置额外的监控,里面的任意一个网卡出现问题,都会进入FAULT状态
    track_interface {
       eth0
    }
    track_script {
    chk_redis        ###执行上面定义的chk_redis 
    }
    virtual_ipaddress {
        192.168.0.99 ####vip
    }
        notify_master /root/scripts/redis_master.sh
        notify_backup /root/scripts/redis_backup.sh
        notify_fault  /root/scripts/redis_fault.sh
        notify_stop  /root/scripts/redis_stop.sh
}
技术图片

4. keepalive脚本

# Keepalive 定义的四种状态脚本说明
当进入Master状态时会呼叫notify_master
当进入Backup状态时会呼叫notify_backup
当发现异常情况时进入Fault状态呼叫notify_fault
当Keepalived程序终止时则呼叫notify_stop

 

redis_check.sh 检测redis是否正常脚本,正常则返回PONG,返回代码0,否则返回代码1

技术图片
#!/bin/bash 

ALIVE=`/usr/local/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ]; then
  echo $ALIVE 
  exit 0
else
  echo $ALIVE 
  exit 1
fi
技术图片

 

redis_master.sh 我被选入Master时,执行的脚本

技术图片
#!/bin/bash

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

LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
技术图片

 

redis_fault.sh 当检测服务不正常是,执行fault脚本

#!/bin/bash 

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

 

redis_stop.sh keepalive终止时写入日志

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

 

redis_backup.sh 进入slave时执行的脚本

技术图片
#!/bin/bash 
 
REDISCLI="/usr/local/bin/redis-cli"
 
LOGFILE="/var/log/keepalived-redis-state.log"
 
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1

echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.0.4 6379 >> $LOGFILE  2>&1  # IP为指定主redis的IP
技术图片

这里backup 脚本主要说明一下,我的keepalive配置了不去抢占Master,也就是当我Master 宕机后,我master再次起来就是slave,这时就是执行redis_backup的时候了,我去指定我的Master去同步

5. 配置开机自启

chkconfig --add keepalived
chkconfig keepalived on

redis keepalive+redis 主从高可用

标签:ice   执行   redis配置   nop   tar   starting   开机   des   running   

原文地址:https://www.cnblogs.com/adam19/p/11535983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!