主:172.16.0.104html
備:172.16.0.105redis
VIP:172.16.0.107bash
客戶端直接連VIP,當master 104的redis掛掉後,105做爲master。當104重啓後,104做爲105的slave。當105又掛掉以後,104做爲master......ui
一、安裝redis並配置主從同步加密
安裝過程省略spa
1.1 配置104的redis實例:/etc/redis/redis.confrouter
requirepass 123456 #生產環境千萬不要配置這麼簡單的密碼htm
masterauth 123456 #備庫密碼,這裏沒有配置slaveof卻配置masterauth是由於主備切換時須要用到密碼blog
1.2 配置105的redis實例ip
requirepass 123456 #生產環境千萬不要配置這麼簡單的密碼
masterauth 123456 #主庫密碼
slaveof 172.16.0.104 6379
二、安裝keepalived
安裝過程省略
三、配置keepalived
Master(104)實例:
keepalived.conf
global_defs { router_id test } vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379 123456" interval 2 timeout 2 fall 3 } vrrp_instance redis { state BACKUP # 主也配置爲SLAVE interface eth0 virtual_router_id 51 priority 150 nopreempt # 不搶佔,注意加上 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 172.16.0.107 } track_script { chk_redis } notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 6379 123456" notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 6379 123456 172.16.0.105 6379" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh
}
redis_master.sh
#!/bin/bash REDISCLI="redis-cli -h $1 -p $2 -a $3" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
redis_backup.sh
#!/bin/bash REDISCLI="redis-cli -h $1 -p $2 -a $3" LOGFILE="/var/log/keepalived-redis-state.log" echo "[backup]" >> $LOGFILE date >> $LOGFILE echo "Being slave...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF $4 $5 >> $LOGFILE 2>&1
Slave(105)實例
global_defs { router_id redis } vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379 123456" ###監控腳本 interval 2 ###監控時間 timeout 2 ###超時時間 fall 3 ###重試次數 } vrrp_instance redis { state BACKUP ###設置爲MASTER interface eth0 ###監控網卡 virtual_router_id 51 priority 100 ###權重值 nopreempt # 不搶佔,注意加上 advert_int 1 authentication { auth_type PASS ###加密 auth_pass 123456 ###密碼 } track_script { chk_redis ###執行上面定義的chk_redis } virtual_ipaddress { 172.16.0.107 ###VIP } notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 6379 123456" notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 6379 123456 172.16.0.104 6379" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh }
#!/bin/bash REDISCLI="redis-cli -h $1 -p $2 -a $3" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
redis_backup.sh
#!/bin/bash REDISCLI="redis-cli -h $1 -p $2 -a $3" LOGFILE="/var/log/keepalived-redis-state.log" echo "[backup]" >> $LOGFILE date >> $LOGFILE echo "Being slave...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF $4 $5 >> $LOGFILE 2>&1
公共配置
redis_check.sh
#!/bin/bash ALIVE=`redis-cli -h $1 -p $2 -a $3 PING` LOGFILE="/var/log/keepalived-redis-check.log" echo "[CHECK]" >> $LOGFILE date >> $LOGFILE if [ $ALIVE == "PONG" ]; then : echo "Success: redis-cli -h $1 -p $2 PING $ALIVE" >> $LOGFILE 2>&1 exit 0 else echo "Failed:redis-cli -h $1 -p $2 PING $ALIVE " >> $LOGFILE 2>&1 exit 1 fi
redis_fault.sh
#!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[fault]" >> $LOGFILE date >> $LOGFILE
redis_stop.sh
#!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[stop]" >> $LOGFILE date >> $LOGFILE
啓動keepalived:keepalived -D
查看ip漂移狀況:ip a
配置要點:
雙BACKUP + nopreempt,優先級大的先啓動做爲master,當master掛掉後,slave被選中成爲新的master。
參考文章:http://www.cnblogs.com/lulu/archive/2013/04/14/3021261.html