LVS-DR 模型实现WEB负载均衡

Posted by Mathew on 2017-02-11

LVS-DR 模型实现WEB负载均衡

主机规划

主机 IP地址 角色 系统
Director 192.168.1.9,192.168.1.10 lvs-director CentOS 6.8
Rs 1 192.168.1.21,192.168.1.10 lvs-rs CentOS 7.2
Rs 2 192.168.1.22,192.168.1.10 lvs-rs CentOS 7.2

实验拓扑

Director

1
2
[root@Director ~]# ifconfig eth0:0 192.168.1.10/32 broadcast 192.168.1.10 up 
[root@Director ~]# route add -host 192.168.1.10 dev eth0:0

RS1

1
2
3
4
5
6
7
8
9
10
11
12
13
## 修改内核参数,若要永久生效请修改配置文件
[root@rs1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@rs1 ~]# echo 1 > /proc/sys/net/ipv4/conf/eno16777736/arp_ignore
[root@rs1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@rs1 ~]# echo 2 > /proc/sys/net/ipv4/conf/eno16777736/arp_announce
## 配置VIP到lo:0
[root@rs1 ~]# ifconfig lo:0 192.168.1.10/32 broadcast 192.168.1.10 up
[root@rs1 ~]# route add -host 192.168.1.10 dev lo:0
[root@rs1 ~]# yum install httpd -y &> /dev/null && echo success || echo failure #RS2安装httpd
success #安装成功
[root@rs1 ~]# echo "Test Page On RS1.maricle.com.cn</h1>" > /var/www/html/index.html #添加网页
[root@rs1 ~]# systemctl start httpd.service &> /dev/null && echo success #启动httpd服务
success #启动成功

RS2

1
2
3
4
5
6
7
8
9
10
11
12
13
## 配置同rs1
[root@rs2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@rs2 ~]# echo 1 > /proc/sys/net/ipv4/conf/eno16777736/arp_ignore
[root@rs2 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@rs2 ~]# echo 2 > /proc/sys/net/ipv4/conf/eno16777736/arp_announce

[root@rs2 ~]# ifconfig lo:0 192.168.1.10/32 broadcast 192.168.1.10 up
[root@rs2 ~]# route add -host 192.168.1.10 dev lo:0
[root@rs2 ~]# yum install httpd -y &> /dev/null && echo success || echo failure #RS2安装httpd
success #安装成功
[root@rs2 ~]# echo "Test Page On RS2.maricle.com.cn</h1>" > /var/www/html/index.html #添加网页
[root@rs2 ~]# systemctl start httpd.service &> /dev/null && echo success #启动httpd服务
success #启动成功

Direcotr添加ipvsadm规则

1
2
3
4
5
6
7
[root@Director ~]# curl http://192.168.1.21
<h1>Test Page On RS1.maricle.com.cn</h1>
[root@Director ~]# curl http://192.168.1.22
<h1>Test Page On RS2.maricle.com.cn</h1>
[root@Director ~]# ipvsadm -A -t 192.168.1.10:80 -s rr
[root@Director ~]# ipvsadm -a -t 192.168.1.10:80 -r 192.168.1.21 -g
[root@Director ~]# ipvsadm -a -t 192.168.1.10:80 -r 192.168.1.22 -g

测试