keepalived + redis高可用方案

前言

主机:
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
image.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,937评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,503评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,712评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,668评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,677评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,601评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,975评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,637评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,881评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,621评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,710评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,387评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,971评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,947评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,189评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,805评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,449评论 2 342

推荐阅读更多精彩内容