1 准备三台机器
分发器(也叫调度器,简写为dir)192.168.189.128
rs1 192.168.189.129
rs2 192.168.189.130
每台机器上(包括dir,所有rs)都设置vip 192.168.189.200
2 dir上编写脚本
#vim /usr/local/sbin/lvs_dr.sh #! /bin/bash echo 1 > /proc/sys/net/ipv4/ip_forward ipv=/usr/sbin/ipvsadm vip=192.168.189.200 rs1=192.168.189.129 rs2=192.168.189.130 #注意这里的网卡名字 ifdown ens33 ifup ens33 ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip dev ens33:2 $ipv -C $ipv -A -t $vip:80 -s wrr $ipv -a -t $vip:80 -r $rs1:80 -g -w 1 $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
参数解释:
echo 1 > /proc/sys/net/ipv4/ip_forward 打开端口转发
ipv=/usr/sbin/ipvsadm
vip=192.168.189.200
rs1=192.168.189.129
rs2=192.168.189.130
设置各参数的变量,变量代替ip
ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
ifconfig设置一个网卡的虚拟网卡
绑定vip的命令配置,ens33:2
route add -host $vip dev ens33:2
设置网关
3 dir上启动脚本
[root@centos7-01 ~]# sh !$
sh /usr/local/sbin/lvs_dr.sh
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/17)
4 编写rs上的脚本
两台rs上也编写脚本 vim /usr/local/sbin/lvs_rs.sh//内容如下
注意rs1上的脚本名是lvs_rs1.sh,rs2上的脚本名是lvs_rs2.sh
rs1上,
[root@centos7-02 ~]# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.189.200
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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
rs2上,
[root@centos7-03 ~]# vim /usr/local/sbin/lvs_rs2.sh
#/bin/bash
vip=192.168.189.200
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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
参数解释:
以下是更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
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
5 检查vip是否绑定了在rs1,rs2,dir上
dir,
[root@centos7-01 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:15:53:53 brd ff:ff:ff:ff:ff:ff
inet 192.168.189.128/24 brd 192.168.189.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.189.200/32 brd 192.168.189.200 scope global ens33:2
valid_lft forever preferred_lft forever
inet 192.168.189.150/24 brd 192.168.189.255 scope global secondary ens33:0
valid_lft forever preferred_lft forever
inet6 fe80::243c:86d7:d85e:224d/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:15:53:5d brd ff:ff:ff:ff:ff:ff
inet 192.168.149.128/24 brd 192.168.149.255 scope global dynamic ens37
valid_lft 1464sec preferred_lft 1464sec
inet 192.168.149.147/24 brd 192.168.149.255 scope global secondary ens37
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe15:535d/64 scope link
valid_lft forever preferred_lft forever
rs1,
[root@centos7-02 sbin]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 192.168.189.200/32 brd 192.168.189.200 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:73:7c:4c brd ff:ff:ff:ff:ff:ff
inet 192.168.189.129/24 brd 192.168.189.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::165b:9d77:48be:464c/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::b485:96d0:c537:251e/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:73:7c:56 brd ff:ff:ff:ff:ff:ff
inet6 fe80::db6f:ae30:c2f3:81c5/64 scope link
valid_lft forever preferred_lft forever
rs2,
[root@centos7-03 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 192.168.189.200/32 brd 192.168.189.200 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:46:b2:9c brd ff:ff:ff:ff:ff:ff
inet 192.168.189.130/24 brd 192.168.189.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::b485:96d0:c537:251e/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:46:b2:a6 brd ff:ff:ff:ff:ff:ff
6 准备工作完成,进行访问vip测试。
在windows浏览器上进行访问测试
由于windows有缓存的问题存在,在实验上可能会影响实验结果,所以只能尝试在不同浏览器上进行访问测试
6.1 在google chrome浏览器上访问vip
截图
转到rs1上
6.2 在360浏览器上访问vip
截图
转到rs2上
上面也实现了负载均衡的作用。
原文地址:http://blog.51cto.com/13578154/2119567