nginx+keepalive 實現高可用負載均衡方案

轉:http://ju.outofmemory.cn/entry/52165php

主nginx負載均衡器:172.26.11.99  (經過keepalived配置了VIP:172.26.11.101供外使用)
副nginx負載均衡器:172.26.11.100 (經過keepalived配置了VIP:172.26.11.101供外使用)nginx

後端web服務器:
172.26.11.73
172.26.11.74web

1、172.26.11.99 以及 172.26.11.100的關鍵nginx配置以下:
vim /etc/nginx/nginx.confubuntu

################# .... upstream  www.xxx.com  { server   172.26.11.73:8080 max_fails=1;#max_fails 表示健康檢查失敗的次數,這裏表示次數爲一次,即標記該服務器down server   172.26.11.74:8080 max_fails=1; } server { listen  80; server_name  www.xxx.com; location / { proxy_next_upstream error timeout http_500 http_502 http_504;  #這裏表示健康檢查涉及到的情形,有這些情形的,都切換到另外的web服務器訪問 proxy_read_timeout 10s;   #這裏表示程序返回的時間,請參考php.ini的max_exe_time來設置。 proxy_pass        http://www.xxx.com; proxy_set_header   Host             $host; proxy_set_header   X-Real-IP        $remote_addr; proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; } #access_log  /var/log/nginx/xxx.log; } ##########################

2、安裝keepalive (centos)vim

#安裝 popt yum -y install popt popt-devel cd /data/software wget http://www.keepalived.org/software/keepalived-1.2.8.tar.gz cd /data/src tar zxf ../software/keepalived-1.2.8.tar.gz cd keepalived-1.2.8 ./configure --prefix=/usr/local/keepalived --sysconf=/etc make && make install cp /usr/local/keepalived/sbin/keepalived /bin/ chkconfig --add keepalived #設置開機啓動 chkconfig keepalived on #啓動keepalive服務 /etc/init.d/keepalived start

若是是ubuntu 直接 apt-get install keepalived 吧….後端

3、keepalive設置
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bakcentos

MASTER
vim /etc/keepalived/keepalived.confbash

! Configuration File for keepalived global_defs { notification_email { admin@test.com } notification_email_from admin@test.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 172.26.11.99 #(多播的源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 { .26.11.101 #(VRRP HA虛擬地址) } }

BACKUP方面只須要修改state爲BACKUP , priority比MASTER稍低便可服務器

4、監控nginx進程的腳本:monitor_nginx.sh 內容以下:
vim /root/monitor_nginx.sh
當檢測到nginx進程不存在的時候,就幹掉全部的keepalived,這時候,請求將會由keepalived的backup接管!!網絡

#!/bin/bash if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ] then killall keepalived fi

chmod +x /root/monitor_nginx.sh

172.26.11.99 172.26.11.100都從新啓動keepalived:
service keepalived restart

這裏請注意,當keepalived啓動後,咱們能夠用命令:

ip add show eth0 來看咱們的eth0網卡確實被添加了虛擬IP,以下圖:

22222222222 nginx+keepalive 實現高可用負載均衡方案

完畢,能夠測試了!

相關文章
相關標籤/搜索