根据前一篇的lvs+heartbeat  做负载均衡(一),最后实现还是有那么一点缺陷,就是网关的问题,HA主机需要添加两个网关,当我的心跳转移后,需要改网关,所以存在一定的缺陷,虽然可以使用脚本解决这个问题,但是还是想考虑更好的方案,经过一个晚上的思考,我又重新建了一个VIP用来当内网网关。

如图:

lvs+heartbeat  做负载均衡(二)_HAzai

/etc/ha.d/haresources中添加一条记录:

Master.example.com 172.24.55.123  (两台LVS配置一样)

HA端添加一条路由记录:

[root@localhost ~]# route add default gw 172.24.55.123

测试:

启动heartbeat

在LVS(master端)

在测试之前在HA上添加路由。

查看IP

[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:8e:cc:05 brd ff:ff:ff:ff:ff:ff
    inet 172.24.55.1/16 brd 172.24.255.255 scope global eth0
    inet 172.24.55.123/16 brd 172.24.255.255 scope global secondary eth0:0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:50:12:12:12:13 brd ff:ff:ff:ff:ff:ff
    inet 1.1.1.2/24 brd 1.1.1.255 scope global eth1
    inet 1.1.1.254/24 brd 1.1.1.255 scope global secondary eth1:0

在HA端添加路由:

[root@localhost ~]# route add default gw 172.24.55.123
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.24.0.0      *               255.255.0.0     U     0      0        0 br0
169.254.0.0     *               255.255.0.0     U     0      0        0 br0
default         172.24.55.123   0.0.0.0         UG    0      0        0 br0

这个网关就是我的内网的VIP.lvs+heartbeat  做负载均衡(二)_heartbeat_02

lvs+heartbeat  做负载均衡(二)_lvs_03

测试LVS(slave端):

查看IP:

[root@slave10 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:40:4c:8c brd ff:ff:ff:ff:ff:ff
    inet 172.24.55.10/16 brd 172.24.255.255 scope global eth0
    inet 172.24.55.123/16 brd 172.24.255.255 scope global secondary eth0:0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:50:12:12:12:12 brd ff:ff:ff:ff:ff:ff
    inet 1.1.1.1/24 brd 1.1.1.255 scope global eth1
    inet 1.1.1.254/24 brd 1.1.1.255 scope global secondary eth1:0

可以看到VIP跳过来。

查看lvs输入输出流状态;

[root@slave10 ~]# ipvsadm -ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  1.1.1.254:80                        3       26       14     4933     5649
  -> 172.24.155.154:80                   3        26       14     4933     5649

可以看到输入输出都有,实验成功!

通过这次实验,让我学到了在做项目的同时思路要严谨,同时要开放自己的思想,要多思考尽量选择最好的方法来实现高性能、稳定的架构环境。