博文说明【前言】:
本文将通过个人口吻介绍Redis+Keeplived实现高可用的相关知识,在目前时间点【2017年6月23号】下,所掌握的技术水平有限,可能会存在不少知识理解不够深入或全面,望大家指出问题共同交流,在后续工作及学习中如发现本文内容与实际情况有所偏差,将会完善该博文内容。
本文为我编写的部署文档(word格式)中关于redis部分的内容复制粘贴而来,因此会存在一些格式问题,因不影响阅读和理解,也没有改的必要了,这样反而便于大家慢下来理解。发表此处,一来是有方便自己查看,二来是给广大网友参考,谢谢大家阅读。
正文:
Redis配置信息
Redis部署使用两台服务器,实现Redis+keepalived,提供redis服务高可用,当主redis进程或服务器宕机之后,备redis进程或服务器继续提供服务。
服务器配置信息如下所示:
主机名/IP |
端口 |
用途 |
JZSHPT-APP5/ 10.133.214.15 |
6379 |
Redis主服务器-master |
JZSHPT-APP6/ 10.133.214.16 |
6379 |
Redis从服务器-salve |
一:Redis主服务器搭建-redis-master
1、安装redis
JZSHPT-APP5:/soft # tar -zxf redis-3.2.1.tar.gz -C /usr/local
JZSHPT-APP5:/soft # cd /usr/local/redis-3.2.1/
JZSHPT-APP5:/usr/local/redis-3.2.1 # make
JZSHPT-APP5:/usr/local/redis-3.2.1 # cd src &&make install
JZSHPT-APP5:/usr/local # mv /usr/local/redis-3.2.1/ /usr/local/redis
1)redis配置文件修改
JZSHPT-APP5:/usr/local# vim /usr/local/redis/redis.conf
修改文件相关内容为:
#bind 127.0.0.1
protected-mode no
port 6379
daemonizeyes
pidfile/usr/local/redis/redis.pid
logfile"/usr/local/redis/redis.log"
dbfilename dump6379.rdb
dir /usr/local/redis/
2)redis启动脚本编写
JZSHPT-APP5:/usr/local/redis# vim /etc/init.d/redisd
#!/bin/sh
#chkconfig345 86 14
#descriptionStartup and Shutdown script for Redis-3.2.1
progdir=/usr/local/redis/src
progname=redis-server
daemon=$progdir/$progname
config=/usr/local/redis/redis.conf
pidfile=/usr/local/redis/redis.pid
desc="redisdaemon"
scriptname=/etc/init.d/redisd
start()
{
if test -x $daemon;
then
echo -e "Starting$desc:$progname"
if $daemon $config
then
echo -e "Start OK!!!"
else
echo -e "Startfailed!!!"
fi
else
echo -e "Couldn't find RedisServer($daemon)"
fi
}
stop()
{
if test -e $pidfile;
then
echo -e "Stopping$desc:$progname"
if kill `cat $pidfile`
then
echo -e "stop OK!!!"
else
echo -e "Stop failed!!!"
fi
else
echo -e "No Redis Server($daemoon)running"
fi
}
restart()
{
echo -e "Restarting$desc:$progname"
stop
start
}
status()
{
ps aux | grep $progname
}
case$1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo"Usage:$scriptnme{start|stop|restart|status}" >&2
exit 1
;;
esac
exit0
JZSHPT-APP5:/usr/local/redis #chmod +x /etc/init.d/redisd
JZSHPT-APP5:/usr/local/redis #chkconfig --add redisd
2、安装Keepalived
安装keepalived前请自行安装解决依赖关系(可能会涉及的软件包为libopenssl-devel、zlib-devel、ipvsadm内核模块等)
JZSHPT-APP5:/soft # tar -zxf keepalived-1.2.23.tar.gz
JZSHPT-APP5:/soft # cd keepalived-1.2.23/
JZSHPT-APP5:/soft/keepalived-1.2.23 # ./configure--prefix=/usr/local/keepalived/
JZSHPT-APP5:/soft/keepalived-1.2.23 # make &&make install
1)keepalived配置文件编辑
JZSHPT-APP5:~# mkdir -p /etc/keepalived/{scripts,log}
JZSHPT-APP5:~ # vim /usr/local/keepalived/etc/keepalived/
! Configuration File for keepalived
global_defs {
router_idRedis1_DEVEL
}
vrrp_script chk_redis {
script"/etc/keepalived/scripts/redis_check.sh"
interval2
timeout2
fall 3
}
vrrp_instance VI_1 {
stateMASTER
interfaceeth0
virtual_router_id 51
nopreempt
priority200
advert_int5
authentication {
auth_typePASS
auth_pass redis
}
virtual_ipaddress {
10.133.214.50
}
track_script {
chk_redis
}
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
}
2)keepalived启动脚本配置
JZSHPT-APP5:~ # cp /soft/keepalived-1.2.23/keepalived/etc/init.d/keepalived.suse.init/etc/init.d/keepalived.suse
JZSHPT-APP5:~ # cp/usr/local/keepalived/sbin/keepalived /usr/local/sbin/
JZSHPT-APP5:~ # cp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/
3)定义监控脚本
JZSHPT-APP5:~ # vim/etc/keepalived/scripts/redis_check.sh
#!/bin/sh
###/etc/keepalived/scripts/redis_check.sh
ALIVE=`/usr/local/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
4)定义状态切换为master时执行的脚本
JZSHPT-APP5:~ # vim/etc/keepalived/scripts/redis_master.sh
#!/bin/sh
###/etc/keepalived/scripts/redis_master.sh
REDISCLI="redis-cli"
LOGFILE="/etc/keepalived/log/redis-state.log"
pid=$$
echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]">> $LOGFILE
echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]Run 'SLAVEOF 10.133.214.16 6379'" >> $LOGFILE
$REDISCLI SLAVEOF 10.133.214.16 6379 >>$LOGFILE 2>&1
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] wait 10 sec for data sync from oldmaster" >> $LOGFILE
sleep 10
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] data rsync from old mater ok...">> $LOGFILE
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] Run slaveof no one,close master/slave">> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] wait other slave connect....">> $LOGFILE
5)定义状态切换为backups时执行的脚本
JZSHPT-APP5:~ # vim/etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
###/etc/keepalived/scripts/redis_backup.sh
REDISCLI="redis-cli"
LOGFILE="/etc/keepalived/log/redis-state.log"
pid=$$
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master]" >> $LOGFILE
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] Being slave state..." >> $LOGFILE2>&1
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] wait 10 sec for data sync from oldmaster" >> $LOGFILE
sleep 10
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] data rsync from old mater ok...">> $LOGFILE
echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]Run 'SLAVEOF 10.133.214.16 6379'" >> $LOGFILE
$REDISCLI SLAVEOF 10.133.214.16 6379 >>$LOGFILE 2>&1
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] slave connect to 192.18.3.21ok..." >> $LOGFILE
6) 定义状态切换为stop时执行的脚本
JZSHPT-APP5:~ # cp/etc/keepalived/scripts/redis_backup.sh /etc/keepalived/scripts/redis_stop.sh
7) 定义状态切换为fault时执行的脚本
JZSHPT-APP5:~ # cp/etc/keepalived/scripts/redis_backup.sh /etc/keepalived/scripts/redis_fault.sh
3、安装rsyslog实现keepalived日志分离
1)安装rsyslog软件包
JZSHPT-APP5:/soft # rpm -ivhrsyslog-5.10.1-0.7.49.x86_64.rpm
2)修改rsyslog配置文件
JZSHPT-APP5:/soft # vim /etc/rsyslog.conf
140 #*.*;mail.none;news.none -/var/log/messages
141 *.*;mail.none;news.none;local0.none -/var/log/messages
153 #local0,local1.* -/var/log/localmessages
154 local0.* -/var/log/keepalived.log
155 local1.* -/var/log/localmessages
156 local2,local3.* -/var/log/localmessages
157 local4,local5.* -/var/log/localmessages
158 cal6,local7.* -/var/log/localmessages
3)修改rsyslog启动脚本配置文件
根据/etc/init.d/syslog中的三个启动选项,修改该文件内的”SYSLOG_DAEMON”选项
JZSHPT-APP5:/soft # vim /etc/sysconfig/syslog
40 #SYSLOG_DAEMON="syslog-ng"
41 SYSLOG_DAEMON="rsyslogd"
4)修改Keepalived启动脚本选项
JZSHPT-APP5:/soft # vim /etc/init.d/keepalived.suse
17 DAEMON_OPT="-d "
18 DAEMON_OPT="-d -D -S 0"
5)启动rsyslog服务
JZSHPT-APP5:/soft # /etc/init.d/syslog restart
4、启动Redis和Keepalived
JZSHPT-APP5:~ # /etc/init.d/redisd start
JZSHPT-APP5:~ # /etc/init.d/keepalived.suse start
二:Redis从服务器搭建-redis-slave
1、安装redis
JZSHPT-APP6:/soft # tar -zxf redis-3.2.1.tar.gz -C /usr/local
JZSHPT-APP6:/soft # cd /usr/local/redis-3.2.1/
JZSHPT-APP6:/usr/local/redis-3.2.1 # make
JZSHPT-APP6:/usr/local/redis-3.2.1 # cd src &&make install
JZSHPT-APP6:/usr/local # mv /usr/local/redis-3.2.1/ /usr/local/redis
1)redis配置文件修改
JZSHPT-APP6:/usr/local# vim /usr/local/redis/redis.conf
修改文件相关内容为:
#bind 127.0.0.1
protected-mode no
port 6379
daemonizeyes
pidfile/usr/local/redis/redis.pid
logfile"/usr/local/redis/redis.log"
dbfilename dump6379.rdb
dir /usr/local/redis/
2)redis启动脚本编写
JZSHPT-APP6:/usr/local/redis# vim /etc/init.d/redisd
#!/bin/sh
#chkconfig345 86 14
#descriptionStartup and Shutdown script for Redis-3.2.1
progdir=/usr/local/redis/src
progname=redis-server
daemon=$progdir/$progname
config=/usr/local/redis/redis.conf
pidfile=/usr/local/redis/redis.pid
desc="redisdaemon"
scriptname=/etc/init.d/redisd
start()
{
if test -x $daemon;
then
echo -e "Starting$desc:$progname"
if $daemon $config
then
echo -e "Start OK!!!"
else
echo -e "Startfailed!!!"
fi
else
echo -e "Couldn't find RedisServer($daemon)"
fi
}
stop()
{
if test -e $pidfile;
then
echo -e "Stopping$desc:$progname"
if kill `cat $pidfile`
then
echo -e "stop OK!!!"
else
echo -e "Stop failed!!!"
fi
else
echo -e "No Redis Server($daemoon)running"
fi
}
restart()
{
echo -e "Restarting$desc:$progname"
stop
start
}
status()
{
ps aux | grep $progname
}
case$1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo"Usage:$scriptnme{start|stop|restart|status}" >&2
exit 1
;;
esac
exit0
JZSHPT-APP6:/usr/local/redis #chmod +x /etc/init.d/redisd
JZSHPT-APP6:/usr/local/redis #chkconfig --add redisd
2、安装Keepalived
安装keepalived前请自行安装解决依赖关系(可能会涉及的软件包为libopenssl-devel、zlib-devel、ipvsadm内核模块等)
JZSHPT-APP6:/soft # tar -zxf keepalived-1.2.23.tar.gz
JZSHPT-APP6:/soft # cd keepalived-1.2.23/
JZSHPT-APP6:/soft/keepalived-1.2.23 # ./configure--prefix=/usr/local/keepalived/
JZSHPT-APP6:/soft/keepalived-1.2.23 # make &&make install
1)keepalived配置文件编辑
JZSHPT-APP6:~ # mkdir -p /etc/keepalived/{scripts,log}
JZSHPT-APP6:~ # vim /usr/local/keepalived/etc/keepalived/
! Configuration File for keepalived
global_defs {
router_idRedis2_DEVEL
}
vrrp_script chk_redis {
script"/etc/keepalived/scripts/redis_check.sh"
interval 2
timeout2
fall 3
}
vrrp_instance VI_1 {
state BACKUP
interfaceeth0
virtual_router_id 51
priority200
advert_int5
authentication {
auth_typePASS
auth_pass redis
}
virtual_ipaddress{
10.133.214.50
}
track_script {
chk_redis
}
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
}
2)keepalived启动脚本配置
JZSHPT-APP6:~ # cp /soft/keepalived-1.2.23/keepalived/etc/init.d/keepalived.suse.init/etc/init.d/keepalived.suse
JZSHPT-APP6:~ # cp /usr/local/keepalived/sbin/keepalived/usr/local/sbin/
JZSHPT-APP6:~ # cp/usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
3)定义监控脚本
JZSHPT-APP6:~ # vim/etc/keepalived/scripts/redis_check.sh
#!/bin/sh
###/etc/keepalived/scripts/redis_check.sh
ALIVE=`/usr/local/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
4)定义状态切换为master时执行的脚本
JZSHPT-APP6:~ # vim/etc/keepalived/scripts/redis_master.sh
#!/bin/sh
###/etc/keepalived/scripts/redis_master.sh
REDISCLI="redis-cli"
LOGFILE="/etc/keepalived/log/redis-state.log"
pid=$$
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]" >> $LOGFILE
echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]Run 'SLAVEOF 10.133.214.15 6379'" >> $LOGFILE
$REDISCLI SLAVEOF 10.133.214.15 6379 >>$LOGFILE 2>&1
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] wait 10 sec for data sync from oldmaster" >> $LOGFILE
sleep 10
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] data rsync from old mater ok...">> $LOGFILE
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] Run slaveof no one,closemaster/slave" >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] wait other slave connect....">> $LOGFILE
5)定义状态切换为backups时执行的脚本
JZSHPT-APP6:~ # vim/etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
###/etc/keepalived/scripts/redis_backup.sh
REDISCLI="redis-cli"
LOGFILE="/etc/keepalived/log/redis-state.log"
pid=$$
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master]" >> $LOGFILE
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] Being slave state..." >>$LOGFILE 2>&1
echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] wait 10 sec for data sync from oldmaster" >> $LOGFILE
sleep 10
echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master]data rsync from old mater ok..." >> $LOGFILE
echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]Run 'SLAVEOF 10.133.214.15 6379'" >> $LOGFILE
$REDISCLI SLAVEOF 10.133.214.15 6379 >>$LOGFILE 2>&1
echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]slave connect to 10.133.214.15 ok..." >> $LOGFILE
6) 定义状态切换为stop时执行的脚本
JZSHPT-APP6:~ # cp/etc/keepalived/scripts/redis_backup.sh /etc/keepalived/scripts/redis_stop.sh
7) 定义状态切换为fault时执行的脚本
JZSHPT-APP6:~ # cp /etc/keepalived/scripts/redis_backup.sh/etc/keepalived/scripts/redis_fault.sh
3、安装rsyslog实现keepalived日志分离
1)安装rsyslog软件包
JZSHPT-APP5:/soft # rpm -ivhrsyslog-5.10.1-0.7.49.x86_64.rpm
2)修改rsyslog配置文件
JZSHPT-APP5:/soft # vim /etc/rsyslog.conf
140 #*.*;mail.none;news.none -/var/log/messages
141 *.*;mail.none;news.none;local0.none -/var/log/messages
153 #local0,local1.* -/var/log/localmessages
154 local0.* -/var/log/keepalived.log
155 local1.* -/var/log/localmessages
156 local2,local3.* -/var/log/localmessages
157 local4,local5.* -/var/log/localmessages
159 cal6,local7.* -/var/log/localmessages
3)修改rsyslog启动脚本配置文件
根据/etc/init.d/syslog中的三个启动选项,修改该文件内的”SYSLOG_DAEMON”选项
JZSHPT-APP5:/soft # vim /etc/sysconfig/syslog
40 #SYSLOG_DAEMON="syslog-ng"
41 SYSLOG_DAEMON="rsyslogd"
4)修改Keepalived启动脚本选项
JZSHPT-APP5:/soft # vim /etc/init.d/keepalived.suse
17 DAEMON_OPT="-d "
18 DAEMON_OPT="-d -D -S 0"
5)启动rsyslog服务
JZSHPT-APP5:/soft # /etc/init.d/syslog restart
4、启动Redis和Keepalived
JZSHPT-APP6:~ # /etc/init.d/redisd start
JZSHPT-APP6:~ # /etc/init.d/keepalived.suse start
结尾:
感谢阅读,祝有收获的一天,谢谢!
本文转自1清风揽月1 51CTO博客,原文链接:http://blog.51cto.com/watchmen/1941410,如需转载请自行联系原作者