环境介绍:
Master: 1.1.1.10
Slave: 1.1.1.11
Virtural IP Address (VIP):1.1.1.111
设计思路:
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从复制功能;
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
然后依次循环。
需要注意的是,这样做需要在Master与Slave上都开启本地化策略,否则在互相自动切换的过程中,未开启本地化的一方会将另一方的数据清空,造成数据完全丢失。
- 下面,是具体的实施步骤:
一、安装
1. 安装keepalived
yum install -y keepalived
2. 安装redis
tar xvf redis-2.8.tar.gz
cd redis-2.8
make PREFIX=/usr/local/redis install
3. 把redis加入开机启动
vim /etc/init.d/redis
#!/bin/sh
#Configurations injected by install_server below....
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/usr/local/redis/var/redis.pid
CONF=/usr/local/redis/etc/redis.conf
REDISPORT="6379"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_6379
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_6379
# Description: Redis daemon
### END INIT INFO
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
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
status)
PID=$(cat $PIDFILE)
if [ ! -x /proc/${PID} ]
then
echo 'Redis is not running'
else
echo "Redis is running ($PID)"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Please use start, stop, restart or status as first argument"
;;
esac
chkconfig redis on
二、配置
1. 配置redis
mkdir /usr/local/redis/etc/
mkdir /usr/local/redis/var/
vim /usr/local/redis/etc/redis.conf
daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
timeout 300
loglevel debug
logfile /usr/local/redis/var/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /usr/local/redis/var/
appendonly no
appendfsync always
- conf的主要配置参数的意义:
daemonize:是否以后台daemon方式运行
pidfile:pid文件位置
port:监听的端口号
timeout:请求超时时间
loglevel:log信息级别
logfile:log文件位置
databases:开启数据库的数量
save * :保存快照的频率,第一个表示多长时间,第三个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。
rdbcompression:是否使用压缩
dbfilename:数据快照文件名(只是文件名,不包括目录)
dir:数据快照的保存目录(这个是目录)
appendonly:是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。
appendfsync:appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步)
- 将Redis的命令所在目录添加到系统参数PATH中
>vim /etc/profile
export PATH="$PATH:/usr/local/redis/bin"
source /etc/profile
启动redis,/etc/init.d/redis start
用redis-cli命令进行测试
2. 配置keepalive
>vim /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL
}
vrrp_script check_redis_port {
script "/etc/keepalived/scripts/redis_check.sh" ###监控脚本
interval 2 ###监控时间
}
vrrp_instance VI_1 {
state BACKUP #主备都用BACKUP
interface eth0
virtual_router_id 51
priority 100 #slave端用 priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 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"
track_script {
check_redis_port
}
virtual_ipaddress {
1.1.1.111
}
}
3. 编写相关脚本
- 编写监控脚本
>mkdir /etc/keepalived/scripts
>cd !$
>vim redis_check.sh
#!/bin/bash
ALIVE=`/usr/local/redis/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
- 编写负责运作的关键脚本
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
具体脚本:
>vim redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
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 1.1.1.11 6379 >> $LOGFILE 2>&1 #master和backup都写对方ip
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
>vim 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
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 1.1.1.11 6379 >> $LOGFILE 2>&1 #master和backup都写对方ip
>vim /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
- 给脚本都加上可执行权限:
chmod +x /etc/keepalived/scripts/*.sh
三、验证
1. 启动
- 启动Master上的Redis
>/etc/init.d/redis start
- 启动Slave上的Redis
>/etc/init.d/redis start
- 启动Master上的Keepalived
>/etc/init.d/keepalived start
- 启动Slave上的Keepalived
>/etc/init.d/keepalived start
- 尝试通过VIP连接Redis:
redis-cli -h 1.1.1.111
set a 3
get a
3
分别的两边的redis进行启停,会看到较色转移,vip转移
- 现在redis的高可用做好了,接下来做redis共享tomcat,session
2. 配置tomcat
在tomcat/lib 下,加入如下jar包
jar百度云盘有(东张瞎望的鱼):
- 分别修改2个tomcat的context.xml
<!--
redis save session
-->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="1.1.1.111"
port="6379"
database="0"
maxInactiveInterval="60"/>
重启tomcat
- 设置nginx
upstream redis-session {
server 1.1.1.10:8081 weight=1; #mem01
# server 1.1.1.10:8082 weight=1; #mem02
server 1.1.1.11:8083 weight=1; #mem03
# server 1.1.1.11:8084 weight=1; #mem04
}
server {
listen 80;
server_name redis-session;
location / {
proxy_pass http://redis-session;
}
}
浏览器访问
1.1.1.10 1.1.1.11多次刷新,session id是不会变的,共享成功
分别对2台redis进行启停,也不会影响session共享
redis-cli -h 1.1.1.111
keys *
keys
卧槽,老子终于完成
Tips:后面redis会崩溃,MISCONF Redis is configured to save RDB snapshots
解决方案如下:
运行 config set stop-writes-on-bgsave-error no 命令
关闭配置项stop-writes-on-bgsave-error解决该问题。
网友评论