一、總體架構圖以下
linux
二、環境準備nginx
今天所配置的是keepalived+nginx 的負載均衡服務器
下載keepalived軟件架構
[root@LB01 tools]# wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz負載均衡
注意安裝前檢查內核的link文件ide
root@LB02 tools]# ll /usr/src/測試
total 8spa
drwxr-xr-x. 2 root root 4096 Sep 23 2011 debugdebug
drwxr-xr-x. 3 root root 4096 Oct 19 02:03 kernels代理
lrwxrwxrwx. 1 root root 43 Oct 19 02:05 linux -> /usr/src/kernels/2.6.32-642.6.1.el6.x86_64/
安裝keepalived以前,安裝幾個依賴包
yum install openssl-devel -y
yum install popt* -y
而後進行編譯安裝keepalived,前面介紹了安裝過程,這裏就不演示了
./configure得出下面的結果
Keepalived configuration
------------------------
Keepalived version : 1.1.17
Compiler : gcc
Compiler : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
Use VRRP Framework : Yes
Use LinkWatch : No
Use Debug flags : No
注意./configure以後的結果,沒有錯誤就能夠了
make && make install
以後規範配置、啓動文件路徑
/bin/cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
/bin/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived -p
/bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
/bin/cp /usr/local/sbin/keepalived /usr/sbin/
/etc/init.d/keepalived start
注:nginx負載均衡相關配置請參考前面的文章
三、實戰配置keepalived
[root@LB01 keepalived]# vi keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
abc@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 1.1.1.1
smtp_connect_timeout 30
router_id LVS_3
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 19
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254/24
}
}
[root@LB02 keepalived]# vi keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
abc@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 1.1.1.1
smtp_connect_timeout 30
router_id LVS_6
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 19
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254/24
}
}
[root@LB01 keepalived]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
[root@LB02 keepalived]# /etc/init.d/keepalived start
Starting keepalived [ OK ]
[root@LB01 keepalived]# ip add|grep 192.168.1.254
inet 192.168.1.254/24 scope global secondary eth0
[root@LB02 keepalived]# ip add|grep 192.168.1.254
測試訪問
代表能夠正常切換
如今咱們模擬keepalived主宕機,再測試
[root@LB01 conf]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]
[root@LB02 ~]# ip add|grep 254
inet 192.168.1.254/24 scope global secondary eth0
四、反向代理服務故障自動切換
若是實際生產環境中當keeplived主的服務器nginx服務宕機,可是主又有VIP,這時就出現沒法訪問的現象,所以能夠作以下的配置,使得這種狀況可自已切換
vi check_nginx.sh
#!/bin/sh
white true
do
PNUM=`ps -ef|grep nginx|wc -l`
#這裏也可以使用nmap 192.168.1.3 -p 80|grep open|wc -l來判斷個數
if [ $PNUM -lt 3 ];then
/etc/init.d/keepalived stop >/dec/null 2>&1
kill -9 keealived >/dec/null 2>&1
kill -9 keealived >/dec/null 2>&1
fi
sleep 5
done
sh check_nginx.sh &
啓動個守護進程進行檢查(或者加入定時任務定時執行檢查),若是nginx服務出現故障,就立馬停掉keepalived的服務,讓它自動切換到備節點上去,這樣就實現了自動切換的工做