主要核心思想,如果master 和 salve 全部存活的情况,VIP就漂移到 master。读写都从master操作,如果master宕机,VIP就会漂移到salve,并将之前的salve切换为master,当宕机的master可以继续服务的时候,首先会从salve同步数据,然后VIP漂移到master服务器上面,持续提供服务。

环境准备:

    master:ip 192.168.28.139;redis 19020;redis 19021;keepalived

    slave :ip 192.168.28.140;redis 19020;redis 19021;keepalived

    keepalived VIP:192.168.28.99

1、下载Redis redis-5.0.5.tar.gz

解压 tar xzf + Redis包(重复一下操作,改名可以配置2个redis)

mv redis-5.0.5 /usr/local/redis19020

进入 Redis文件安装

安装依赖文件

yum install gcc-c++

安装

make

mv redis.conf redis.conf.back

重写conf文件

vim redis.conf

=======================================

daemonize yes

pidfile /var/run/redis19020.pid

#pidfile /var/run/redis19021.pid

port 19020       #19021

tcp-backlog 511

timeout 30

tcp-keepalive 60

loglevel warning

logfile /logs/redis/redis19020.log

#logfile /logs/redis/redis19021.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 /usr/local/redis-5.0.5/ #redis安装路径

protected-mode no  #关闭保护模式

requirepass test123  #主Redis 密码

#masterauth test123    #从Redis 密码(与主一致)

#slaveof 192.168.28.139 19021 #从Redis设置 Redis主从配置(主Redis ip 端口)

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

==============================================

mkdir /logs/redis

touch /logs/redis/{redis19020.log,redis19021.log}

启动redis服务,并指定启动服务配置文件

src/redis-server redis.conf

设置环境变量

将 Redis 添加到环境变量中:

# vi /etc/profile

在最后添加以下内容:

## Redis env

export PATH=$PATH:/usr/local/redis19020/src

export PATH=$PATH:/usr/local/redis19021/src

使配置生效:

# source /etc/profile

现在就可以直接使用 redis-cli 等 redis 命令了。

2、keepalived安装

下载keepalived

官网: https://keepalived.org/download.html

上传并解压keepalived

/home

cd /home/

tar -zxvf keepalived-2.0.18.tar.gz -C /usr/local/src/

进入目录/usr/local/src/keepalived-2.0.18

cd /usr/local/keepalived-2.0.18/

检查安装环境

./configure --prefix=/usr/local/keepalived

首次检查

configure: error:

!!! OpenSSL is not properly installed on your system. !!!

!!! Can not inclu

de OpenSSL headers files.            !!!

安装openssl openssl-devel解决问题

yum -y install openssl openssl-devel

二次检查

*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

安装libnl libnl-devel解决问题

yum -y install libnl libnl-devel

其他问题

configure: error: libnfnetlink headers missing

安装libnfnetlink-devel解决问题

yum -y install libnfnetlink-devel

编译并安装

make && make install

将keepalived添加到系统服务中

拷贝执行文件

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

将init.d文件拷贝到etc下,加入开机启动项

cp /usr/local/src/keepalived-2.0.18/keepalived/etc/init.d/keepalived /etc/init.d/keepalived

将keepalived文件拷贝到etc下

cp /usr/local/src/keepalived-2.0.18/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

创建keepalived文件夹

mkdir -p /etc/keepalived

将keepalived配置文件拷贝到etc下

cp /usr/local/src/keepalived-2.0.18/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

添加keepalived到开机启动

chkconfig --add keepalived

添加可执行权限

chmod +x /etc/init.d/keepalived

二、部署keepalived双击自主切换(以下配置文件和脚本,两台机器都要重新布)

备份keepalived配置文件

cp keepalived.conf keepalived.conf.back

重新编辑配置文件

vim keepalived.conf

配置文件

================================================

! COnfiguration File for keepalived

global_defs {
router_id MASTER-HA  #主机标识
#router_id BACKUP   #备机标识

script_user root
enable_script_security

}

vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"   #脚本地址和名字,此处调用改脚本
interval 2
weight -5
fall 2
rise 1
}

vrrp_instance VI_1 {
state MASTER  #主机MASTER、备机BACKUP
interface eth0  #本机的网卡
virtual_router_id 51
priority 101           #主机101,备机小于101便可
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.28.99    #新的IP地址,需要在同机网段内
}

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/redis_stop.sh

}

================================================

新建文件夹scripts

mkdir scripts

新建脚本文件夹

touch {redis_check.sh,redis_master.sh,redis_backup.sh,redis_fault.sh,redis_stop.sh}

编写脚本

vim redis_check.sh(共用脚本)

#!/bin/bash
#This scripts is check for Redis Slave status
#19020
counter=$(netstat -na|grep "LISTEN"|grep "19020"|wc -l)
#查询本机Redis是否存活,如果不存在,则执行以下脚本
if [ "${counter}" -eq 0 ]; then
/etc/init.d/keepalived keepalived stop
killall keepalived
fi
ping 192.168.28.139 -w1 -c1 &>/dev/null #(本机IP)
#查询本机ip是否存活,不存活则执行以下脚本
if [ $? -ne 0 ]
then
/etc/init.d/keepalived keepalived stop
killall keepalived
fi
#19021
counterB=$(netstat -na|grep "LISTEN"|grep "19021"|wc -l)
#查询本机Redis是否存活,如果不存在,则执行以下脚本
if [ "${counterB}" -eq 0 ]; then
/etc/init.d/keepalived keepalived stop
killall keepalived
fi
ping 192.168.28.139 -w1 -c1 &>/dev/null #(本机IP)
#查询本机ip是否存活,不存活则执行以下脚本
if [ $? -ne 0 ]
then
/etc/init.d/keepalived keepalived stop
killall keepalived
fi

主reids脚本

vim redis_master.sh

#!/bin/bash
REDISCLI="/usr/local/redis19020/src/redis-cli -a test123 -p 19020"
LOGFILE="/var/log/keepalived-redis-state.log" #需要新建日志文件
REDISCLIA="/usr/local/redis19021/src/redis-cli -a test123 -p 19021"
LOGFILEA="/var/log/keepalived-redis-stateA.log" #需要新建日志文件
#19020
sleep 5
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >>$LOGFILE 2>&1
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 192.168.28.140 19020 >>$LOGFILE 2>&1 #主库IP
if [ $? -ne 0 ];then
echo "data rsync fail." >>$LOGFILE 2>&1
else
echo "data rsync OK." >> $LOGFILE 2>&1
fi
sleep 5 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
if [ $? -ne 0 ];then
echo "Run SLAVEOF NO ONE cmd fail." >>$LOGFILE 2>&1
else
echo "Run SLAVEOF NO ONE cmd OK." >> $LOGFILE 2>&1
fi
#19021
sleep 5
echo "[master2]" >> $LOGFILEA
date >> $LOGFILEA
echo "Being master...." >>$LOGFILEA 2>&1
echo "Run SLAVEOF cmd ...">> $LOGFILEA
$REDISCLIA SLAVEOF 192.168.28.140 19020 >>$LOGFILEA 2>&1 #主库IP
if [ $? -ne 0 ];then
echo "data rsync fail." >>$LOGFILEA 2>&1
else
echo "data rsync OK." >> $LOGFILEA 2>&1
fi
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILEA
$REDISCLIA SLAVEOF NO ONE >> $LOGFILEA 2>&1
if [ $? -ne 0 ];then
echo "Run SLAVEOF NO ONE cmd fail." >>$LOGFILEA 2>&1
else
echo "Run SLAVEOF NO ONE cmd OK." >> $LOGFILEA 2>&1

redis_backup.sh

#!/bin/bash
REDISCLI="/usr/local/redis19020/src/redis-cli -a test123-p 19020"
REDISCLIA="/usr/local/redis19021/src/redis-cli -a test123 -p 19021"
LOGFILE="/var/log/keepalived-redis-state.log"
LOGFILEA="/var/log/keepalived-redis-stateA.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >>$LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 192.168.28.140 19020 >>$LOGFILE 2>&1 #主库IP
#19021
echo "[backup2]" >> $LOGFILEA
date >> $LOGFILEA
echo "Being slave...." >>$LOGFILEA 2>&1
sleep 3 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ...">> $LOGFILEA
$REDISCLIA SLAVEOF 192.168.28.140 19021 >>$LOGFILEA 2>&1 #从库IP

从Redis脚本

vim redis_master.sh

#!/bin/bash
REDISCLI="/usr/local/redis19020/src/redis-cli -a test123 -p 19020"
REDISCLIA="/usr/local/redis19021/src/redis-cli -a test123 -p 19021"
LOGFILE="/var/log/keepalived-redis-state.log" #需要新建日志文件
LOGFILEA="/var/log/keepalived-redis-stateA.log" #需要新建日志文件
sleep 15
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >>$LOGFILE 2>&1 echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 192.168.28.139 19020 >>$LOGFILE 2>&1 #主库IP
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
#19021
sleep 15
echo "[masterA]" >> $LOGFILEA
date >> $LOGFILEA
echo "Being master...." >>$LOGFILEA 2>&1 echo "Run SLAVEOF cmd ...">> $LOGFILEA
$REDISCLIA SLAVEOF 192.168.28.139 19021 >>$LOGFILEA 2>&1 #主库IP
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILEA
$REDISCLIA SLAVEOF NO ONE >> $LOGFILEA 2>&1

redis_backup.sh

#!/bin/bash
REDISCLI="/usr/local/redis19020/src/redis-cli -a test123 -p 19020"
REDISCLIA="/usr/local/redis19021/src/redis-cli -a test123 -p 19021"
LOGFILE="/var/log/keepalived-redis-state.log"
LOGFILEA="/var/log/keepalived-redis-stateA.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >>$LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 192.168.28.139 19020 >>$LOGFILE 2>&1 #主库IP
#19021
echo "[backupA]" >> $LOGFILEA
date >> $LOGFILEA
echo "Being slave...." >>$LOGFILEA 2>&1
sleep 3 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ...">> $LOGFILEA
$REDISCLIA SLAVEOF 192.168.28.139 19021 >>$LOGFILEA 2>&1 #主库IP

共用脚本

redis_fault.sh

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

redis_stop.sh

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

添加可执行权限

chmod +x /etc/init.d/keepalived

显示脚本文件为

启动keepalived

/etc/init.d/keepalived start

查看是否启动成功

ps -ef | grep keepalived

启动成功之后会生成一个新的服务器IP地址,可通过新IP直接连接Redis,可以自由关闭其中一台Redis服务,keepalived会自动切换到另外一台Redis服务器当中。

宕机后启动顺序为,先启动Redis,在启动keepalived即可,启动后会自动同步两台Redis的数据。

linux 双Redis + keepalived 主从复制+宕机自主切换的更多相关文章

  1. linux MySQL 5.7+keepalived 主备服务器自主切换

    一.环境准备1.关闭防火墙与selinux systemctl stop firewalld setenforce 0 sed -i 's/SELINUX=.*/SELINUX=disabled/g' ...

  2. redis主从架构宕机问题手动解决

    1    主机宕机 1.  设置端口6379是主机,端口6380是从机,全部都正常启动 2.  验证在6379写入数据,在6380也能得到数据 3.  现在将6379主机停掉,模拟主机宕机 4.  由 ...

  3. linux安装redis及主从复制、读写分离、哨兵模式

    Redis安装与部署 版本最好选择3.0及以上.以后还可以部署Redis集群. 1.下载: [root@bogon redis-3.0.0]# cd /usr/local [root@bogon lo ...

  4. Keepalived 双机web服务宕机检测切换系统软件

    简介 Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Kee ...

  5. [文章]Linux宕机故障分析案例

    [文章]Linux宕机故障分析案例 已采纳 收藏  0  1669 0 马化辉 2018-07-01发布 背景 在Linux系统环境下,服务器宕机发生的频率比较小,但是不少工程师或多或少都会遇到这 ...

  6. Linux中Redis的安装

    一.下载redis redis官网地址:http://www.redis.io/ 下载地址:http://download.redis.io/releases/ redis中文文档地址:http:// ...

  7. linux上redis的安装与配置

    1.redis安装 wget http://download.redis.io/releases/redis-4.0.8.tar.gz tar xzf redis-4.0.8.tar.gz ln -s ...

  8. 《【面试突击】— Redis篇》-- Redis的主从复制?哨兵机制?

    能坚持别人不能坚持的,才能拥有别人未曾拥有的.关注左上角编程大道公众号,让我们一同坚持心中所想,一起成长!! <[面试突击]— Redis篇>-- Redis的主从复制?哨兵机制? 在这个 ...

  9. keepalived+nginx实现niginx高可用,宕机自动重启

    nginx作为http服务器,在集群中 用于接受客户单发送过来的请求,并且根据配置的策略将请求 转发给具体的哪台服务器 如果在nginx服务器使用轮询策略处理客户端的请求,出现了tomcat 宕机的情 ...

随机推荐

  1. spark sql thrift server

    ### create data ## cat ## echo "$(date ;echo ## cat }'";exit}' ..} do passwd) echo "$ ...

  2. oracle避免在索引列上使用IS NULL和IS NOT NULL

    避免在索引中使用任何可以为空的列,ORACLE将无法使用该索引 .对于单列索引,如果列包含空值,索引中将不存在此记录. 对于复合索引,如果每个列都为空,索引中同样不存在此记录. 如果至少有一个列不为空 ...

  3. Redis在Laravel项目中的应用实例详解

    https://mp.weixin.qq.com/s/axIgNPZLJDh9VFGVk7oYYA 在初步了解Redis在Laravel中的应用 那么我们试想这样的一个应用场景 一个文章或者帖子的浏览 ...

  4. html前端登录验证

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 【Activiti工作流引擎】官方快速入门demo

    Activiti官方快速入门demo 地址: https://www.activiti.org/quick-start 0. 版本 activiti 5.22.0 JDK 1.8 1. 介绍 这个快速 ...

  6. Java反射机制(五):使用反射增强简单工厂设计模式

    关于简单工厂设计模式的讲解,可参考博文<设计模式: 简单工厂模式>,此处不再介绍:  我们先观察之前介绍的关于简单工厂: public class OperateFactory { pub ...

  7. java TreeSet的排序之定制排序

    TreeSet的自然排序是根据元素的大小进行升序排序的,若想自己定制排序,比如降序排序,就可以使用Comparator接口了: 该接口包含int compare(Object o1,Object o2 ...

  8. Codeforces 1100F(离线 or 在线)

    传送门 •参考资料 [1]:在线线性基 [2]:离线线性基 [3]:离线线性基 •题意 给你 n 个数,m 次询问: 每次询问给定一个区间 $l,r$,求 $a_{l \cdots r}$ 异或的最大 ...

  9. 浅谈使用spring security中的BCryptPasswordEncoder方法对密码进行加密与密码匹配

    浅谈使用springsecurity中的BCryptPasswordEncoder方法对密码进行加密(encode)与密码匹配(matches) spring security中的BCryptPass ...

  10. PHP PDO扩展整理,包括环境配置\基本增删改查\事务\预处理

    相关文章:PHP的mysql扩展整理,操作数据库的实现过程分析    PHPmysqli扩展整理,包括面向过程和面向对象的比较\事务控制\批量执行\预处理 介绍 PDO是一种PHP程序连接数据库的接口 ...