一:安裝配置 LVS+DR+Keepalived
client 訪問的地址 VIP 192.168.133.100
負載服務器master真實IP 192.168.133.128
負載服務器backup真實IP 192.168.133.150
負載服務器虛擬IP 192.168.133.100
後端WEB服務器IP 192.168.133.128
後端WEB服務器IP 192.168.133.150
系統:Redhat 5.4 x86 內核:2.6.18-164.el5
1.下載所須要的軟件:ipvsadm keepalived
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz 我這裏是直接安裝的RHEL4.0自帶的ipvsadm-1.24-6.i386.rpm
wget http://www.keepalived.org/software/keepalived-1.1.18.tar.gz
2.安裝ipvsadm(負載均衡器都要配置master和backup)
從Linux內核版本2.6起,ip_vs code已經被整合進了內核中,所以,只要在編譯內核的時候選擇了ipvs的功能,您的Linux即能支持LVS。Linux 2.4.23之後的內核版本也整合了ip_vs code,但如 果是更舊的內核版本,您得本身手動將ip_vs code整合進內核原碼中,並從新編譯內核方可以使用lvs。
我使用的RHEL4.0 這個版本支持ipvsadm,因此無需更改內核配置 rpm -ivh ipvsadm-1.24-6.i386.rpm
而後使用ipvsadm -v
ipvsadm v1.24 2005/12/10 (compiled with popt and IPVS v1.2.1)
則Ipvsadm安裝成功
3.安裝keepalived負載均衡器都要配置master和backup) 如下建議徹底參照,不然會有一些問題
tar zxvf keepalived-1.1.18tar.gz
cd keepalived-1.1.18
./configure --prefix=/usr/local/keepalived
make
make install
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
啓動keepalived
service keepalived start|stop|restartphp
4.配置master和backup
A.開啓ip_forward 轉發功能:
echo 1 >/proc/sys/net/ipv4/ip_forward 默認是0,關閉ip轉發;這裏須要開啓,因此設置值爲1
B.配置keepalived.conf 配置文件 (backup 192.168.133.150)
! Configuration File for keepalivedhtml
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.133.128
smtp_connect_timeout 30
router_id LVS_DEVEL
}linux
vrrp_instance VI_1 { #配置負載均衡器的 主和備 ,並作相關設置
state BACKUP #備份服務器上將MASTER改成BACKUP
interface eth0 #HA監測網絡接口
virtual_router_id 51 #主、備機的virtual_router_id必須相同
priority 100 #主、備機取不一樣的優先級,主機值較大,備份機值較小
advert_int 1 #VRRP Multicast廣播週期秒數
authentication {
auth_type PASS #VRRP認證方式
auth_pass 1111 #VRRP口令字
}
virtual_ipaddress {
192.168.133.100 #(若是有多個VIP,繼續換行填寫.)
}
}web
virtual_server 192.168.133.100 80 { VIP設置
delay_loop 2 #每隔2秒查詢realserver狀態
lb_algo rr #lvs 算法
lb_kind DR #Direct Route
nat_mask 255.255.255.0
persistence_timeout 50 #同一IP的鏈接60秒內被分配到同一臺realserver
protocol TCP #用TCP協議檢查realserver狀態算法
real_server 192.168.133.128 80 { 真實主機1配置
weight 100 #設置後臺主機的權重
TCP_CHECK { #TCP協議檢查
connect_timeout 3 #
nb_get_retry 3 #
delay_before_retry 3 #
}
}
real_server 192.168.133.150 80 { 真實主機2配置
weight 1 #設置後臺主機的權重
TCP_CHECK { #TCP協議檢查
connect_timeout 3 #
nb_get_retry 3 #
delay_before_retry 3 #
}
}
}
配置keepalived.conf 配置文件 (master 192.168.133.128)
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.133.128
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.133.100
}
}vim
virtual_server 192.168.133.100 80 {
delay_loop 2
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP後端
real_server 192.168.133.128 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.133.150 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}服務器
5.配置 VIP: master 配置一下就能夠
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.133.100
NETMASK=255.255.255.255
ONBOOT=yes
NAME=loopback
重啓網卡,啓動keepalived 顯示以下成功!
service network restart
service keepalived restart網絡
6.配置ipvsadm(配置master和backup)得先啓動keepalived
添加負載均衡器
/sbin/ipvsadm -A -t 192.168.133.100:80 -s rr
增長realserver
/sbin/ipvsadm -a -t 192.168.133.100:80 -r 192.168.133.128:80 -g #192.168.133.128 是真實機的ip
/sbin/ipvsadm -a -t 192.168.133.100:80 -r 192.168.133.150:80 -g #192.168.133.150 是真實機的ip
route add -host 192.168.133.100 dev lo:0
這些命令只能使用一次,第二次會有提示已經輸入該命令負載均衡
7.配置realserver (web1和web2)
配置vip 地址
/sbin/ifconfig lo 192.168.133.100 netmask 255.255.255.255 broadcast 192.168.133.100 up 啓用loopback
route add -host 192.168.133.100 dev lo 增長一條路由 指向端口和IP地址
8.在realserver 安裝測試httpd(只爲顯示結果)
yum install httpd –y
service httpd start
vim /var/www/html/index.html
this is 192.168.133.128/150
9.在master負載均衡器上查看
[root@master rc.d]# ipvsadm -L -n
IP Virtual Server version 1.2.0 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.133.100:80 rr persistent 50 注意這裏的rr persistent 50 其中rr 表明輪詢(round robin, rr); persistent 50:表示同一IP的鏈接60秒內被 分配到同一臺realserver
-> 192.168.133.150:80 Route 1 0 0 這裏realserver只有一臺,實際我設置的兩臺,可是配置權重weight的時候,不同,128的權重是100;150的權重是 1, 這裏選擇權重最小的150.
TCP 10.10.10.3:1358 rr persistent 50
TCP 10.10.10.2:1358 rr persistent 50
-> 192.168.200.200:1358 Masq 1 0 0
10.訪問:192.168.133.100 查看IE顯示狀態,正常應該顯示默認應該指向 192.168.133.150 主機的/var/www/html/index.php 若是關閉192.168.133.150的httpd服務,會指向192.168.133.128主機的/var/www/html/index.php出現一下表示安裝成功 刷新幾回。當master服務器down的時候,backup自動會接替服務,當master起來的時候,backup會自動斷掉。