Redis + keepalived 主从设置与搭建

Redis+keepalived安装及配置:

1.ip规划:


服务器


Ip地址


VIP


应用软件


Redis主


192.168.1.102


192.168.1.120


Redis+

keepalived


Redis备


192.168.1.103


Redis+

keepalived

2.服务器基础配置安装

先安装好基础装机环境,具体参考  服务器基础环境配置 文档

redis安装:

1.#下载redies安装包

wget http://download.redis.io/releases/redis-3.2.0.tar.gz  tar -xf redis-3.2.0.tar.gz

2.#编译安装

cd redis-3.2.0

make

3.#添加相关文件及命令

mkdir /usr/local/redis/bin/ -p

cd src

cp redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server redis-sentinel /usr/local/redis/bin/

cd ..

cp redis.conf /etc/

4.#添加启动脚本

echo ‘#chkconfig: 2345 10 90

#description: Start and Stop redis

REDISPORT=6379

EXEC=/usr/local/redis/bin/redis-server

REDIS_CLI=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis.pid

CONF="/etc/redis.conf"

case "$1" in

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

‘ > /etc/init.d/redis

添加执行权限:

chmod +x /etc/init.d/redis

5.#设置开机自启动

chkconfig --add redis

chkconfig redis on

6.#创建redis状态日志

mkdir /var/log/redis/

touch /var/log/redis/redis.log

Keepalived安装:

1.获取安装包

wget http://www.keepalived.org/software/keepalived-1.2.20.tar.gz

2.解压安装

tar -xf keepalived-1.2.20.tar.gz

cd keepalived-1.2.20

yum -y install openssl openssl-devel

./configure && make && make install

3.文件配置

mkdir /etc/keepalived

mkdir /usr/local/keepalived/scripts/ -p

cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/

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

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

4.添加开机自启动

chmod +x /etc/init.d/keepalived

chkconfig --add keepalived

chkconfig keepalived on

以上操作主从都要执行

 

 

 

redis +keepalived配置:

1.配置redis master高可用配置文件:

vim  /etc/keepalived/keepalived

! Configuration File for keepalived

global_defs {

router_id redis103

}

vrrp_script chk_redis

{

script "/usr/local/keepalived/scripts/redis_check.sh 127.0.0.1 6379" #监控脚本

interval 2                                   #监控时间

timeout 2                                   #超时时间

fall 3

}

vrrp_instance redis {

state BACKUP # master set to SLAVE also

interface eth0

lvs_sync_daemon_interface eth0

virtual_router_id 202

priority 150                                      #权重值

nopreempt # no seize,must add

advert_int 1

authentication { #all node must same

auth_type PASS                                 #加密

auth_pass 1111                                 #密码

}

virtual_ipaddress {

192.168.1.120                                     #VIP

}

track_script {

chk_redis

}

notify_master "/usr/local/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.1.103 6379"

notify_backup "/usr/local/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.1.103 6379"

notify_fault /usr/local/keepalived/scripts/redis_fault.sh

notify_stop /usr/local/keepalived/scripts/redis_stop.sh

}

2.配置slave高可用配置文件(可从线上拷贝再做修改)

[[email protected] scripts]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

router_id redis104

}

vrrp_script chk_redis

{

script "/usr/local/keepalived/scripts/redis_check.sh 127.0.0.1 6379"                                            #监控脚本

interval 2                                     #监控时间

timeout 2                                      #超时时间

fall 3                                    #

}

vrrp_instance redis {

state BACKUP                                      #设置为BACKUP

interface eth0                                #监控网卡

lvs_sync_daemon_interface eth0                      #

virtual_router_id 202

priority  100                                    #权重值

nopreempt # no seize,must add

advert_int 1

authentication {   #all node must same

auth_type PASS                               #加密

auth_pass 1111                            #密码与102的相同

}

virtual_ipaddress {

192.168.1.120                                     #  VIP

}

track_script {

chk_redis

}

notify_master "/usr/local/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.1.102 6379"

notify_backup "/usr/local/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.1.102 6379"

notify_fault /usr/local/keepalived/scripts/redis_fault.sh

notify_stop /usr/local/keepalived/scripts/redis_stop.sh

}

 

3.masterslave上创建监控脚本:

1).配置master脚本文件(可从线上拷贝再做修改)

脚本目录 /usr/local/keepalived/scripts

以下用于监控Redis的脚本

/usr/local/keepalived/scripts/redis_check.sh

[[email protected] scripts]# cat redis_check.sh

#!/bin/bash

ALIVE=`/usr/local/redis/bin/redis-cli -h $1 -p $2 PING`

LOGFILE="/var/log/keepalived-redis-check.log"

echo "[CHECK]" >> $LOGFILE

date >> $LOGFILE

if [ $ALIVE == "PONG" ]; then :

echo "Success: redis-cli -h $1 -p $2 PING $ALIVE" >> $LOGFILE 2>&1

exit 0

else

echo "Failed:redis-cli -h $1 -p $2 PING $ALIVE " >> $LOGFILE 2>&1

exit 1

fi

以下负责运作的关键脚本:

notify_master /usr/local/keepalived/scripts/redis_master.sh

notify_backup /usr/local/keepalived/scripts/redis_backup.sh

notify_fault /usr/local/keepalived/scripts/redis_fault.sh

notify_stop /usr/local/keepalived/scripts/redis_stop.sh

因为Keepalived在转换状态时会依照状态来呼叫:

当进入Master状态时会呼叫notify_master

当进入Backup状态时会呼叫notify_backup

当发现异常情况时进入Fault状态呼叫notify_fault

当Keepalived程序终止时则呼叫notify_stop

首先,在Redis Master上创建notity_master与notify_backup脚本:

[[email protected] scripts]# cat redis_master.sh

#!/bin/bash

REDISCLI="/usr/local/redis/bin/redis-cli -h $1 -p $3"

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 $2 $3 >> $LOGFILE  2>&1

#echo "SLAVEOF $2 cmd can‘t
excute ... " >> $LOGFILE

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

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

$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

[[email protected] scripts]# cat 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

echo "Run
SLAVEOF cmd ..." >> $LOGFILE 2>&1

$REDISCLI SLAVEOF $2 $3 >> $LOGFILE

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

exit(0)

接着,在Redis Slave上创建notity_master与notify_backup脚本(因为是双主脚本和slave一致):

然后在Master与

[[email protected] scripts]# cat redis_fault.sh

#!/bin/bash

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

echo "[fault]" >> $LOGFILE

date >> $LOGFILE

[[email protected] scripts]# cat redis_stop.sh

#!/bin/bash

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

echo "[stop]" >> $LOGFILE

date >> $LOGFILE

给脚本都加上可执行权限:

$ sudo chmod +x /etc/keepalived/scripts/*.sh

2).Redis配置文件说明/etc/redis.conf:

参考文档 redis配置文件参数详解

3).设置环境变量:

echo
“export PATH=$PATH:/usr/local/redis/bin” >> /root/.bash_profile

source
/root/.bash_profile

4).重启服务:

/etc/init.d/keepalived
restart

/etc/init.d/redis
restart

redis测试:

脚本创建完成以后,我们开始按照如下流程进行测试:

1.启动Master上的Redis

$ /etc/init.d/redis start

2.启动Slave上的Redis

$ /etc/init.d/redis start

3.启动Master上的Keepalived

$ /etc/init.d/keepalived start

4.启动Slave上的Keepalived

$ /etc/init.d/keepalived start

5.尝试通过VIP连接Redis:

$ redis-cli -h 192.168.1.120 INFO

role:master

$ redis-cli -h 192.168.1.102 INFO

role:master

$ redis-cli -h 192.168.1.103 INFO

role:slave

连接成功,Slave也连接上来了。

6.尝试插入一些数据:

$ redis-cli -h 192.168.1.120 SET Hello Redis

OK

从VIP读取数据

$ redis-cli -h 192.168.1.120 GET Hello

"Redis"

从Master读取数据

$ redis-cli -h 192.168.1.102 GET Hello

"Redis"

从Slave读取数据

$ redis-cli -h 192.168.1.103 GET Hello

"Redis"

下面,模拟故障产生:

将Master上的Redis进程杀死:

$ killall -9 redis-server

查看Master上的Keepalived日志

$ tailf /var/log/keepalived-redis-state.log

同时Slave上的日志显示:

$ tailf /var/log/keepalived-redis-state.log

然后我们可以发现,Slave已经接管服务,并且担任Master的角色了。

$ redis-cli -h 192.168.1.120 INFO

$ redis-cli -h 192.168.1.104 INFO

role:master

然后我们恢复Master的Redis进程

$ /etc/init.d/redis start

查看Master上的Keepalived日志

$ tailf /var/log/keepalived-redis-state.log

同时Slave上的日志显示:

$ tailf /var/log/keepalived-redis-state.log

时间: 2024-12-27 20:29:22

Redis + keepalived 主从设置与搭建的相关文章

redis+keepalived 主从热备 实例

qiehuan 环境: 操作系统:centos 5.6 x64 redis版本:redis-2.8.10.tar.gz keepalived版本:keepalived-1.2.12.tar.gz redis  master : 192.168.101.50 redis     salve : 192.168.101.51 原理:如果返回时间超过keepalived vrrp_scripts的超时时间,keepalived会进入faild 状态,从而触发keepalived集群开始进行vip漂移

redis+Keepalived主从热备秒级切换

一 简介 安装使用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接管服务,同时关闭

redis(三)redis+Keepalived主从热备秒级切换

一 简介 安装使用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接管服务,同时关闭主

Redis + keepalived 高可用群集搭建

本次实验环境介绍: 操作系统: Centos 7.3 IP : 192.168.10.10 Centos 7.3 IP : 192.168.10.20  VIP    地址   : 192.168.10.254 软件版本介绍: redis : redis-3.2.8 下载链接: http://download.redis.io/releases/redis-3.2.8.tar.gz keepalived : keepalived-1.2.10  下载链接: http://www.keepaliv

redis+keepalived高可用搭建

一.Redis高可用部署及监控 目录 [相关资料脚本配置文件在此文件夹内]   Redis达到的目的:  1.主拥有读写权限,从拥有读和复制的权限. sentinel达到的目的:1.监控主从redis, 2.当其中一个redis宕机遇到故障后自动切换主从关系. 一.Redis Sentinel简介 二.硬件需求 三.拓扑结构 1.单M-S结构 2.双M-S结构 3.优劣对比 四.配置部署 1.Redis配置 2.Redis Sentinel配置 3.启动服务 4.故障模拟检测 五.备份恢复 1.

主从集群搭建及容灾部署redis

redis主从集群搭建及容灾部署(哨兵sentinel) Redis也用了一段时间了,记录一下相关集群搭建及配置详解,方便后续使用查阅. 提纲 l  Redis安装 l  整体架构 l  Redis主从结构搭建 l  Redis容灾部署(哨兵sentinel) l  Redis常见问题 Redis安装 发行版:CentOS-6.6 64bit 内核:2.6.32-504.el6.x86_64 CPU:intel-i7 3.6G 内存:2G 下载redis,选择合适的版本 [[email prot

企业实战-KeepAlived+Redis实现主从热备、秒级切换

最近公司生产环境需要做一个Redis+Keepalived的集群架构,分别用六个端口,实现多路复用,最终实现主从热备.秒级切换. 一.部署Redis集群 首先用两台虚拟机模拟6个节点,一台机器3个节点,创建出3 master.3 salve 环 境.然后模拟成功,方可去线上生产服务器部署. redis 采用 redis-3.2.4 版本. 两台虚拟机都是 CentOS ,一台 CentOS6.5 (IP:192.168.31.245),一台 CentOS7(IP:192.168.31.210)

redis 业务数据的主从设置

redis与mysql的主从很相似,而且比mysql设置主从更加简单: 分析: 1.redis主从复制特点: (1).master可以拥有多个slave (2).多个slave可以连接同一个master外,还可以连接到其他slave (3).主从复制不会阻塞master,在同步数据时,master可以继续处理client请求 (4).提高系统的伸缩性 2.redis主从复制过程: 当配置好slave后,slave与master建立连接,然后发送sync命令.无论是第一次连接还是重新连接,mast

Redis的主从搭建

Redis在作为可内存持久化的Key-Value数据库是使用的过程中是可以做主从服务的.在Redis的大规模集群中Redis的主从服务就是集群实现的基础,在Redis的主从服务中有以下几个优点: 1.master可以有多个slave. 2.除了多个slave连到相同的master外,slave也可以连接其它slave形成图状结构. 3.主从复制不会阻塞master.也就是说当一个或多个slave与master进行初次同步数据时,master 可以继续处理客户端发来的请求.相反slave在初次同步