四、Redis+Keepalived 主从热备部署方案

目录

 

一、备份方式

二、安装部署keepalived

三、安装部署redis


一、备份方式

Redis部署方式采用主从的方式,通过keepalived来对外提供虚IP,并实现主从自动切换功能。
主实例A(master):188.102.2.96
备实例B(slave):188.102.2.97
虚IP(VIP):188.102.2.98

正常工作时:虚IP在主实例A上,数据同步备份:A→B
主实例A宕机:主实例B自动接管IP,并将redis转换为主模式,待原主实例A恢复后,A将自动转化为备模式,数据同步备份:A←B,主备角色互换,实现容灾。

二、安装部署keepalived

1、安装keepalived

# 安装依赖
yum install -y curl gcc openssl-devel popt-devel libnl3-devel net-snmp-devel
#下载包
wget https://www.keepalived.org/software/keepalived-2.0.17.tar.gz
# 解压并安装
tar zxvf keepalived-2.0.17.tar.gz
cd keepalived-2.0.17
./configure –prefix=/data/keepalived  #指定安装到/data/keepalived目录下
make
make install

2、配置keepalived
   (1)首先在master上创建如下配置:

#配置keepalived.conf
vim /data/keepalived/keepalived.conf

# 加入如下内容脚本
vrrp_script chk_redis {
    script "/data/keepalived/scripts/redis_check.sh"   ###监控脚本 
    interval 2                                         ###监控时间
}
vrrp_instance VI_1 {
        state MASTER                            ###设置为MASTER
        interface ens160                        ###监控网卡
        virtual_router_id 51
        priority 101                            ###权重值,比slave值高
        authentication {
            auth_type PASS         ###加密
            auth_pass redis        ###密码 与 slave 相同
			}
        track_script {
            chk_redis                  ###执行上面定义的chk_redis
        }
        virtual_ipaddress {
             188.102.2.98              ###VIP
        }
        notify_master /data/keepalived/scripts/redis_master.sh
        notify_backup /data/keepalived/scripts/redis_backup.sh
        notify_fault  /data/keepalived/scripts/redis_fault.sh
        notify_stop   /data/keepalived/scripts/redis_stop.sh
}
# 配置redis_master.sh
vim /data/keepalived/scripts/redis_master.sh

#!/bin/bash
 
REDISCLI="/data/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
REDISPORT=7001
REDIS_MASTER=188.102.2.96
REDIS_SLAVE=188.102.2.97
 
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
 
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -a redis -h $REDIS_MASTER -p $REDISPORT SLAVEOF $REDIS_SLAVE $REDISPORT >> $LOGFILE  2>&1
sleep 10 #10̬ͬ 
 
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI -a redis -h $REDIS_MASTER -p $REDISPORT SLAVEOF NO ONE >> $LOGFILE 2>&1
#配置redis_backup.sh
vim /data/keepalived/scripts/redis_backup.sh

#!/bin/bash
 
REDISCLI="/data/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
REDISPORT=7001
REDIS_MASTER=188.102.2.96
REDIS_SLAVE=188.102.2.97
 
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
 
sleep 15 #15

             echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -a redis -p $REDISPORT SLAVEOF $REDIS_SLAVE $REDISPORT >> $LOGFILE  2>&1

 (2)在slave上创建如下配置:
 

#配置keepalived.conf
vim /data/keepalived/keepalived.conf

vrrp_script chk_redis {
                script "/data/keepalived/scripts/redis_check.sh"   ###监控脚本
                interval 2                                        ###监控时间
}
vrrp_instance VI_1 {
        state BACKUP                                ###设置为BACKUP
        interface ens160                            ###监控网卡
        virtual_router_id 51
        priority 100                                ###权重,比ASTRE值低
        authentication {
                     auth_type PASS                 ###加密
                     auth_pass redis                ###密码与MASTRE相同
        }
        track_script {
                chk_redis                          ###执行上边定义的 chk_redis
        }
        virtual_ipaddress {
             188.102.2.98                         ###VIP
        }
        notify_master /data/keepalived/scripts/redis_master.sh
        notify_backup /data/keepalived/scripts/redis_backup.sh
        notify_fault  /data/keepalived/scripts/redis_fault.sh
        notify_stop   /data/keepalived/scripts/redis_stop.sh
}

#配置redis_master.sh
vim /data/keepalived/scripts/redis_master.sh

#!/bin/bash
 
REDISCLI="/data/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
REDISPORT=7001
REDIS_MASTER=188.102.2.96
REDIS_SLAVE=188.102.2.97
 
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
 
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -a redis -h $REDIS_SLAVE -p $REDISPORT SLAVEOF $REDIS_MASTER $REDISPORT >> $LOGFILE  2>&1
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
 
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI -a redis -h $REDIS_SLAVE -p $REDISPORT SLAVEOF NO ONE >> $LOGFILE 2>&1
#配置redis_backup.sh 
vim /data/keepalived/scripts/redis_backup.sh

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

(3)在master和slave创建如下配置:

#配置redis_check.sh
vim /data/keepalived/scripts/redis_check.sh
 
#!/bin/bash

REDISPORT=7001 
ALIVE=`/data/redis/bin/redis-cli -a redis -p $REDISPORT PING`
if [ "$ALIVE" == "PONG" ]; then
  echo $ALIVE
  exit 0
else
  echo $ALIVE
  exit 1
fi

3、添加执行权限

chmod +x /data/keepalived/scripts/*.sh

4、启动keepalived

Service keepalived start

 

三、安装部署redis

1、安装redis,下载地址:https://redis.io/download

# 解压并安装(主从一样安装)
tar zxvf redis-5.0.5.tar.gz
cd redis-5.0.5
./configure
make
make install

注意:系统时间需设置正确

2、配置redis.config

#配置redis.conf 
vim /data/redis/etc/7001.conf 

daemonize no
pidfile /var/run/redis.pid
port 7001
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/data/redis/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
requirepass redis
masterauth redis

3、启动redis

# 根据redis.conf启动redis服务
/data/redis-7001/bin/redis-server /data/redis-7001/etc/7001.conf


#写成start_redis.sh 方便执行
#!/bin/bash
/data/redis-7001/bin/redis-server /data/redis-7001/etc/7001.conf   2>&1 &

 

你可能感兴趣的:(Redis)