SuSE11环境下Redis+Keepalived实现高可用技术
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SuSE11环境下Redis+Keepalived实现高可用技术相关的知识,希望对你有一定的参考价值。
1.Redis配置信息
Redis部署使用两台服务器,实现Redis+keepalived,提供redis服务高可用,当主redis进程或服务器宕机之后,备redis进程或服务器继续提供服务。
实现目的:主MASTER宕机后,从BACKUP接管VIP提供服务,升为MASTER,主MASTER恢复后状态变为从BACKUP,不抢占回VIP,待新MASTER宕机后继续接替VIP,重新升为MASTER
MASTER-->BACKUP-->MASTER 依次轮询接管服务
服务器配置信息如下所示:
主机名/IP
端口
用途
JZSHPT-APP5/
10.133.214.15
6379
Redis主服务器
JZSHPT-APP6/
10.133.214.16
6379
Redis从服务器
2.Redis主服务器搭建
2.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
修改文件相关内容为:
protected-mode no
port 6379
daemonize yes
pidfile /usr/local/redis/redis.pid
logfile "/usr/local/redis/redis.log"
2)redis启动脚本编写(非必须)
JZSHPT-APP5:/usr/local/redis # vim /etc/init.d/redisd
#!/bin/sh
#chkconfig 345 86 14
#description Startup 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="redis daemon"
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 "Start failed!!!"
fi
else
echo -e "Couldn‘t find Redis Server($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
exit 0
JZSHPT-APP5:/usr/local/redis # chmod +x /etc/init.d/redisd
JZSHPT-APP5:/usr/local/redis # chkconfig --add redisd
2.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_id Redis1_DEVEL
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 2
timeout 2
fall 3
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
nopreempt
priority 200
advert_int 5
authentication {
auth_type PASS
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/ /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 old master" >> $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..." >> $LOGFILE 2>&1
echo "date +‘%Y-%m-%d:%H:%M:%S‘
|$pid|state:[master] wait 10 sec for data sync from old master" >> $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.21 ok..." >> $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
2.2.安装rsyslog实现keepalived日志分离
1)安装rsyslog软件包
JZSHPT-APP5:/soft # rpm -ivh rsyslog-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 start
2.3.启动Redis和Keepalived
JZSHPT-APP5:~ # /etc/init.d/redisd start
JZSHPT-APP5:~ # /etc/init.d/keepalived.suse start
3.Redis从服务器搭建
3.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
修改文件相关内容为:
protected-mode no
port 6379
daemonize yes
pidfile /usr/local/redis/redis.pid
logfile "/usr/local/redis/redis.log"
2)redis启动脚本编写(非必须)
JZSHPT-APP6:/usr/local/redis # vim /etc/init.d/redisd
#!/bin/sh
#chkconfig 345 86 14
#description Startup 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="redis daemon"
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 "Start failed!!!"
fi
else
echo -e "Couldn‘t find Redis Server($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
exit 0
JZSHPT-APP6:/usr/local/redis # chmod +x /etc/init.d/redisd
JZSHPT-APP6:/usr/local/redis # chkconfig --add redisd
3.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_id Redis2_DEVEL
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 2
timeout 2
fall 3
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 150
advert_int 5
authentication {
auth_type PASS
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/ /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 old master" >> $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-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 old master" >> $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.3.安装rsyslog实现keepalived日志分离
1)安装rsyslog软件包
JZSHPT-APP5:/soft # rpm -ivh rsyslog-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 start
3.4.启动Redis和Keepalived
JZSHPT-APP6:~ # /etc/init.d/redisd start
JZSHPT-APP6:~ # /etc/init.d/keepalived.suse start
测试结果可通过依次停Redis主从单节点来观测vip漂移
以上是关于SuSE11环境下Redis+Keepalived实现高可用技术的主要内容,如果未能解决你的问题,请参考以下文章
SLES Suse Linux 11 SP4环境下NFS的搭建
您好,我是suse11企业版的用户,现在项目正在搭建集群环境。我想咨询下如何使用syslog-ng搭建中央日志服务
SuSE 12 linux 开发环境搭建 之 redis安装与自启设置