Nginx+keepalived負載均衡篇

火星人 @ 2014-03-03 , reply:0
←手機掃碼閱讀

Nginx+keepalived負載均衡篇

由於nginx的url hash功能可以很好的提升squid的性能,所以我把squid前端的負載均衡器更換為nginx,但是一台nginx就形成了單點,現在使用keepalived來解決這個問題,keepalived的故障轉移時間很短,而且配置簡單,這也是選擇keepalived的一個主要原因,建議日PV值小的中小型企業web均可採用如下方案實行,下面直接上安裝步驟:



一、環境:
centos5.3、nginx-0.7.51、keepalived-1.1.19
主nginx負載均衡器:192.168.0.154
輔nginx負載均衡器:192.168.9.155
vip:192.168.0.188



二、安裝keepalived


#tar zxvf keepalived-1.1.19.tar.gz
#cd keepalived-1.1.19
#./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 {
    yuhongchun027@163.com
         }
    notification_email_from keepalived@chtopnet.com
    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
我們來看一下日誌:
# tail /var/log/messages


Oct 6 03:25:03 ltos avahi-daemon: Registering new address record for 192.168.0.188 on eth0.
Oct 6 03:25:03 ltos avahi-daemon: Registering new address record for 192.168.0.154 on eth0.
Oct 6 03:25:03 ltos avahi-daemon: Registering HINFO record with values 'I686'/'LINUX'.
Oct 6 03:25:23 ltos avahi-daemon: Withdrawing address record for fe80::20c:29ff:feb9:eeab on eth0.
Oct 6 03:25:23 ltos avahi-daemon: Withdrawing address record for 192.168.0.154 on eth0.
Oct 6 03:25:23 ltos avahi-daemon: Host name conflict, retrying with
Oct 6 03:25:23 ltos avahi-daemon: Registering new address record for fe80::20c:29ff:feb9:eeab on eth0.
Oct 6 03:25:23 ltos avahi-daemon: Registering new address record for 192.168.0.188 on eth0.
Oct 6 03:25:23 ltos avahi-daemon: Registering new address record for 192.168.0.154 on eth0.
Oct 6 03:25:23 ltos avahi-daemon: Registering HINFO record with values 'I686'/'LINUX'.



很顯然vrrp已經啟動,我們還可以通過命令:#ip a 來檢查

# ip a


1: lo: 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: 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: mtu 1480 qdisc noop
     link/sit 0.0.0.0 brd 0.0.0.0


說明vip已經啟動,這樣主伺服器就配置好了,輔機的配置大致一樣,除了配置文件有少部分的變化,下面貼出輔機的配置文件:



! Configuration File for keepalived
global_defs {
    notification_email {
    yuhongchun027@163.com
         }
    notification_email_from keepalived@chtopnet.com
    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
     }
}



檢查其配置
# ip a


1: lo: 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: 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: mtu 1480 qdisc noop
     link/sit 0.0.0.0 brd 0.0.0.0


測試其效果方法很簡單,分別在主輔機上建立不同的主頁,index.html的內容分別為192.168.0.154,192.168.0.155,然後用客戶機上elinks http://192.168.0.188,主機down掉后輔機會馬上接替提供服務,間隔時間幾乎無法感覺出來,
《解決方案》

這麼做我發現了一個問題 就是假如服務DOWN了 但網路正常通信 服務是不會飄過來的
《解決方案》

還是做一個腳本來檢測服務,可以去查詢一下 keepalived 中執行腳本就可以解決樓上的問題




[火星人 via ] Nginx+keepalived負載均衡篇已經有334次圍觀

http://www.coctec.com/docs/service/show-post-260.html