LVS+keepalivedhtml
OS: Centos 6.4 Bit64linux
Lvs vip: 172.16.10.188web
LvsKeepalived –master:172.16.10.160算法
Lvskeepalived –backup: 172.16.10.161後端
realserverWeb1:172.16.10.162bash
realserverWeb2:172.16.10.163服務器
軟件:ipvsadm keepalived網絡
Lvs Keepalived –maste和Lvs keepalived –backup安裝app
一.ln -s/usr/src/kernels/2.6.32-358.6.2.el6.x86_64/ /usr/src/linux 和內核結合負載均衡
rpm -ivh ipvsadm-1.25-9.el6.x86_64.rpm
/etc/rc.d/init.d/ipvsadmrestart
二.
三.wget http://www.keepalived.org/software/keepalived-1.2.6.tar.gz
四.tar zxvf keepalived-1.2.6.tar.gz
五.cd keepalived-1.2.6
./configure --prefix=/usr/local/keepalived
六.make -j 2 && make install 二個CPU編譯
七.
八.將keepalived作成啓動腳務,方便管理:
cp/usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp/usr/local/keepalived/sbin/keepalived /usr/sbin/
mkdir /etc/keepalived
cp/usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
#service keepalived start|stop
開啓負載服務器路由機制,分別在master和backup上操做:
vi/etc/sysctl.conf
net.ipv4.ip_forward = 1
執行sysctl -p,當即生效。
Keealived的配置:
分別在主從負載均衡服務器上配置/etc/keepalived/keepalived.conf,內容分別以下:
Lvs Keepalived –master配置.
global_defs {
notification_email {
info@139.com#email通知
}
notification_email_from info@139.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS1 #設置lvs的id,在一個網絡內應該是惟一的
}
vrrp_instanceVI_1 {
state MASTER #lvs的狀態,MASTER或BACKUP兩種,必須大寫
interface eth0 #設置對外服務的接口
virtual_router_id 51 #設置虛擬路由表示
priority 100 #設置優先級,數值越大,優先級越高
advert_int 1 #設置同步時間間隔
authentication { #設置驗證類型和密碼
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #設置lvs vip,若是有多個VIP,繼續換行填寫.
172.16.10.188
}
}
virtual_server 172.16.10.188 80 {
delay_loop 6 #健康檢查時間間隔
lb_algo rr #負載均衡調度算法,八種.
lb_kind DR #負載均衡轉發規則,DR(改寫請求MAC,傳到真實服務
器,並直接返回給客戶,需同一網段)NAT(地址轉換,地
址重寫再返回給客戶)
TUN(相似NAT,後端操做系統需支持"IP Tunneling"協議)
persistence_timeout 50 #設置會話保持時間,對bbs等頗有用
protocol TCP #協議
real_server 172.16.10.162 80 {
weight 1 #設置權重
TCP_CHECK {
connect_port 80
connect_timeout 30 #(30秒無響應超時)
}
}
real_server 172.16.10.163 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 30
}
}
}
Lvs keepalived –backup配置。
global_defs {
notification_email {
info@139.com#email通知
}
notification_email_from info@139.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS2 #設置lvs的id,在一個網絡內應該是惟一的
}
vrrp_instanceVI_1 {
state BACKUP #lvs的狀態,MASTER或BACKUP兩種,必須大寫
interface eth0 #設置對外服務的接口
virtual_router_id 51 #設置虛擬路由表示
priority 99 #設置優先級,數值越大,優先級越高
advert_int 1 #設置同步時間間隔
authentication { #設置驗證類型和密碼
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #設置lvs vip,若是有多個VIP,繼續換行填寫.
172.16.10.188
}
}
virtual_server 172.16.10.188 80 {
delay_loop 6 #健康檢查時間間隔
lb_algo rr #負載均衡調度算法,rr,wrr,lc,wlc,lblc,lblcr,dh,sh
lb_kind DR #負載均衡轉發規則,DR(改寫請求MAC,傳到真實服務器,
並直接返回給客戶,需同一網段)NAT(地址轉換,地址重寫
再返回給客戶)TUN(同上)
persistence_timeout 50 #設置會話保持時間,對bbs等頗有用
protocol TCP #協議
real_server 172.16.10.162 80 {
weight 1 #設置權重
TCP_CHECK {
connect_port 80
connect_timeout 30 #(30秒無響應超時)
}
}
real_server 172.16.10.163 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 30
}
}
}
Realserver的配置,兩臺web服務器增長以下腳本:
chmod +x /etc/rc.d/init.d/realserver.sh
/etc/rc.d/init.d/realserver.sh start
echo /etc/rc.d/init.d/realserver.sh start >> /etc/rc.local
cat /etc/rc.d/init.d/realserver.sh
#!/bin/bash
# description: Config realserver lo andapply noarp
SNS_VIP=172.16.10.188
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfiglo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/routeadd -host $SNS_VIP dev lo:0
echo"1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo"2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo"1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo"2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl-p >/dev/null 2>&1
echo"RealServer Start OK"
;;
stop)
ifconfiglo:0 down
routedel $SNS_VIP >/dev/null 2>&1
echo"0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo"0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo"0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo"0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo"RealServer Stoped"
;;
*)
echo"Usage: $0 {start|stop}"
exit1
esac
exit 0
lo:0 Link encap:Local Loopback
inet addr:172.16.10.188 Mask:255.255.255.255
UPLOOPBACK RUNNING MTU:16436 Metric:1
查看lvs服務是否正常:
1、watch ipvsadm –ln
2、tail–f /var/log/message 監聽日誌,測試LVS負載均衡及高可用性是否有效。
3、停Master服務器的keepalived服務,查看BAKCUP服務器是否能正常接管服務。
參考:
http://network.51cto.com/art/201006/206831.htm
一個很方便管理LVS+Keepalived負載均衡平臺的B/S軟件。
https://code.google.com/p/tianyalvs