CentOS下Redis高可用安裝過程

來源:互聯網
上載者:User

CentOS下Redis高可用安裝過程
CentOS下Redis高可用安裝過程[x] 安裝環境介紹:

Master: T1
Slave: T2
VIP: 192.168.68.45

[x] 安裝Redis(Master,Slave)

注意: 安裝redis前flushall的修改

尋找src/redis.c檔案,把

 `{"flushdb",flushdbCommand,1,"w",0,NULL,0,0,0,0,0},`   `{"flushall",flushallCommand,1,"w",0,NULL,0,0,0,0,0},`   

修改成:

 `{"xflushdb",flushdbCommand,1,"w",0,NULL,0,0,0,0,0},`   `{"xflushall",flushallCommand,1,"w",0,NULL,0,0,0,0,0},`   
vi /etc/sysctl.conf添加一行: `vm.overcommit_memory=1`mkdir /opt/redismkdir /opt/redis/logmkdir /opt/redis/dbtar zxvf ./redis-2.8.17.tar.gzcd redis-2.8.17make PREFIX=/opt/redis install
[x] Redis啟動指令碼(Master,Slave):/opt/redis/bin/startRedis.sh
#!/bin/bashbasedir=`dirname $0`echo "Redis BASE DIR:$basedir"cd $basedirnohup ./redis-server ./redis.conf > /dev/null 2>&1 &
[x] Redis停止指令碼(Master,Slave):/opt/redis/bin/StopRedis.sh
#!/bin/shbasedir=`dirname $0`echo "Redis BASE DIR:$basedir"cd $basedir./redis-cli -h localhost -a 123456 shutdown
[x] Redis設定檔(Master,Slave):/opt/redis/bin/redis.conf
#requirepass 123456pidfile /opt/redis/bin/redis.pidlogfile /opt/redis/log/redis.logdir /opt/redis/db/daemonize yesport 6379timeout 300loglevel warningdatabases 16maxmemory 1g#不要快照#save 900 1#save 300 10#save 60 10000#rdbcompression yes#dbfilename dump.rdb#使用AOFappendonly yesappendfsync everysec
[x] 安裝Keepalived(Master,Slave):
#wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gztar zxvf keepalived-1.2.13.tar.gzcd ./keepalived-1.2.13./configuremake make installcp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/cp /usr/local/sbin/keepalived /usr/sbin/mkdir /etc/keepalivedcp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/chkconfig --add keepalived#chkconfig --level 35 keepalived on#service keepalived start
[x] notify_*解釋
Keepalived在轉換狀態時會依照狀態來呼叫:當進入Master狀態時會呼叫notify_master當進入Backup狀態時會呼叫notify_backup當發現異常情況時(track_script,track_interface失敗)進入Fault狀態呼叫notify_fault當Keepalived程式終止時則呼叫notify_stop
[x] 在Master上建立如下設定檔:

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalivedglobal_defs {   notification_email {     root@localhost   }   notification_email_from root@localhost   smtp_server localhost   smtp_connect_timeout 30   router_id LVS_REDIS}vrrp_script chk_redis {  script "/etc/keepalived/scripts/redis_check.sh"   ###監控指令碼  interval 2                                        ###監控時間}vrrp_instance VI_1 {    nopreempt ###不搶佔,防止腦裂    state MASTER             #備的是BACKUP    interface br0    virtual_router_id 51    priority 100            #備的是90    advert_int 1    authentication {        auth_type PASS        auth_pass redis    }    virtual_ipaddress {        192.168.68.45    }    track_script {      chk_redis                       ###執行上面定義的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}
[x] 在Slave上建立如下設定檔:

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalivedglobal_defs {   notification_email {     root@localhost   }   notification_email_from root@localhost   smtp_server localhost   smtp_connect_timeout 30   router_id LVS_REDIS}vrrp_script chk_redis {  script "/etc/keepalived/scripts/redis_check.sh"   ###監控指令碼  interval 2                                        ###監控時間}vrrp_instance VI_1 {    nopreempt ###不搶佔,防止腦裂    state BACKUP             #備的是BACKUP    interface br0    virtual_router_id 51    priority 90            #備的是90    advert_int 1    authentication {        auth_type PASS        auth_pass redis    }    virtual_ipaddress {        192.168.68.45    }    track_script {      chk_redis                       ###執行上面定義的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}
[x] 在Master和Slave上建立監控Redis的指令碼:

mkdir /etc/keepalived/scripts/
vi /etc/keepalived/scripts/redis_check.sh

#!/bin/bashC_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`ALIVE=`/opt/redis/bin/redis-cli PING`if [ "$ALIVE" == "PONG" ]; then  echo "${C_DATE} $ALIVE"  exit 0else  echo "${C_DATE} $ALIVE"  exit 1fi
[x] 在Master與Slave建立如下指令碼 notify_faultnotify_stop

vim /etc/keepalived/scripts/redis_fault.sh

#!/bin/bashC_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`LOGFILE=/var/log/keepalived-redis-state.logecho "${C_DATE} [fault]" >> $LOGFILE

vim /etc/keepalived/scripts/redis_stop.sh

#!/bin/bashC_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`LOGFILE=/var/log/keepalived-redis-state.logecho "${C_DATE} [stop]" >> $LOGFILE
[x] 在Master上建立 notity_masternotify_backup指令碼:

vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bashC_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`REDISCLI="/opt/redis/bin/redis-cli"LOGFILE="/var/log/keepalived-redis-state.log"echo "${C_DATE} [master]" >> $LOGFILE#當keepalived配置為"搶佔式"時,開啟下面注釋#echo "Being master...." >> $LOGFILE 2>&1#echo "Run SLAVEOF cmd ..." >> $LOGFILE#$REDISCLI SLAVEOF T2 6379 >> $LOGFILE  2>&1#sleep 10 #延遲10秒以後待資料同步完成後再取消同步狀態echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

vim /etc/keepalived/scripts/redis_backup.sh

#!/bin/bashC_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`REDISCLI="/opt/redis/bin/redis-cli"LOGFILE="/var/log/keepalived-redis-state.log"echo "${C_DATE} [backup]" >> $LOGFILE#當keepalived配置為"搶佔式"時,開啟下面注釋#echo "Being slave...." >> $LOGFILE 2>&1#sleep 15 #延遲15秒待資料被對方同步完成之後再切換主從角色echo "Run SLAVEOF cmd ..." >> $LOGFILE$REDISCLI SLAVEOF T2 6379 >> $LOGFILE  2>&1
[x] 在Slave上建立 notity_masternotify_backup指令碼:

vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bashC_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`REDISCLI="/opt/redis/bin/redis-cli"LOGFILE="/var/log/keepalived-redis-state.log"echo "${C_DATE} [master]" >> $LOGFILE#當keepalived配置為"搶佔式"時,開啟下面注釋#echo "Being master...." >> $LOGFILE 2>&1#echo "Run SLAVEOF cmd ..." >> $LOGFILE#$REDISCLI SLAVEOF T1 6379 >> $LOGFILE  2>&1#sleep 10 #延遲10秒以後待資料同步完成後再取消同步狀態echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

vim /etc/keepalived/scripts/redis_backup.sh

#!/bin/bashC_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`REDISCLI="/opt/redis/bin/redis-cli"LOGFILE="/var/log/keepalived-redis-state.log"echo "${C_DATE} [backup]" >> $LOGFILE#當keepalived配置為"搶佔式"時,開啟下面注釋#echo "Being slave...." >> $LOGFILE 2>&1#sleep 15 #延遲15秒待資料被對方同步完成之後再切換主從角色echo "Run SLAVEOF cmd ..." >> $LOGFILE$REDISCLI SLAVEOF T1 6379 >> $LOGFILE  2>&1
[x] 在Master和Slave上,給監控指令碼都加上可執行許可權:

chmod +x /etc/keepalived/scripts/*.sh

[x] 啟動步驟:
  1. 啟動Master上的Redis
    /opt/redis/bin/startRedis.sh

  2. 啟動Slave上的Redis
    /opt/redis/bin/startRedis.sh

  3. 啟動Master上的Keepalived
    service keepalived start

  4. 啟動Slave上的Keepalived
    service keepalived start

下面關於Redis的文章您也可能喜歡,不妨參考下:

Ubuntu 14.04下Redis安裝及簡單測試

Redis主從複製基本配置

Redis叢集明細文檔

Ubuntu 12.10下安裝Redis(圖文詳解)+ Jedis串連Redis

Redis系列-安裝部署維護篇

CentOS 6.3安裝Redis

Redis安裝部署學習筆記

Redis設定檔redis.conf 詳解

Redis 的詳細介紹:請點這裡
Redis 的:請點這裡

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.