首頁 > 後端開發 > php教程 > Keepalived+nginx實現雙主高可用負載平衡

Keepalived+nginx實現雙主高可用負載平衡

WBOY
發布: 2016-08-08 09:29:52
原創
1478 人瀏覽過
Nginx+keepalived高可用有兩種設定方案:
1、Nginx+keepalived 主從設定
這種方案,使用一個vip位址,前端使用2台機器,一台做主,一台做備,但同時只有一台機器工作,另一台備份機器在主機器不故障的時候,永遠處於浪費狀態,對於伺服器不多的網站,該方案不經濟實惠,所以本次不予採用。
2、Nginx+keepalived 雙主配置

這種方案,使用兩個vip位址,前端使用2台機器,互為主備,同時有兩台機器工作,當其中一台機器出現故障,兩台機器的請求轉移到一台機器負擔,非常適合於當前架構環境,故本次採用此方案對網站進行高可用架構。

下面我們以第二種配置演示實現雙主高可用負載平衡。


第一組

VIP1:192.168.36.100036.10005313236383836.100.36.1005236.3396.

備nginx:192.168.36.86

第二組

VIP2:192.168.36.100

主nginx:192.168.36.86%

web服務:

web1:

192.168.36.215

web2:

192.168.36.80

一、nginx配置(安裝省略). 6.86的nginx。檢查訪問是否正常.

web1

http://192.168.36.86:88/

web2

http://192.168.36.86:99/
二、安裝、設定keepalived(安裝省略)

1)192.168.36.86配置vi /etc/

keepalived

/keepD. 2)192.168.36.99配置

vi /etc/keepalived

/keepalived.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

    upstream web1 {
        server 192.168.36.215:80 max_fails=3 fail_timeout=3s;
    }
    upstream web2 {
        server 192.168.36.80:80 max_fails=3 fail_timeout=3s;
    }
    server {
            listen         88;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
                proxy_pass http://web1;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
   }
   server {
            listen         99;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
                proxy_pass http://web2;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
   }
}
登入後複製
3)兩台機器都需要有nginx進程檢查腳本,當檢測不到進程的時候切換到nginx,無法重啟就讓keeppved備機。 vi /etc/keepalived/chk_nginx.sh

! Configuration File for keepalived

global_defs {
   notification_email {
     monitor@3evip.cn
     #failover@firewall.loc
   }
   notification_email_from tianwei7518@163.com
   smtp_server smtp.163.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
        script "/etc/keepalived/chk_nginx.sh"
        interval 2
        weight 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 100
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.36.100
    }
}


vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 200
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }   
    virtual_ipaddress {
        192.168.36.200
    }
}
登入後複製
設定執行權限:chmod 、測試 分別啟動nginx和keepalived:

 /usr/local/nginx/sbin/nginx

service keepalived start

service keepalived start

http://192.168. 36.100:88/

http://192.168.36.200:88/

web2http://192.1682 2.168.36.200:99 /

可以正常存取。

我們可以試一下,關閉一個keepalived。關閉192.168.36.86上的

keepalived


service keepalived stop

service keepalived stop

tail -f /var/log/messages 

192.168.36.86日誌:

! Configuration File for keepalived

global_defs {
   notification_email {
     monitor@3evip.cn
     #failover@firewall.loc
   }
   notification_email_from tianwei7518@163.com
   smtp_server smtp.163.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
        script "/etc/keepalived/chk_nginx.sh"
        interval 2
        weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 100
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.36.100
    }
}


vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 200
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }   
    virtual_ipaddress {
        192.168.36.200
    }
}
登入後複製
192.168.36.99日誌:

#!/bin/sh
#description: # 如果启动失败,则停止keepalived
status=$( ps -C nginx --no-heading| wc -l)
if [ "${status}" = "0" ]; then
        /usr/local/nginx/sbin/nginx
        status2=$( ps -C nginx --no-heading| wc -l)
        if [ "${status2}" = "0" ]; then
                service keepalived stop
        fi
fi
登入後複製

再開啟192.168.36.86上的

86日誌:

Feb  7 00:39:05 slave-d Keepalived[5738]: Stopping Keepalived v1.2.15 (02/07,2015)
Feb  7 00:39:05 slave-d Keepalived_vrrp[5741]: VRRP_Instance(VI_2) sending 0 priority
Feb  7 00:39:05 slave-d Keepalived_vrrp[5741]: VRRP_Instance(VI_2) removing protocol VIPs.
Feb  7 00:39:05 slave-d Keepalived_healthcheckers[5740]: Netlink reflector reports IP 192.168.36.200 removed
Feb  7 00:39:05 slave-d avahi-daemon[1823]: Withdrawing address record for 192.168.36.200 on eth0.
登入後複製
192.168.36.99日誌:

Feb  7 00:39:11 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Transition to MASTER STATE
Feb  7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Entering MASTER STATE
Feb  7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) setting protocol VIPs.
Feb  7 00:39:12 slave-c Keepalived_healthcheckers[25102]: Netlink reflector reports IP 192.168.36.200 added
Feb  7 00:39:12 slave-c avahi-daemon[1832]: Registering new address record for 192.168.36.200 on eth0.IPv4.
Feb  7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200
Feb  7 00:39:17 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200
登入後複製
關閉後依舊可以正常存取。 關閉nginx後,可以看到nginx立即有啟動。

參考文章:CentOS6.5 keepalived詳解及實作Nginx服務的高可用性
用NginX+用NginX+可用的可用負載和雙主節點負載平衡

nginx+keepalived實現nginx雙主高可用的負載平衡


以上就介紹了Keepalived+nginx實現雙主高可用負載平衡,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板