keepalived官方有中文文檔:LVS + Keepalived Chinese application doc - March 16, 2010。java
keepalived 實現VRRP協議,從路由級別實現VIP切換,能夠徹底避免相似heartbeat 腦裂問題。能夠很nice的實現主從、主備、互備方案,尤爲是無狀態業務,有狀態業務就須要額外花些功夫了。mysql
既然mysql 可使用keepalived很好的作到主從切換,redis天然也就有樣學樣就成了。redis
redis主從實現徹底沒有mysql成熟,僅僅是可用而已,按照做者規劃相應改進須要等3.0之後了。通過測試主從也不是那麼徹底不靠譜。主要問題在於同步鏈接斷開以後須要從新全量同步,若是頻繁進行會對主服務帶來很大性能影響。 但現實中主從機器每每要求放在一個機櫃同一臺交換設備下,網絡閃斷狀況極低;再者主從同步在同步數量量大狀況下,須要將緩存區調得足夠大,否則也容易形成鏈接斷開。sql
實現切換邏輯以下:A B兩臺機器ubuntu
1. A 、B 依次啓動,A做爲主、B爲從緩存
2.主A 掛掉,B接管業務,做爲主網絡
3.A 起來,做爲從SLAVEOF Bapp
4.B 掛掉,A 切回主性能
將一臺所有做爲主,便可實現主從,可作讀寫分離;也能夠一臺機器上多個實例一半主、一半從,實現互備份,兩機同時接管業務,一臺宕機後業務都集中在一臺上。測試
在keepalived 有兩個角色:Master(一個)、Backup(多個),若是設置一個爲Master,但Master掛了後再起來,必然再次業務又一次切換,這對於有狀態服務是不可接受的。解決方案就是兩臺機器都設置爲Backup,並且優先級高的Backup設置爲nopreemt 不搶佔。
安裝比較簡單:
須要依賴包:openssl-devel(ubuntu 中爲 libssl-dev),popt-devel (ubuntu中爲libpopt-dev),redheat中都已經有了。
配置文件默認路徑:/etc/keepalived/keepalived.conf 也能夠手動指定路徑,不過要注意的是手動指定須要使用絕對路徑。
使用keepalived -D 運行,便可啓動3個守護進程:一個父進程,一個check健康檢查,一個Vrrp,-D將日誌寫入/var/log/message
配置以下:
主:
! Configuration File for keepalived global_defs { router_id test } vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 7001" interval 2 timeout 2 fall 3 } vrrp_script chk_redis2 { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 7002" interval 2 timeout 2 fall 3 } vrrp_instance redis { state BACKUP # 主也配置爲SLAVE interface wlan0} virtual_router_id 51 priority 150 nopreempt # 不搶佔,注意加上 advert_int 1 authentication { auth_type PASS auth_pass lulu } virtual_ipaddress { 192.168.1.144 } track_script { chk_redis } notify_master "/etc/keepalived/scripts/redis_master_slave.sh 127.0.0.1 192.168.1.126 7001" notify_backup "/etc/keepalived/scripts/redis_backup_slave.sh 127.0.0.1 192.168.1.126 7001" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh } vrrp_instance redis2 { state BACKUP interface wlan0 } virtual_router_id 50 priority 150 advert_int 1 nopreempt authentication { auth_type PASS auth_pass lulu } virtual_ipaddress { 192.168.1.145 } track_script { chk_redis2 } notify_master "/etc/keepalived/scripts/redis_master_slave.sh 127.0.0.1 192.168.1.126 7002" notify_backup "/etc/keepalived/scripts/redis_backup_slave.sh 127.0.0.1 192.168.1.126 7002" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh }
備:
! Configuration File for keepalived global_defs { router_id test } vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 7001" interval 2 timeout 2 fall 3 } vrrp_script chk_redis2 { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 7002" interval 2 timeout 2 fall 3 } vrrp_instance redis { state BACKUP interface wlan0 } virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass test } virtual_ipaddress { 192.168.1.144 } track_script { chk_redis } notify_master "/etc/keepalived/scripts/redis_master_slave.sh 127.0.0.1 192.168.1.126 7001" notify_backup "/etc/keepalived/scripts/redis_backup_slave.sh 127.0.0.1 192.168.1.126 7001" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh } vrrp_instance redis2 { state BACKUP interface wlan0 } virtual_router_id 50 advert_int 1 priority 100 authentication { auth_type PASS auth_pass test } virtual_ipaddress { 192.168.1.145 } track_script { chk_redis2 } notify_master "/etc/keepalived/scripts/redis_master_slave.sh 127.0.0.1 192.168.1.126 7002" notify_backup "/etc/keepalived/scripts/redis_backup_slave.sh 127.0.0.1 192.168.1.126 7002" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh }
其餘腳本見附件:http://files.cnblogs.com/lulu/scripts.rar