码迷,mamicode.com
首页 > 其他好文 > 详细

Redis + keepalived 主从设置与搭建

时间:2017-03-21 16:50:33      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:tar.gz   nbsp   order   应用软件   角色   idt   root   eof   top   

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高可用配置文件(可从线上拷贝再做修改)

[root@www 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 

 

[root@www 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脚本:

[root@www 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

 

[root@www 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与

[root@www scripts]# cat redis_fault.sh

#!/bin/bash

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

echo "[fault]" >> $LOGFILE

date >> $LOGFILE

[root@www 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

 

Redis + keepalived 主从设置与搭建

标签:tar.gz   nbsp   order   应用软件   角色   idt   root   eof   top   

原文地址:http://www.cnblogs.com/JimGreen/p/6595168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!