keepalived + redis高可用方案

系统运维 waitig 501℃ 百度已收录 0评论

主机:
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                            ###权重值,9.25设置为101,9.26必须设置值必须小于100,否则选举不出master
        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

备注:要给shell赋权限
chmod 755 /usr/local/keepalived/scripts/redis/*.sh

三、测试方案
查看日志命令:tail -f /data/keepalived/log/keepalived-redis-state.log
查看redis主从角色命令:/usr/local/redis/redis-cli -p 6379 info replication
查看vrrp的信息:tail /var/log/message
查看VIP的漂移信息:ifconfig

启动9.25和9.26的redis,顺序不分先后。启动命令:service redis start
先启动9.25的keepalived,再启动9.26的keeplived,有先后顺序。启动命令:service keepalived start

注意的一点,如果9.25的keepalived和redis都是正常的,那么redis的角色肯定为master(主)。

测试结果如下:

序号操作前环境状态操作步骤期望结果实际结果测试状态
1(1)9.25_keepalived正常(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
停9.26redis(从)不发生主从切换,9.26_keepalived会触发notify_fault,打印[fault]日志;
完成后状态:
(1)9.25_keepalived正常(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[停]
不发生主从切换,9.26_keepalived不会触发notify_fault,不打印[fault];
完成后状态:
(1)9.25_keepalived正常
(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[停]
通过
2(1)9.25_keepalived正常(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[停]
启动9.26redis(从)不发生主从切换;
9.26_keepalived会触发notify_backup,将9.26_redis设置为9.25的从服务器,可以正常同步数据,打印[notify_backup]日志;
完成后状态:
(1)9.25_keepalived正常(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
不发生主从切换;
9.26_keepalived不会触发notify_backup,不打印[backup];
redis之前没有主从关系,没法同步数据。
完成后状态:
(1)9.25_keepalived正常(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[主]
通过
3(1)9.25_keepalived正常(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
停9.25redis(主)发生主从切换;
9.25_keepalived会触发notify_fault,打印[fault]日志;
9.26_keepalived会触发notify_master,打印[master]日志;
完成后状态:
(1)9.25_keepalived正常
(2)9.26_keepalived正常
(3)9.25_redis正常[停]
(4)9.26_redis正常[主]
发生主从切换;
9.25_keepalived会触发notify_fault,打印[fault]日志;
9.26_keepalived会触发notify_master,打印[master]日志;
完成后状态:
(1)9.25_keepalived正常
(2)9.26_keepalived正常
(3)9.25_redis正常[停]
(4)9.26_redis正常[主]
通过
4(1)9.25_keepalived正常(2)9.26_keepalived正常
(3)9.25_redis正常[停]
(4)9.26_redis正常[主]
启动9.25redis(主)发生主从切换;
9.25_keepalived会触发notify_master,打印[master]日志;
9.26_keepalived会触发notify_backup,打印[backup]日志;
完成后状态:
(1)9.25_keepalived正常
(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
发生主从切换;
9.25_keepalived会触发notify_master,打印[master]日志;
9.26_keepalived会触发notify_backup,打印[backup]日志;
完成后状态:
(1)9.25_keepalived正常
(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
通过
5(1)9.25_keepalived正常(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
停9.26keepalived不发生主从切换;
9.26_keepalived会触发notify_stop,打印[stop]日志
完成后状态:
1)9.25_keepalived正常
(2)9.26_keepalived停
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
不发生主从切换;
9.26_keepalived会触发notify_stop,打印[stop]日志;
完成后状态:
(1)9.25_keepalived正常
(2)9.26_keepalived停
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
通过
6(1)9.25_keepalived正常(2)9.26_keepalived停
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
启动9.26keepalived不发生主从切换;
9.26_keepalived会触发notify_backup,打印[backup]日志;
完成后状态:
(1)9.25_keepalived正常
(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
不发生主从切换;
9.26_keepalived会触发notify_backup,打印[backup]日志;
完成后状态:
(1)9.25_keepalived正常
(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
通过
7(1)9.25_keepalived正常(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
停9.25keepalived发生主从切换;
9.25_keepalived会触发notify_stop,打印[stop]日志;
9.26_keepalived会触发notify_master,打印[master]日志;
完成后状态:
(1)9.25_keepalived停
(2)9.26_keepalived正常
(3)9.25_redis正常[从]
(4)9.26_redis正常[主]
发生主从切换;
9.25_keepalived会触发notify_stop,打印[stop]日志;
9.26_keepalived会触发notify_master,打印[master]日志;
2个redis节点都是主节点,无法同步数据。
完成后状态:
(1)9.25_keepalived停
(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[主]
需要人工处理
8(1)9.25_keepalived停(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
启9.25keepalived发生主从切换;
9.25_keepalived会触发notify_master,打印[master]日志;
9.26_keepalived会触发notify_backup,打印[backup]日志;
完成后状态:
(1)9.25_keepalived正常
(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
发生主从切换;
9.25_keepalived会触发notify_master,打印[master]日志;
9.26_keepalived会触发notify_backup,打印[backup]日志;
完成后状态:
(1)9.25_keepalived正常
(2)9.26_keepalived正常
(3)9.25_redis正常[主]
(4)9.26_redis正常[从]
通过


本文由【waitig】发表在等英博客
本文固定链接:keepalived + redis高可用方案
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)