Master ip: 192.168.240.78
Backup ip: 192.168.240.79
Virtual ip: 192.168.240.89
Slave ip: 192.168.240.80
Slave2 ip: 192.168.240.81
實現的功能:
1.當Master與Slave均運作正常時,Master負責寫,Backup負責熱備,Slave負責讀
2.當Master掛掉,Backup正常時,Backup接管服務,同時關閉主從複製功能
3.Slave關閉寫入磁碟,減少IO負載
一、安裝keepalived(backup也安裝)
| 代碼如下 |
複製代碼 |
################################# 1.安裝相容包 ########################### yum -y install openssl openssl-devel ipvsadm ################################ 2.下載並安裝keepalived #################### wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz tar zxvf keepalived-1.2.7.tar.gz cd keepalived-12.7 && make && make install ###############################3.redis_master keepalived配置################## cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived cp /usr/local/sbin/keepalived /usr/sbin/ cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ chkconfig --add keepalived vi /etc/keepalived/keepalived.conf #redis master配置 Configuration File for keepalived global_defs { notification_email { zhengxiaofeiccc@126.com } notification_email_from zhengxiaofeiccc@126.com smtp_server smtp.126.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 52 priority 100 #redis_backup 改為90 advert_int 1 nopreempt #不搶佔ip,只在master上設定 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.240.89 } } ######################4.啟動keepalived,並查看########################################### service keepalived start [root@redis_master ~]# ip a|grep inet inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host inet 192.168.240.78/24 brd 192.168.240.255 scope global eth0 inet 192.168.240.89/32 scope global eth0 inet6 fe80::250:56ff:fe98:6a56/64 scope link
|
二、安裝redis(每台都安裝)
| 代碼如下 |
複製代碼 |
#####################1.下載並安裝redis################################################## wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz tar zxvf redis-2.6.14.tar.gz cd redis-2.6.14 make && make install mkdir -p /etc/redis/ #####################2.編寫redis啟動指令碼################################################ vi /etc/init.d/redis
|
啟動指令碼
| 代碼如下 |
複製代碼 |
#!/bin/sh # # redis - this script starts and stops the redis-server daemon # # chkconfig: - 85 15 # description: Redis is a persistent key-value database # processname: redis-server # config: /etc/redis/6501.conf # config: /etc/sysconfig/redis # pidfile: /var/run/redis.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 redis="/usr/local/bin/redis-server" prog=$(basename $redis) REDIS_CONF_FILE="/etc/redis/6501.conf" [ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis lockfile=/var/lock/subsys/redis start() { [ -x $redis ] || exit 5 [ -f $REDIS_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $redis $REDIS_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { echo -n $"Reloading $prog: " killproc $redis -HUP RETVAL=$? echo } force_reload() { restart } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 |
配置Redis
| 代碼如下 |
複製代碼 |
vi /etc/redis/6501.conf (redis_master ) daemonize yes #啟用守護進程 port 6501 #監聽的連接埠號碼 timeout 0 #當用戶端閑置多長時間後關閉串連,如果指定為0,表示關閉該功能 tcp-keepalive 60 #表示將周期性使用SO_KEEPALIVE檢測用戶端是否還處於健康狀態,單位秒 loglevel notice #指定日誌記錄層級,Redis總共支援四個層級:debug、verbose、notice、warning logfile /var/log/redis_6501.log #指定日誌路徑 databases 8 #設定資料庫的數量 save 900 1 #表示900秒(15分鐘)內有1個更改,儲存資料到磁碟 save 300 10 #300秒(5分鐘)內有10個更改 save 60 10000 #60秒內有10000個更改 # slaveof 192.168.240.78 6501 這行在redis_backup 上面添加 stop-writes-on-bgsave-error yes #當持久化出現錯誤之後,是否繼續提供寫服務 rdbcompression yes #在進行鏡像備份時,是否進行壓縮 rdbchecksum yes #讀取和寫入的時候是否支援CRC64校檢 dbfilename dump.rdb #指定本機資料庫檔案名稱,預設值為dump.rdb dir /var/lib/redis/6501 #指定本機資料庫存放目錄 slave-serve-stale-data yes #當slave伺服器和master伺服器失去串連後,或者當資料正在複製傳輸的時候,如果此參數值設定“yes”,slave伺服器可以繼續接受用戶端的請求,否則,會返回給請求的用戶端如下資訊“SYNC with master in progress” slave-read-only yes #是否允許slave伺服器節點只提供讀服務 repl-disable-tcp-nodelay no #指定向slave同步資料時,是否禁用socket的NO_DELAY選 項。若配置為“yes”,則禁用NO_DELAY,則TCP協議棧會合并小包統一發送,這樣可以減少主從節點間的包數量並節省頻寬,但會增加資料同步到 slave的時間。若配置為“no”,表明啟用NO_DELAY,則TCP協議棧不會延遲小包的發送時機,這樣資料同步的延時會減少,但需要更大的頻寬。 通常情況下,應該配置為no以降低同步延時,但在主從節點間網路負載已經很高的情況下,可以配置為yes。 slave-priority 100 #指定slave優先順序 appendonly yes #開啟append only 模式之後,redis 會把所接收到的每一次寫操作請求都追加到appendonly.aof 檔案中,當redis 重新啟動時,會從該檔案恢複出之前的狀態。但是這樣會造成appendonly.aof 檔案過大,所以redis 還支援了BGREWRITEAOF 指令,對appendonly.aof 進行重新整理。預設是不開啟的。 appendfilename appendonly.aof #預設為appendonly.aof appendfsync everysec #設定aof的同步頻率,有三種選擇always、everysec、no,預設是everysec表示每秒同步一次 no-appendfsync-on-rewrite no #指定是否在後台aof檔案rewrite期間調用fsync,預設為no auto-aof-rewrite-percentage 100 #指定Redis重寫aof檔案的條件,預設為100,表示與上次rewrite的aof檔案大小相比,當前aof檔案增長量超過上次afo檔案大小的100%時,就會觸發background rewrite。若配置為0,則會禁用自動rewrite auto-aof-rewrite-min-size 64mb #指定觸發rewrite的aof檔案大小。若aof檔案小於該值,即使當前檔案的增量比例達到auto-aof-rewrite-percentage的配置值,也不會觸發自動rewrite。即這兩個配置項同時滿足時,才會觸發rewrite lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes #aof rewrite過程中,是否採取增量檔案同步策略,預設為“yes”。 rewrite過程中,每32M資料進行一次檔案同步,這樣可以減少aof大檔案寫入對磁碟的操作次數 ########################4.配置redis_slave (兩台配置一樣)################################# vi /etc/redis/6501.conf daemonize yes pidfile /var/run/redis_6501.pid port 6501 timeout 0 tcp-keepalive 60 loglevel notice logfile /var/log/redis_6501.log databases 8 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /var/lib/redis/6501 slaveof 192.168.240.89 6501 slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 maxclients 20000 appendonly no #slave不寫入到磁碟 appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes #####################5.編寫redis監控指令碼 redis_master############################## vi check_Redis.sh #!/bin/sh while : do redis-cli -p 6501 info &> /dev/null if [ $? -ne 0 ] ;then sleep 2 redis-cli -p 6501 info &> /dev/null if [ $? -ne 0 ] ;then service keepalived stop fi fi sleep 10 done nohup check_Redis.sh & #後台運行 #####################6.編寫redis_backup 監控指令碼 ##################################### # vi check_Redis.sh #!/bin/sh while : do redis-cli -p 6501 info &> /dev/null if [ $? -ne 0 ] ;then sleep 2 redis-cli -p 6501 info &> /dev/null if [ $? -ne 0 ] ;then service keepalived stop fi else ip a|grep 192.168.240.89 if [ $? -eq 0 ] ;then grep “^slaveof” /etc/redis/6501.conf If [ $? -eq 0 ]; then sed -i 's/^slaveof/#&/' /etc/redis/6501.conf service redis stop sleep 1 service redis start fi fi fi sleep 10 done nohup check_Redis.sh & #後台運行 |