nginx + keepalive 實現HA

來源:互聯網
上載者:User

  一、拓撲環境:
 
主nginx負載平衡器 192.168.166.203
輔nginx負載平衡器 192.168.166.177
VIP地址 192.168.166.178   http://www.keepalived.org/download.html
tar -zxvf keepalived-1.2.1.tar.gz
cd keepalived-1.2.1
./configure --prefix=/usr/local/keepalived --sysconf=/etc make && make install
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /bin/
chkconfig --add keepalived
chmod 755 /etc/init.d/keepalived
chkconfig keepalived on
service keepalived start   二.修改設定檔為以下內容: [master slave]
MASTER
vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs {  notification_email {
  xxx@xxx.com
} notification_email_from xxx@xxx.com
smtp_server xxx.smtp.com
smtp_connect_timeout 30
router_id LVS_DEVEL
} vrrp_script Monitor_Nginx {
 script "/root/monitor_nginx.sh"
 interval 2
 weight 2
} vrrp_instance VI_1 {
 state MASTER #(主機為MASTER,備用機為BACKUP)
 interface eth0 #(HA監測網路介面)
 virtual_router_id 61 #(主、備機的virtual_router_id必須相同)
 #mcast_src_ip 192.168.166.203 #(多播的源IP,設定為本機外網IP,與VIP同一網卡)此項可不設定
 priority 90 #(主、備機取不同的優先順序,主機值較大,備份機值較小,值越大優先順序越高)
 advert_int 1 #(VRRP Multicast廣播周期秒數)
 authentication {
 auth_type PASS #(VRRP認證方式)
 auth_pass 1234 #(密碼)
}
 track_script {
 Monitor_Nginx #(調用nginx進程檢測指令碼)
}
 virtual_ipaddress {
 192.168.166.178 #(VRRP HA虛擬位址)
 }
}
###BACKUP方面只需要修改state為BACKUP , priority比MASTER稍低就OK了   三:註:monitor_nginx.sh為監控nginx進程的指令碼,內容如下 #!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
 /usr/local/nginx/sbin/nginx
 sleep 5
 if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
 then
 killall keepalived
 fi
fi #chmod +x monitor_nginx.sh   四.啟動keepalived
service keepalived start
或
/usr/local/keepalived/sbin/keepalived -D -f /etc/keepalived/keepalived.conf   五.測試步驟 1.    訪問VIP看是否能夠正常訪問後端的web
2.    停止其中一個web看是否能將訪問轉到另一台上
3.    停止兩台nginx上任何一個nginx進程看監控進程指令碼是否會自動啟動nginx
4.    停止任何一台nginx上的keepalived進程看另一台是否接管vip
track_interface的意思是將Linux中你想監控的網路介面卡監控起來,當其中的一塊出現故障是keepalived都將視為路由器出現故障。
這裡請注意: virtual_router_id 61,同一組master/backup中,也就是如果你設定了多個VRRP,同一組這個ID必須相同,不然啟動的時候主從都會把VIP給啟動了 keepalived 通過傳送接收群組播包中的同一個virtual_router_id 的中的成員的存活,來確定對方的不可用,一旦檢測到對方的不可用,即會切換它的備份角色為主。
即:當真實機192.168.166.177上的keepalived 檢測到 真實機192.168.166.203上的keepalived 不可用時,177上將使用vip:192.168.166.178對外服務並由backup角色轉變為master      

keepalived+nginx實現 HA與負載平衡中keepalived負責實現HA功能,控制VIP(虛擬網路地址)轉移功能,nginx實現對realserver七層負載平衡功能,nginx提供代理服務和cache功能,DNS服務可以負責前段VIP的負載平衡。

keepalived的配置
192.168.200.1 keepalived配置
! Configuration File for keepalived

global_defs {
notification_email {
xxx@xxx.com
}
notification_email_from xxx@xxx.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_TEST_YDQ
}

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 61
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.101/24 dev eth0 label eth0:0
}
}

vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 62
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.200.102/24 dev eth0 label eth0:1
}
}


vrrp_instance VI_3 {
state BACKUP
interface eth0
virtual_router_id 63
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 3333
}
virtual_ipaddress {
192.168.200.103/24 dev eth0 label eth0:1
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.