Nginx+keepalived負載均衡高可用篇第③版

Nginx+keepalived負載均衡高可用篇第③版

對付中、小型企業,假如沒有資金去購買昂貴的四/七層負載均衡交換機,那麼Nginx是不錯的七層負載均衡選擇,並且可以通過Nginx + Keepalived實現Nginx 負載均衡器雙機互備,恣意一臺呆板發生妨礙,對方都可以或許將假造IP吸取已往。以下內容爲第三次更新了,下面將其安裝步調具體闡發下:


一、拓撲環境:
二、安裝keepalived
wget

tar zxvf keepalived-1.1.15.tar.gz
cd keepalived-1.1.15
./configure --prefix=/usr/local/keepalived
make
make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived
cd /etc/keepalived/

vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {

}
notification_email_from

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
mcast_src_ip 192.168.0.154 <==主nginx的IP所在
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.0.188 <==vip所在
}
}
#service keepalived start
我們來看一下日記:
[root@ltos ~]# tail /var/log/messages
Oct 6 03:25:03 ltos avahi-daemon[2306]: Registering new address record for 192.168.0.188 on eth0.
Oct 6 03:25:03 ltos avahi-daemon[2306]: Registering new address record for 192.168.0.154 on eth0.
Oct 6 03:25:03 ltos avahi-daemon[2306]: Registering HINFO record with values 'I686'/'LINUX'.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Withdrawing address record for fe80::20c:29ff:feb9:eeab on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Withdrawing address record for 192.168.0.154 on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Host name conflict, retrying with <ltos-31>
Oct 6 03:25:23 ltos avahi-daemon[2306]: Registering new address record for fe80::20c:29ff:feb9:eeab on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Registering new address record for 192.168.0.188 on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Registering new address record for 192.168.0.154 on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Registering HINFO record with values 'I686'/'LINUX'.

很顯然vrrp已經啓動,我們還可以通過下令來反省
[root@ltos html]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:ba:9b:e7 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.154/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.188/32 scope global eth0
inet6 fe80::20c:29ff:feba:9be7/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0

闡發vip已經啓動,如許主理事器就設置好了,輔機的設置大抵一樣,除了設置文件有少部分的變動,下
面貼出輔機的設置文件:
! Configuration File for keepalived
global_defs {
notification_email {

}
notification_email_from

smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.0.155 <==輔nginx的IP的所在
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.0.188 <==vip所在
}
}

反省其設置
[root@ltos html]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:ba:9b:e7 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.155/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.188/32 scope global eth0
inet6 fe80::20c:29ff:feba:9be7/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0

測試其效果行動很大略,判別在主輔機上/var/www/html/下成立差別的主頁及內容文件index.html,內容判別爲192.168.0.154,192.168.0.155,然後用客戶機上elinks ,主機down掉後輔機會立刻接替供給做事,隔斷工夫險些無法感傷出來;但假如是停掉master的nginx做事,keepalived就不能主動啓動nginx做事了,這個也是相對付heartbeat2.X不完美的地方,即它不能做到做事級別-Nginx的HA。


※這裏要闡發的是:一、怎樣讓master狀況的主機掛掉後不搶佔的辦法:可將二臺Nginx負載均衡器均設成backup狀況是由於nopreempt(不搶佔)只支持backup模式,而讓哪一臺backup成爲master,這個由優先級priority爲決議,這個設置是參考了《Keepalived權勢鉅子指南》做法,二、此種架構的錯誤是備機做事器處於閒置狀況,浪費了硬件資源,假如要有效的操作二臺nginx負載均衡器,可參考張宴的金山悠閒網nginx負載均衡的雙機熱備的做法;三、keepalived比擬heartbeat2.X而言,它如今不能做nginx做事的HA,但由於如今nginx作反向署理/負載均衡器非常穩定出現宕機的機率微乎其乎(關鍵是nginx的master進程超穩定),我做的一些基於nginx負載均衡的小項目已在線穩定一年以上(險些從來沒出現單nginx負載均衡掛掉的情況),以是這個可以操縱於成熟的生產環境(譬喻新浪);在生產環境下,比起用shell腳本監控nginx的master狀況,反而效果不如採用nagios/手機短信報警的行動更爲高效。

51cto.com/a/luyoujiaohuan/index.html
http://www.net527.com
Linux 系統


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章