两台redis + keepalived主备

1 redis安装、  keepalived安装

2 keepalived配置

master:

/etc/keepalived/keepalived.conf

############

! Configuration File for keepalived

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 110                           ###权重值

authentication {

auth_type PASS             ###加密

auth_pass 7IB8LAW3RB       ###密码

}

track_script {

chk_redis                       ###执行上面定义的chk_redis

}

virtual_ipaddress {

192.168.2.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

}

#########

/etc/keepalived/scripts/下创建脚本(注意脚本要有可执行权限):

redis_master.sh

###############

#!/bin/bash

REDISCLI="/usr/local/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 -p 6379 -a admin SLAVEOF 192.168.2.82 6379 >> $LOGFILE  2>&1

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE

$REDISCLI -p 6379 -a admin SLAVEOF NO ONE >> $LOGFILE 2>&1

########

redis_backup.sh

########

#!/bin/bash

REDISCLI="/usr/local/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 -p 6379 -a admin SLAVEOF 192.168.2.82 6379 >> $LOGFILE  2>&1

########

192.168.2.82从机IP

redis_fault.sh

######

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "[fault]" >> $LOGFILE

date >> $LOGFILE

#####

redis_stop.sh

####

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "[fault]" >> $LOGFILE

date >> $LOGFILE

####

backup:

/etc/keepalived/keepalived.conf

##########

! Configuration File for keepalived

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 7IB8LAW3RB           ###密码与MASTRE相同

}

track_script {

chk_redis                       ###执行上面定义的chk_redis

}

virtual_ipaddress {

192.168.2.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

}

/etc/keepalived/scripts/下创建脚本:

redis_master.sh

####

#!/bin/bash

REDISCLI="/usr/local/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 -h 192.168.2.82 -p 6379 -a admin SLAVEOF NO ONE

$REDISCLI -p 6379 -a admin SLAVEOF 192.168.2.81 6379

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE

$REDISCLI -p 6379 -a admin SLAVEOF NO ONE >> $LOGFILE 2>&1

####

192.168.2.81为masterIP

redis_backup.sh

#####

#!/bin/bash

REDISCLI="/usr/local/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 -h 192.168.2.81 -p 6379 -a admin >> $LOGFILE  2>&1

$REDISCLI  -h 192.168.2.82 -p 6379 -a admin slaveof 192.168.2.81 6379

####

redis_fault.sh

####

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "[fault]" >> $LOGFILE

date >> $LOGFILE

####

redis_stop.sh

####

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "[stop]" >> $LOGFILE

date >> $LOGFILE

####

时间: 2024-10-18 08:39:16

两台redis + keepalived主备的相关文章

MM(主主数据库)+keepalived主备高可用集群

博客分享的第一篇技术文章: 项目主要搭建:主主数据库高可用集群搭建. 数据库互为主备,应用技术:MM+keepalived 使用的是虚拟机搭建的实验向大家展示: 数据库1:192.168.4.7 数据库2:192.168.4.77 VIP:192.168.4.68 web1:192.168.4.69 web2:192.168.4.70 一.安装mysql,部署主主同步结构. 直接yum安装 配置主主同步: 由于主数据库192.168.4.7里面存放着数据,所以需要先导出数据,方法很多,我们采取m

Nginx+Keepalived主备切换(包含nginx服务停止)

原文地址:http://blog.sina.com.cn/s/blog_79ac6aa80101bmed.html Nginx+Keepalived主备切换(包含nginx服务停止) 环境: VM中4台CentOS 本机PC充当测试机win8 步骤: 1.搭建 nginx+keepalived主备切换(不考虑主nginx停掉现象)文章中的环境 2.修改两台部署的keepalived的配置文件 3.编写观察nginx进程情况的脚本内容 #!/bin/bash A=`ps -C nginx --no

haproxy+keepalived主备与双主模式配置

Haproxy+Keepalived主备模式 主备节点设置 主备节点上各安装配置haproxy,配置内容且要相同 global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon defaults #defaults段默认值对frontend和backend和listen段生效 mode http #运行模式

Nginx+Keepalived主备负载均衡学习笔记

实验环境及软件版本: CentOS版本:    6.6(2.6.32.-504.el6.x86_64) nginx版本:     nginx-1.6.2 keepalived版本:keepalived-1.2.7 准备工作: 关闭防火墙:service iptables stop chkconfig iptables off 关闭selinux: sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' /etc/selinux/config init 6

keepalived 主备服务负载均衡、基于LAMP平台

一.keepalived的基本原理介绍 keepalived最初设计的目的是为了实现lvs前端director的高可用,非常轻量级.主要的实现的vrrp协议. VRRP是一种容错协议,它保证当主机的下一跳路由器出现故障时,由另一台路由器来代替出现故障的路由器进行工作,从而保持网络通信的连续性和可靠性. VRRP具有如下优点: 简化网络管理:在具有多播或广播能力的局域网(如以太网)中,借助VRRP 能在某台设备出现故障时仍然提供高可靠的缺省链路,有效避免单一链路发生故障后网络中断的问题,而无需修改

Nginx+Keepalived 主备高可用 安装与配置

环境说明:操作系统:CentOS6.7 x86_64Nginx版本:nginx-1.9.7Keepalived版本:keepalived-1.2.24 主nginx + Keepalived :10.219.24.26备nginx + Keepalived :10.219.24.23虚拟IP:10.219.24.100后端tomcat_1:10.219.24.21:8080后端tomcat_2:10.219.24.21:8081 架构与原理: 前端双 Nginx + Keepalived ,Ng

nginx+keepalived 主备搭建

nginx1 ip:192.168.12.4 #MASTERnginx2 ip:192.168.12.10 #BACKUPnginx_vip :192.168.12.100 原理可参考:http://www.keepalived.org/documentation.html系统为CentOS7 1.配置一下yum源 curl -L http://mirrors.aliyun.com/repo/Centos-7.repo > /etc/yum.repos.d/CentOS-Base.repocur

keepAlived主备及双主

nginx用默认配置即可 1.主备配置 1.主keepAlived配置 vrrp_instance VI_1 { state MASTER #主备区分 interface eth0 virtual_router_id 51 #主备一致 priority 100 #主备区分 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.244.200 } track_script

测试keepalived主备模式

目的:观察VIP在主备服务器上的切换过程 一.安装 yum -y install ipvsadm keepalived 二.服务配置 [keepalived  主] [[email protected] ~]# cat /etc/keepalived/keepalived.conf  ! Configuration File for keepalived global_defs {    router_id R201-1 } vrrp_instance VIP_Mysql {     state