前言
主机:
172.17.209.25
172.17.209.26
软件版本:
keepalived-1.2.19.tar.gz
redis-2.8.1.tar.gz
一、安装Redis
(1)安装
tar -xvf redis-2.8.1.tar.gz
cd redis-2.8.1
make
mkdir -p /usr/local/redis/var
cd src
cp redis-server redis-sentinel redis-cli redis-check-dump redis-check-aof redis-benchmark /usr/local/redis/
(2)配置成系统服务
vi /etc/init.d/redis
#!/bin/bash
#
# init file for redis
#
# chkconfig: - 80 12
# description: redis daemon
#
# processname: redis
# config: /usr/local/redis/redis.conf
# pidfile: /usr/local/redis/var/redis.pid
source /etc/init.d/functions
REDISPORT=6379
EXEC=/usr/local/redis/redis-server
REDIS_CLI=/usr/local/redis/redis-cli
PIDFILE=/usr/local/redis/var/redis.pid
CONF="/usr/local/redis/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|reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|reload}" >&2
exit 1
esac
(3)配置redis
vi /usr/local/redis/redis.conf
daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
timeout 300
loglevel notice
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/
#8g memory 32*1024*1024*1024
maxmemory 34359738368
appendonly no
(4)配置自启动
chkconfig nginx on
(5)相关命令
启动:service redis start
关闭:service redis stop
(6)查看服务
ps -ef | grep redis
(7)拷贝redis到另一台器机9.26
scp -r /usr/local/redis/ root@172.17.209.26:/usr/local/
scp -r /etc/init.d/redis root@172.17.209.26:/etc/init.d/
二、安装keepalived
(1)安装
tar -zxvf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure --prefix=/usr/local/keepalived
make
make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir -p /etc/keepalived
mkdir -p /usr/local/keepalived/var/log
mkdir -p /usr/local/keepalived/scripts/redis
备注:在9.26上也要创建目录:/etc/keepalived,/usr/local/keepalived/var/log,/usr/local/keepalived/scripts/redis
(3)配置keepalived.conf
vi /etc/keepalived/keepalived.conf
vrrp_script chk_redis {
script "/usr/local/keepalived/scripts/redis/redis_check.sh" ###监控脚本
interval 2 ###监控时间
fall 3 ###失败尝试3次
rise 1 ###成功尝试1次
}
vrrp_instance VI_1 {
state MASTER ###设置为MASTER、BACKUP
interface eth0 ###监控网卡,网卡必须有接网线,可以对外网通信的
virtual_router_id 55 ###主从需要在同一路由编号
priority 101 ###权重值
authentication {
auth_type PASS ###加密
auth_pass redis ###密码
}
track_script {
chk_redis ###执行上面定义的chk_redis
}
virtual_ipaddress {
172.17.209.20 dev eth0 scope globa ###Keppalived虚拟出来的VIP
}
notify_master "/usr/local/keepalived/scripts/redis/redis_master.sh 172.17.209.26 6379"
notify_backup "/usr/local/keepalived/scripts/redis/redis_backup.sh 172.17.209.26 6379"
notify_fault "/usr/local/keepalived/scripts/redis/redis_fault.sh"
notify_stop "/usr/local/keepalived/scripts/redis/redis_stop.sh"
}
备注:拷贝到9.26后,要改一下notify_master和notify_backup参数中的172.17.209.26为172.17.209.25;
(4)配置自启动
chkconfig keepalived on
(5)相关命令
启动:service keepalived start
关闭:service keepalived stop
(6)查看服务
ps -ef | grep keepalived
(7)配置keepalived相关redis检查脚本
(1)redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/redis/redis-cli"
LOGFILE="/data/keepalived/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF $1 $2 >> $LOGFILE 2>&1
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
(2)redis_backup.sh
#!/bin/bash
REDISCLI="/usr/local/redis/redis-cli"
LOGFILE="/data/keepalived/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 $2 >> $LOGFILE 2>&1
(3)redis_fault.sh
#!/bin/bash
LOGFILE=/data/keepalived/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
(4)redis_stop.sh
#!/bin/bash
LOGFILE=/data/keepalived/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
(5)redis_check.sh
#!/bin/bash
ALIVE=`/usr/local/redis/redis-cli PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
将以上shell拷贝到目录:/usr/local/keepalived/scripts/redis/,
然后赋权:chmod 755 /usr/local/keepalived/scripts/redis/*.sh
(8)拷贝redis到另一台器机9.26
scp -r /usr/sbin/keepalived root@172.17.209.26:/usr/sbin/
scp -r /etc/sysconfig/keepalived root@172.17.209.26:/etc/sysconfig/
scp -r /etc/init.d/keepalived root@172.17.209.26:/etc/init.d/
scp -r /usr/local/keepalived/scripts/redis/* root@172.17.209.26:/usr/local/keepalived/scripts/redis
三、测试方案
查看日志命令:tailf -500 /data/keepalived/log/keepalived-redis-state.log
查看redis主从角色命令:/usr/local/redis/redis-cli -p 6379 info replication