使用5台虚拟机 1台作为Linux路由器
2台作为LVS调度器 1台作为Real Server 物理机作为客户端
一、配置网络环境
真实Web服务器地址1、192.168.4.52
真实Web服务器地址2、192.168.4.53
服务器VIP地址设置为 192.168.4.252
1、配置Realserver >52
[root@web52 html]# ifconfig lo:1 192.168.4.252/32
[root@web52 conf]#cd /proc/sys/net/ipv4/conf
[root@web52 conf]# echo 1 > lo/arp_ignore
[root@web52 conf]# echo 2 > lo/arp_announce
[root@web52 conf]# echo 1 > all/arp_ignore
[root@web52 conf]# echo 2 > all/arp_announce
>53
[root@web53 html]# ifconfig lo:1 192.168.4.252/32
[root@web53 html]# cd /proc/sys/net/ipv4/conf/
[root@web53 conf]# echo 1 > lo/arp_ignore
[root@web53 conf]# echo 2 > lo/arp_announce
[root@web53 conf]# echo 1 > all/arp_ignore
[root@web53 conf]# echo 2 > all/arp_announce
2、配置调度器
LVS1调度器真实IP地址为192.168.4.50
LVS2调度器真实IP地址为192.168.4.51
安装ipvsadm、 keepalived
[root@pc50 ~]# yum -y install ipvsadm
[root@pc51 ~]# yum -y install ipvsadm
[root@pc50 ~]# yum -y install keepalived
[root@pc51 ~]# yum -y install keepalived
3、编写网页文件(52,53)
[root@web52 ~]#yum -y install httpd
[root@web53 ~]#yum -y install httpd
[root@web52 ~]#echo 192.168.4.52 > /var/www/html/test.html
[root@web53 ~]#echo 192.168.4.53 > /var/www/html/test.html
二、 修改配置文件
[root@pc50 ~]# vim /etc/keepalived/keepalived.conf
15 vrrp_instance VI_1 {
16 state MASTER 主服务器为MASTER
17 interface eth0
18 virtual_router_id 51
19 priority 150 优先级
20 advert_int 1
21 authentication {
22 auth_type PASS
23 auth_pass 1111 主辅服务器密码一致
24 }
25 virtual_ipaddress { 虚拟IP
26 192.168.4.252
27 }
28 }
29
30 virtual_server 192.168.4.252 80 { 设置VIP为192.168.4.252
31 delay_loop 6
32 lb_algo rr 设置LVS的调度算法为RR
33 lb_kind DR 设置LVS的模式为DR
34 nat_mask 255.255.255.0
35 persistence_timeout 50
36 protocol TCP
37 connect_timeout 3
38 nb_get_retry 3
39 delay_before_retry 3
40 real_server 192.168.4.52 80 {
41 weight 1 设置权重为1
42 }
43 real_server 192.168.4.53. 80 {
44 weight 2 设置权重为2
45 }
46 }
2.1 LVS2调度器设置大致一样,修改以下两样
[root@pc50 ~]# scp /etc/keepalived/keepalived.conf 192.168.4.51:/etc/keepalived/
[root@pc51 ~]# vim /etc/keepalived/keepalived.conf
16 state DACKUP从服务器
19 priority 100 优先级
三、测试
[root@pc50 ~]# service keepalived start 开启服务
[root@pc50 ~]# ip addr show | grep 192.168.4.*
inet 192.168.4.50/24 brd 192.168.4.255 scope global eth0
inet 192.168.4.252/32 scope global eth0 虚拟ip地址
[root@pc50 ~]# ipvsadm -Ln 查看信息
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.4.252:80 rr persistent 50
-> 192.168.4.52:80 Route 1 0 0
-> 192.168.4.53:80 Route 1 0 0
3.1 访问
[root@stu4 ~]# elinks -dump http://192.168.4.252/test.html
192.168.4.52
[root@stu4 ~]# elinks -dump http://192.168.4.252/test.html
192.168.4.53
[root@pc50 ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 192.168.4.252:80 2 10 0 848 0
-> 192.168.4.52:80 1 5 0 424 0
-> 192.168.4.53:80 1 5 0 424 0
【把50的服务关闭】
[root@pc50 ~]# service keepalived stop
[root@stu4 ~]# elinks -dump http://192.168.4.252/test.html
192.168.4.52
[root@stu4 ~]# elinks -dump http://192.168.4.252/test.html
192.168.4.53
【VIP会在51上面】
[root@pc51 ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 192.168.4.252:80 2 10 0 848 0
-> 192.168.4.52:80 1 5 0 424 0
-> 192.168.4.53:80 1 5 0 424 0
原文:http://blog.51cto.com/13560729/2063904