1、爲何要使 用負載均衡技術?
php
一、系統高可用性linux
二、 系統可擴展性算法
三、 負載均衡能力後端
LVS+keepalived能很好的實現以上的要求,LVS提 供負載均衡,keepalived提供健康檢查,故障轉移,提升系統的可用性!採用這樣的架構之後 很容易對現有系統進行擴展,只要在後端添加或者減小realserver,只要更改lvs的 配置文件,並能實現無縫配置變動!bash
2、LVS+Keepalived介紹服務器
一、 LVS網絡
LVS是一個開源的軟件,能夠實現LINUX平臺下的簡單負載均衡。LVS是Linux Virtual Server的縮寫,意思是Linux虛擬服務器。目前有三種IP負 載均衡技術(VS/NAT、VS/TUN和VS/DR);八種調度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。架構
二、 keepalivedapp
Keepalived 是運行在lvs 之上,它的主要功能是實現真實機的故障隔離及負載均衡器間的失敗 切換,提升系統的可用性負載均衡
3、LVS+keepalived負載均衡架構圖
4、LVS+keepalived的安裝和配置
1. 配置環境
System OS:CentOS release 5.4
Software:ipvsadm-1.24.tar.gz, keepalived-1.1.19.tar.gz
安裝lvs
分別在backup lvs和master lvs上安裝
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
ln -s /usr/src/kernels/2.6.18-164.el5-i686/ /usr/src/linux
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make && make install
安裝keepalived
分別在backup lvs和master lvs上安裝
wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
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
配置keepadlived
LVS-Master的配置文件以下
[root@linux5 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
jimo291@gmail.com #email 通知
}
notification_email_from jimo291@gmail.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS1 # 設置lvs的id,在一個網絡內應該是惟一的
}
vrrp_sync_group test { #設置vrrp組
group {
loadbalance
}
}
vrrp_instance loadbalance {
state MASTER #設置lvs的狀態,報錯MASTER和BACKUP兩種,必須大寫
interface eth0 #設置對外服務的接口
lvs_sync_daemon_inteface eth0 #設置lvs監聽的接口
virtual_router_id 51 #設置虛擬路由表示
priority 180 #設置優先級,數值越大,優先級越高
advert_int 5 #設置同步時間間隔
authentication { #設置驗證類型和密碼
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #設置lvs vip
192.168.1.115
}
}
virtual_server 192.168.1.115 80 {
delay_loop 6 #健康檢查時間間隔
lb_algo rr #負載均衡調度算法
lb_kind DR #負載均衡轉發規則
#persistence_timeout 20 #設置會話保持時間,對bbs等頗有用
protocol TCP #協議
real_server 192.168.1.105 80 {
weight 3 #設置權重
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.103 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.104 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
LVS-backup的配置文件以下
global_defs {
notification_email {
jimo291@gmail.com
}
notification_email_from jimo291@gmail.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS2
}
vrrp_sync_group test {
group {
loadbalance
}
}
vrrp_instance loadbalance {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 150
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.115
}
}
virtual_server 192.168.1.115 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 20
protocol TCP
real_server 192.168.1.105 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.103 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.104 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
Realserver的配置
三臺客戶端的腳本都同樣!
cat /etc/rc.d/init.d/realserver.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
SNS_VIP=192.168.1.115
/etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -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)
ifconfig lo:0 down
route del $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}"
exit 1
esac
exit 0