1、簡介
無論是Keepalived仍是Heartbeat作高可用,其高可用,都是站在服務器腳本去說的高可用,而不是服務的角度。
也就是說,若是服務器DOWN機或者網絡出現故障,高可用是能夠實現自動切換的。若是運行的服務,好比Nginx掛掉
這些高可用軟件是意識不到的,須要本身寫腳本去實現服務的切換。nginx
2、安裝配置Keepalivedvim
# ./configure # make# make install # cp /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/bin/ # chkconfig --add keepalived # mkdir /etc/keepalived/# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ # vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_DEVEL} vrrp_script chk_nginx { script "/opt/monnginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass mdnginx } virtual_ipaddress { 1.1.1.100 } track_script { chk_nginx } } } 編輯BACKUP的主配置文件(BACKUP:1.1.1.2) # vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_DEVEL} vrrp_script chk_nginx { script "/opt/monnginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass mdnginx } virtual_ipaddress { 1.1.1.100 } track_script { chk_nginx } } 3、啓動服務與腳本編寫(主機和備機都要執行) # service nginx start # service keepalived start 編寫監控Nginx的腳本 # vim /opt/monnginx.sh #!/bin/bash# author: hujianli # date: 2013-03-15 if [ $(ps -C nginx --no-header | wc -l) -eq 0 ]; then service nginx start fi sleep 3 if [ $(ps -C nginx --no-header | wc -l) -eq 0 ]; then service keepalived stop fi 改進腳本,加上報警功能。 # vim /opt/monnginx.sh #!/bin/bash# author: hujianli # date: 2013-03-15 IPADDR=$(ip addr show eth0|awk '{print $2}' | sed -n 3p) contact=(137XXXXXXXX@139.com) num=${#contact[@]} function email() { for ((i=0;i<num;i++));do echo "$IPADDR service problem" | mail -s "WARNING" ${contact[i]} -- -f gm100861@gmail.com done } if [ $(ps -C nginx --no-header | wc -l) -eq 0 ]; then service nginx start fi email sleep 3 if [ $(ps -C nginx --no-header | wc -l) -eq 0 ]; then service keepalived stop email fi