centos6上redis+Keepalived实现Redis主从复制

前端之家收集整理的这篇文章主要介绍了centos6上redis+Keepalived实现Redis主从复制前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

环境:CentOs6.8

Master:172.20.52.72

Slave:172.20.52.159

Virtual IP address(VIP):172.20.52.253


应用软件:

1、keepalived-1.2.12.tar.gz

下载地址http://www.keepalived.org/download.html

2、redis-2.8.7.tar.gz

下载地址http://www.oschina.net/news/49449/redis-2-8-7


一、安装配置前准备

yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel


Redis属于内存数据库,所以调优都集中到了内存上,需要将 vm.overcommit 设置为1
echo "vm.overcommit_memory=1" >> /etc/sysctl
sysctl -p

1.在主服务器172.20.52.160上面做下面操作
echo " 172.20.52.72 node1" >> /etc/hosts
echo " 172.20.52.159 node2" >> /etc/hosts
2.在从服务器10.10.10.204上面做下面操作
echo " 172.20.52.72node1" >> /etc/hosts
echo " 172.20.52.159 node2" >> /etc/hosts

二、在Master和slave上安装Redis

tar -zxvfredis-2.8.7.tar.gz

cd redis-2.8.7

make && make install

cd src

mkdir -p /usr/local/redis/bin

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

mkdir -p /usr/local/redis/etc

复制源码中的redis.conf至/usr/local/redis/etc/ cp redis.conf /usr/local/redis/etc/

修改 /usr/local/redis/etc/redis.conf里面可以把 daemonize no 修改为daemonize yes

制作一个redis init.d 的启动脚本:

复制如下代码

#!/usr/bin/envbash
#
#redisstartuptheredisserverdaemon
#
#chkconfig:3459999
#description:redisservicein/etc/init.d/redis\
#chkconfig--addredisorchkconfig--listredis\
#serviceredisstartorserviceredisstop
#processname:redis-server
#config:/etc/redis.conf

PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis.pid
CONF="/etc/redis.conf"
#makesuresomedirexist
if[!-d/var/lib/redis];then
mkdir-p/var/lib/redis
mkdir-p/var/log/redis
fi

case"$1"in
status)
ps-A|grepredis
;;
start)
if[-f$PIDFILE]
then
echo"$PIDFILEexists,processisalreadyrunningorcrashed"
else
echo"StartingRedisserver..."
$EXEC$CONF
fi
if["$?"="0"]
then
echo"Redisisrunning..."
fi
;;
stop)
if[!-f$PIDFILE]
then
echo"$PIDFILEdoesnotexist,processisnotrunning"
else
PID=$(cat$PIDFILE)
echo"Stopping..."
$REDIS_CLI-p$REDISPORTSHUTDOWN
while[-x${PIDFILE}]
do
echo"WaitingforRedistoshutdown..."
sleep1
done
echo"Redisstopped"
fi
;;
restart|force-reload)
${0}stop
${0}start
;;
*)
echo"Usage:/etc/init.d/redis{start|stop|restart|force-reload}">&2
exit1
esac

vim /etc/init.d/redis-server
chmod o+x /etc/init.d/redis-server
chkconfig --add redis-server
/etc/init.d/redis-server start

从服务上redis的配置

修改 /etc/redis.conf

slaveof <masterip> <masterport>修改

slaveof 172.20.52.72 6379

然后开启从服务器的redis服务。

start redis-server start


三、进行主从测试

主服务器:redis-cli -p 6379 set hello world

从服务器:

redis-cli -p 6379 get hello

"world"

#主服务器

redis-cli -p 6379 set hello world2

#从服务器

redis-cli -p 6379 get hello

"world2"

redis-cli -p 6379 set hello world

(error) READONLY You can't write against a read only slave.

成功配置主从redis服务器,由于配置中有一条从服务器是只读的,所以从服务器没法设置数据,只可以读取数据。

四、安装配置keepalived

1.在Master和Slave上安装Keepalived

$ yum install keepalived -y

2.默认安装完成keepalived之后是有配置文件的,但是我们需要创建合适的配置文件

首先,在Master上创建如下配置文件

$ vim /etc/keepalived/keepalived.conf

vrrp_scriptchk_redis{
script"/etc/keepalived/scripts/redis_check.sh"###监控脚本
interval2###监控时间
}

vrrp_instanceVI_1{
stateMASTER###设置为MASTER
interfaceeth1###监控网卡
virtual_router_id51
priority100###权重值
authentication{
auth_typePASS###加密
auth_pass1111###密码
}

track_script{
chk_redis###执行上面定义的chk_redis
}

virtual_ipaddress{
172.20.52.253######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
}

3.然后,在Slave上创建如下配置文件

vim /etc/keepalived/keepalived.conf

vrrp_scriptchk_redis{
script"/etc/keepalived/scripts/redis_check.sh"###监控脚本
interval2###监控时间
}

vrrp_instanceVI_1{
stateBACKUP###设置为BACKUP
interfaceeth1###监控网卡
virtual_router_id51
priority10###比MASTRE权重值低
authentication{
auth_typePASS
auth_pass1111###密码与MASTRE相同
}

track_script{
chk_redis###执行上面定义的chk_redis
}

virtual_ipaddress{
192.168.1.253####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

}

4. 在Master和Slave上创建监控Redis的脚本

mkdir/etc/keepalived/scripts
#!/bin/bash
ALIVE=`/usr/local/bin/redis-cliPING`
if["$ALIVE"=="PONG"];then
echo$ALIVE
exit0
else
echo$ALIVE
exit1
fi

5.编写以下负责运作的关键脚本:

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

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

当进入Master状态时会呼叫notify_master

当进入Backup状态时会呼叫notify_backup

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

当Keepalived程序终止时则呼叫notify_stop

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

$ vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo"[master]">>$LOGFILE
date>>$LOGFILE
echo"Beingmaster....">>$LOGFILE2>&1

echo"RunSLAVEOFcmd...">>$LOGFILE
$REDISCLISLAVEOF172.20.52.1596379>>$LOGFILE2>&1
sleep10#延迟10秒以后待数据同步完成后再取消同步状态

echo"RunSLAVEOFNOONEcmd...">>$LOGFILE
$REDISCLISLAVEOFNOONE>>$LOGFILE2>&1

vim /etc/keepalived/scripts/redis_backup.sh

#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"

echo"[backup]">>$LOGFILE
date>>$LOGFILE
echo"Beingslave....">>$LOGFILE2>&1

sleep15#延迟15秒待数据被对方同步完成之后再切换主从角色
echo"RunSLAVEOFcmd...">>$LOGFILE
$REDISCLISLAVEOF172.20.52.1596379>>$LOGFILE2>&1

2)在Redis Slave上创建notity_master与notify_backup脚本:

vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"

echo"[master]">>$LOGFILE
date>>$LOGFILE
echo"Beingmaster....">>$LOGFILE2>&1

echo"RunSLAVEOFcmd...">>$LOGFILE
$REDISCLISLAVEOF172.20.52.726379>>$LOGFILE2>&1
sleep10#延迟10秒以后待数据同步完成后再取消同步状态

echo"RunSLAVEOFNOONEcmd...">>$LOGFILE
$REDISCLISLAVEOFNOONE>>$LOGFILE2>&1

vim /etc/keepalived/scripts/redis_backup.sh

#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"

echo"[backup]">>$LOGFILE
date>>$LOGFILE
echo"Beingslave....">>$LOGFILE2>&1

sleep15#延迟15秒待数据被对方同步完成之后再切换主从角色
echo"RunSLAVEOFcmd...">>$LOGFILE
$REDISCLISLAVEOF172.20.52.726379>>$LOGFILE2>&1

3)然后在Master与Slave创建如下相同的脚本:

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

6.在主从服务器上面给脚本都加上可执行权限:

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

7.

.进行相关功能测试

启动Master和slave上的Redis

$ /etc/init.d/redis start

启动Master和slave上的Keepalived

$ /etc/init.d/keepalived start


尝试通过VIP连接Redis:

$ redis-cli -h 172.20.52.253 INFO

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

role:master

slave0:172.20.52.159,6379,online

尝试插入一些数据:

$ redis-cli -h 172。20.52.253 SET Hello Redis

OK

从VIP读取数据

$ redis-cli -h 172.20.52.253 GET Hello

"Redis"

从Master读取数据

$ redis-cli -h 172.20.52.72 GET Hello

从Slave读取数据

$ redis-cli -h 172.20.52.159 GET Hello


8.通过模拟相关故障,进行功能测试

将Master上的Redis进程杀死:

$ killall -9 redis-server

查看Master上的Keepalived日志

$ tail -f /var/log/keepalived-redis-state.log

[fault]

Thu Sep 27 08:29:01 CST 2016

同时Slave上的日志显示

[master]

2016年 07月 07日 星期一 16:42:52 CST

Being master....

Run SLAVEOF cmd ...

OK Already connected to specified master

Run SLAVEOF NO ONE cmd ...

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

$ redis-cli -h 172.20.52.159 INFO

然后我们恢复Master的Redis进程

2016年 07月 07日 星期一 16:48:08 CST

[backup]

Fri Sep 28 14:16:37 CST 2016

Being slave....

发现目前的Master已经再次恢复了Master的角色,故障切换以及自动恢复都成功了。




1.问题:刚使用redis-cli -h 172.20.52.253 INFO

说连接失败,没有路由。

此时我的解决办法是iptables -F

2.VIP的添加

ifconfig eth1:0 172.20.52.253 netmask 172.20.52.253 up

原文链接:https://www.f2er.com/centos/378227.html

猜你在找的CentOS相关文章