一 简介
安装使用centos 5.10
Master 192.168.235.135
Slave 192.168.235.152
Vip 192.168.235.200
编译环境 yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从复制功能;
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
然后依次循环。
需要注意的是,这样做需要在Master与Slave上都开启本地化策略,否则在互相自动切换的过程中,未开启本地化的一方会将另一方的数据清空,造成数据完全丢失
二 安装
Redis安装配置->: http://liuyieyer.iteye.com/blog/2078093
下载安装wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz
tar xf keepalived-1.2.12.tar.gz
cd keepalived-1.2.12
./configure
make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
chmod +x /etc/init.d/keepalived
chkconfig –add keepalived
mkdir /etc/keepalived
ln –s /usr/local/sbin/keepalived /usr/sbin
三 配置
首先在master上创建如下配置
mkdir –p /etc/keepalived
vim /etc/keepalived/keepalived.conf
//加入如下内容脚本
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh" ###监控脚本
interval 2 ###监控时间
}
vrrp_instance VI_1 {
state MASTER ###设置为MASTER
interface eth0 ###监控网卡
virtual_router_id 51
priority 101 ###权重值
authentication {
auth_type PASS ###加密
auth_pass redis ###密码
}
track_script {
chk_redis ###执行上面定义的chk_redis
}
virtual_ipaddress {
192.168.235.200 ###VIP
}
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
}
vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/opt/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.135.152(slave) 6379 >> $LOGFILE 2>&1
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
vim /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/opt/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.135.152(slave) 6379 >> $LOGFILE 2>&1
在slave上创建如下文件:
vim /etc/keepalived/keepalived.conf
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh" ###监控脚本
interval 2 ###监控时间
}
vrrp_instance VI_1 {
state BACKUP ###设置为BACKUP
interface eth0 ###监控网卡
virtual_router_id 51
priority 100 ###比MASTRE权重值低
authentication {
auth_type PASS
auth_pass redis ###密码与MASTRE相同
}
track_script {
chk_redis ###执行上面定义的chk_redis
}
virtual_ipaddress {
192.168.235.200 ###VIP
}
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
}
vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/opt/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.235.135(master) 6379 >> $LOGFILE 2>&1
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
vim /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/opt/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 10.6.1.143 6379 >> $LOGFILE 2>&1
以下脚本分别在master和slave上创建
mkdir /etc/keepalived/scripts
vim /etc/keepalived/scripts/redis_check.sh
#!/bin/bash
ALIVE=`/opt/redis/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
vim /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
vim /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
授执行权 chmod +x /etc/keepalived/scripts/*.sh
测试
在msater和slave上分别启动redis;
Service redis start
分别启动keepalived
Service keepalived start
在master上执行 ip a
虚拟IP已经绑定了
在master上执行service redis stop 查看slave
看到了吧秒级切换。
接下来再吧master上的redis启动起来
又切换回来了。
测试redis同步
到此安装完成了。
相关推荐
标题 "keepalived + redis 实现双机热备" 涉及到的是在IT行业中构建高可用性系统的关键技术。keepalived 和 redis 是两个关键组件,它们一起工作以确保服务的持续运行,即使在主服务器故障时也能无缝切换到备份...
在这个部署方案中,Redis被配置为双主模式(也称为双活或双击),搭配Sensu监控工具和Keepalived来实现自动故障切换,确保即使在一台服务器宕机的情况下,服务仍然能够不间断地运行。 **Redis双主模式** Redis双主...
keepalived+nginx+tomcat+redis+mysql所需的包,里面包含了三个tomcat与Redis做session共享的jar,编译安装Mysql所需要Boost库和Cmake的包。
MySQL小型高可用架构方案:MySQL双主、主从+Keepalived主从自动切换服务器资源:两台PCServer优点:架构简单,节省资源缺点:无法线性扩展,主从失败之后需要手动恢复主从架构方案:MMM+MySQL双主+多从高可用方案...
Redis 是一个高性能的键值存储系统,而 Keepalived 则是一个网络守护进程,主要用于在主服务器故障时无缝地将流量切换到备份服务器。在本篇文章中,我们将深入探讨 Redis 和 Keepalived 配置的相关知识点,以及如何...
KVM,Zabbix企业级实战,自动化运维体系实战,Gitlab+Ansible+Jenkins运维实战,Nginx,Docker,Kubernetes,Openstack,ELK等云计算运维技术,企业级数据库Redis,MySQL主从复制备份等等,让同学们体验一站式Linux...
13、 部署方面使用LVS + keepalived + Nginx实现了双机主备、双主热备,实现了一个高可用的系统架构。 14、 项目开发采用团队式的开发,统一使用Maven私服构建项目,使用统一的SVN服务进行代码的管理。 【课程内容...
2. 服务器集群:使用负载均衡技术(如LVS)和心跳检测(如Keepalived),实现服务器之间的主从热备,提高可用性。 3. 数据库优化:采用读写分离,缓解数据库压力;利用缓存(如Redis)来处理频繁的读操作,提升响应...
- 配置Keepalived以监控Nginx的状态,并在故障发生时自动切换至备用节点。 - 使用`check_http`或`check_tcp`进行健康检查。 - 配置文件通常位于`/etc/keepalived/keepalived.conf`。 #### 三、后端服务器 - **...
可以考虑采用Nginx或Haproxy结合Keepalived的方式,增加热备机制,提高系统的可用性和可靠性。 - **扩展性考量**:随着业务规模的扩大,需要更加重视系统的可扩展性。可以考虑使用容器化技术(如Docker)和容器编排...