1. 程式人生 > >Nginx+keepalived雙機熱備(主從模式)

Nginx+keepalived雙機熱備(主從模式)

簡單介紹:

Keepalived是Linux下面實現VRRP備份路由的高可靠性執行軟體,能夠真正做到

主伺服器和備份伺服器故障時IP瞬間無縫交接;

Keepalived的目的是模擬路由器的高可用;

Heartbeat或Corosync的目的是實現Service的高可用.

那heartbaet與corosync又應該選擇哪個好?

corosync的執行機制更優於heartbeat,從heartbeat分離出來的pacemaker在以後的開發當中

更傾向於corosync,所以現在corosync+pacemaker是最佳組合.

環境說明:

master機器(master-node):10.0.0.5/172.16.1.5

slave機器(slave-node):10.0.0.6/172.16.1.6

公用的虛擬IP(VIP):10.0.0.3

網站URL:

svn-------dev.qingfeng.com/svn(10.0.0.8:801/svn)

svn web---dev.qingfeng.com/submin(10.0.0.8:801/submin)

網站------www.qingfeng.com(10.0.0.7:80&10.0.0.8:80)

oa--------oa.qingfeng.com(10.0.0.7:802&10.0.0.8:802)

反向代理總結:

多域名指向是通過虛擬主機的不同server實現;

同一域名的不同虛擬目錄是通過每個server下面的不同location

實現;

反向代理到後端的伺服器需要在vhost/LB.conf下面配置upstream,

然後在server或location中通過proxy_pass引用.

我們的目的:

如果master主伺服器的keepalived停止服務,slave從伺服器會自動接管VIP對外服務;

一旦主伺服器的keepalived恢復,會重新接管VIP.

這並不是我們需要的,我們需要的是當NginX停止服務,無法啟動時,能夠自動切換.

# ip addr add 10.0.0.3/24 dev eth0 label eth0:0

yum -y install keepalived

1.master的keepalived配置

cat /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
    [email protected] 
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id lb01
}

vrrp_script chk_http_port {
    script "/service/scripts/chk_nginx.sh"  # 通過指令碼監測
    interval 2  # 指令碼執行間隔,每2s檢測一次
    weight -5   # 指令碼結果導致的優先順序變更,檢測失敗(指令碼返回非0)則優先順序 -5
    fall 2      # 檢測連續2次失敗才算確定是真失敗,會用weight減少優先順序(1-255之間)
    rise 1      # 檢測1次成功就算成功,不修改優先順序
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.3/24 dev eth0 label eth0:1
    }

    track_script {
        chk_http_port  # 這個設定不能緊挨著寫在vrrp_script配置塊的後面,否則nginx監控失效.
    }
}

2.監測nginx狀態的指令碼

mkdir -p /service/scripts
cat /service/scripts/chk_nginx.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    /application/nginx/sbin/nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
        /etc/init.d/keepalived stop
        exit 1
    fi
fi
chmod +x chk_nginx.sh

3.slave的keepalived配置

global_defs {
   notification_email {
    [email protected] 
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id lb02
}

vrrp_script chk_http_port {
    script "/service/scripts/chk_nginx.sh"
    interval 2
    weight -5
    fall 2
    rise 1
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.3/24 dev eth0 label eth0:1
    }

    track_script {
        chk_http_port
    }
}

4.總結:

測試機10.0.0.5

cat /etc/hosts
10.0.0.3      dev.qingfeng.com www.qingfeng.com oa.qingfeng.com
[[email protected] ~]# curl dev.qingfeng.com/svn/
this is the page of svn-10.0.0.8
[[email protected] ~]# curl dev.qingfeng.com/submin/
this is the page of submin-10.0.0.8
[[email protected] ~]# curl www.qingfeng.com
www-10.0.0.7:80
[[email protected] ~]# curl www.qingfeng.com
www-10.0.0.8:80

用ifconfig無法檢視到虛擬ip時,可以用ip addr

無論master還是slave,當其中的一個keepalived服務停止後,vip都會漂移到keepalived還存活的節點上;

如果master上的nginx服務掛了,則nginx會自動重啟,重啟失敗後會自動關閉keepalived,vip也會轉移到slave上.

 

主從模式參考部落格:https://www.cnblogs.com/kevingrace/p/6138185.html