Nginx + Keepalived 配置實例

Nginx_Master: 192.168.1.103 提供負載均衡
Nginx_BackUp: 192.168.1.104 負載均衡備機

Nginx_VIP_TP: 192.168.1.108 網站的 VIP 地址(虛擬 IP)

原理:
VIP 是外網訪問的IP地址,通過 keepalived 設置,以及 VRRP 將 VIP 綁定到主機和備機上,通過權重實現控制。當主機掛掉後,keepalived 釋放對主機的控制,備機接管VIP。

擴展:
主機和備機可進一步延伸,它們只通過 Nginx 提供負載均衡。再讓其它的機器提供真實的 web 服務。

**********************************************************************************************
安裝 Nginx (省略)
-----------------------

Keepalived 安裝
-----------------------
wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
tar -zxf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7

64 位系統:
./configure --sysconf=/etc --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/
或 32 位系統:
./configure --sysconf=/etc --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-358.6.2.el6.i686/

可通過 getconf LONG_BIT 得到系統位數。

參數解釋:
--sysconf 指定了配置文件的地址.即:/etc/keepalived/keepalived.conf
--prefix 指定了安裝目錄
--with-kernel-dir 指定使用內核源碼中的頭文件,即 include 目錄.只有使用 LVS 時才需要這個參數,其它的時候不需要。

如果報錯:
configure: error: Popt libraries is required

解決:
yum install popt-devel

再 configue .成功後提示:

Keepalived configuration
------------------------
Keepalived version       : 1.2.7
Compiler                 : gcc
Compiler flags           : -g -O2
Extra Lib                : -lpopt -lssl -lcrypto 
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
IPVS use libnl           : No
Use VRRP Framework       : Yes
Use VRRP VMAC            : Yes
SNMP support             : No
Use Debug flags          : No

安裝:
make
make install

設置成爲服務並開機啓動:
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

/etc/rc.d/init.d/keepalived status
chkconfig --add keepalived
chkconfig keepalived on

設置主機上的配置文件內容:

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    # 此處是主 Nginx 的 IP 地址.
    mcast_src_ip 192.168.1.103
    # 該機的 priority(優先) 爲 100
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111222
    }
    virtual_ipaddress {
        192.168.1.108
    }
}

前面的結構那裏已經規定好了 VIP 和 主備機的 IP, 所以這裏按上面的填。

備機的配置文件:

! Configuration File for keepalived

global_defs {
   notification_email {
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state SLAVER
    interface eth0
    virtual_router_id 51
    # 此處是備 Nginx 的 IP 地址.
    mcast_src_ip 192.168.1.104
    # 該機的 priority(優先) 爲 99
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111222
    }
    virtual_ipaddress {
        192.168.1.108
    }
}

這時候 ping 192.168.1.108 是不通的.

然後在兩臺機器上分別啓動 keepalived 服務

這時候再 ping 192.168.1.108 .通了.
實際上這時候 108 是被綁到主機上的。在主機上:
查看系統日誌:
tailf /var/log/messages
May 29 18:32:16 localhost Keepalived_vrrp[27731]: Opening file '/etc/keepalived/keepalived.conf'.
May 29 18:32:16 localhost Keepalived_vrrp[27731]: Configuration is using : 62906 Bytes
May 29 18:32:16 localhost Keepalived_vrrp[27731]: Using LinkWatch kernel netlink reflector...
May 29 18:32:16 localhost Keepalived_healthcheckers[27729]: Using LinkWatch kernel netlink reflector...
May 29 18:32:16 localhost Keepalived_vrrp[27731]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
May 29 18:32:17 localhost Keepalived_vrrp[27731]: VRRP_Instance(VI_1) Transition to MASTER STATE
May 29 18:32:18 localhost Keepalived_vrrp[27731]: VRRP_Instance(VI_1) Entering MASTER STATE
May 29 18:32:18 localhost Keepalived_vrrp[27731]: VRRP_Instance(VI_1) setting protocol VIPs.
May 29 18:32:18 localhost Keepalived_vrrp[27731]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.108
May 29 18:32:18 localhost Keepalived_healthcheckers[27729]: Netlink reflector reports IP 192.168.1.108 added

可以看到.VRRP(虛擬路由冗餘協議)已經啓動.我們可以通過命令 ip addr 來檢查主 Nginx 上的 IP 分配情況.

[root@localhost ~]# ip addr
1: lo: mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:15:c5:ef:53:8c brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.103/25 brd 192.168.1.255 scope global eth0
    inet 192.168.1.108/32 scope global eth0
    inet6 fe80::215:c5ff:feef:538c/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 00:15:c5:ef:53:8e brd ff:ff:ff:ff:ff:ff

可以看到 VIP 地址已經綁定到主 Nginx 機器上: inet 192.168.1.108/32 scope global eth0

我們通過 tcpdump 抓包:

[root@localhost ~]# tcpdump vrrp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
13:38:27.797982 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
13:38:28.794693 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
13:38:29.794518 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
13:38:30.798581 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
13:38:31.795902 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
13:38:32.804050 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
13:38:33.801191 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
13:38:34.798793 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20

這樣,一個 Nginx + Keepalived 的架構就完成了。

監控和主備切換
接下來可以完善一下,加上實時監控,如果發現負載均衡的 Nginx 出現問題,就將該機器上的 Keepalived 服務停掉。
nginx_check.sh:

#!/bin/bash
while :
do
nginxpid = 'ps -C nginx --no-header | wc -l'
if[ $nginxpid -eq 0 ];then
service nginx start
sleep 3
nginxpid = 'ps -C nginx --no-header | wc -l'
echo $nginxpid
if[ $nginxpid -eq 0 ];then
service keepalived stop
fi
fi
sleep 3
done

然後讓該腳本一直在後臺運行:
nohup /etc/nginx_check.sh
或者將它添加成服務,讓它開機自啓動:

測試:
--------------------
在兩臺機器的 web 服務器上分別放一個 index.html, 裏面內容分別是自己機器的IP.
通過VIP訪問:
http://192.168.1.108/index.html 發現顯示的是主機的IP.
此時,關掉主機的 nginx, 這時候由於上面的監控腳本。主機的 keepalived 也會關閉。這時候再訪問上面地址,發現顯示的是備機的IP。可見,切換成功。

負載均衡
--------------------
通過 ip_hash 實現會話保持
發佈了12 篇原創文章 · 獲贊 7 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章