vyatta多ip负载均衡


这种方法主要用于下面的情形:nat后的机器通过多个公网ip出去,动态分配选择。在网上多是考虑到有多个isp的情况下的负载均衡及容错,本文使用一个网段使用多个ip地址进行均衡。
因为这些机器上运行的是网页爬虫程序,出于容量和安全考虑,采用了虚拟机系统,并为其分配了一段私有ip地址段,同时将它们的网关指向vyatta软路由。
由于爬虫自身的原因,对部分站点的抓取过于频繁,这样出口的公网ip就容易被所访问的站点封锁;经过多次实验,一般是短时间(数分钟或一小时之内)的阻止,阻止是针对单个ip地址进行,一般不会阻止地址段且为系统自动处理。
目前idc机房给了多个ip地址,并且是连续的,但要将把这些公网ip地址绑到虚拟机上不现实也不太可能(ip地址不够,且不能解决还是一个公网ip的现实)。所以让这些爬虫虚拟机动态的在一个时间段里选择一个公网ip地址,可以基本上解决上面的问题,而且在idc看来,多个ip分摊了连接和流量,不容易引起防火墙的误判。
下面是配置的过程:
注意:在配置过程中犯发一个错误,只是在nat配置中指定了可以使用的ip段(参考文档中是这样配置的),在实际中却不行。仔细一想:这些ip地址必须寄生到路由器上才行,所以必须将这些地址一个个地配到软路由上,这样配置才能生效。
eth0:公网地址
eth1:内网段1(该idc机房的所有内网网关)
eth2:内网段2(windows爬虫专用的网段网关)
# show interfaces ethernet eth0
address 217.68.208.218/26
address 217.68.208.222/26
address 217.68.208.223/26
address 217.68.208.224/26
address 217.68.208.225/26
duplex auto
hw-id 08:00:27:e2:12:88
smp_affinity auto
speed auto
[edit]
上面是eth0网口所配置的公网ip地址,这一步必须完成,不然后面能做但达不到预期,操作指令如下:
set interfaces ethernet eth0 address ip/mask
接下来将设置这种NAT:
# show nat source rule
...
rule 20 {
outbound-interface eth0
source {
address 192.168.30.0/24
}
translation {
address 217.68.208.222-217.68.208.225
}
}
[edit]
配置过程如下:
vyatta@shzjrouter# delete nat source rule 10
[edit]
vyatta@shzjrouter# set nat source rule 10
[edit]
vyatta@shzjrouter# set nat source rule 10 source address 192.168.30.0/24
[edit]
vyatta@shzjrouter# set nat source rule 10 outbound-interface eth0
[edit]
vyatta@shzjrouter# set nat source rule 10 translation address 217.68.208.222-217.68.208.225
[edit]
vyatta@shzjrouter# commit
[edit]
完成后,可以另开ssh窗口,切换到root用户下(sudo -i),使用'iftop -n -i eth0'就可以看到那一段的公网ip流量的情况了,基本上能均衡使用。
但也有一些情况下,如不间断的连接访问,vyatta就会维持所使用的公网ip与内网ip之间的映射关系。
参考文档:
Vyatta-NAT_6.5R1_v01.pdf
Source NAT (Many‐to‐Many)
In many-to-many translations, a number of private addresses are mapped to a number of public addresses. This provides a way of reducing the possibility of port exhaustions that is possible in a many-to-one scenario. For this reason, it can provide more capacity for outbound translations. Figure 2-3 shows a large private address space (a /8 network prefix, here represented as three /16 subnets) mapped to a small range of external addresses.

因为这些机器上运行的是网页爬虫程序,出于容量和安全考虑,采用了虚拟机系统,并为其分配了一段私有ip地址段,同时将它们的网关指向vyatta软路由。
由于爬虫自身的原因,对部分站点的抓取过于频繁,这样出口的公网ip就容易被所访问的站点封锁;经过多次实验,一般是短时间(数分钟或一小时之内)的阻止,阻止是针对单个ip地址进行,一般不会阻止地址段且为系统自动处理。
目前idc机房给了多个ip地址,并且是连续的,但要将把这些公网ip地址绑到虚拟机上不现实也不太可能(ip地址不够,且不能解决还是一个公网ip的现实)。所以让这些爬虫虚拟机动态的在一个时间段里选择一个公网ip地址,可以基本上解决上面的问题,而且在idc看来,多个ip分摊了连接和流量,不容易引起防火墙的误判。
下面是配置的过程:
注意:在配置过程中犯发一个错误,只是在nat配置中指定了可以使用的ip段(参考文档中是这样配置的),在实际中却不行。仔细一想:这些ip地址必须寄生到路由器上才行,所以必须将这些地址一个个地配到软路由上,这样配置才能生效。
eth0:公网地址
eth1:内网段1(该idc机房的所有内网网关)
eth2:内网段2(windows爬虫专用的网段网关)
# show interfaces ethernet eth0
address 217.68.208.218/26
address 217.68.208.222/26
address 217.68.208.223/26
address 217.68.208.224/26
address 217.68.208.225/26
duplex auto
hw-id 08:00:27:e2:12:88
smp_affinity auto
speed auto
[edit]
上面是eth0网口所配置的公网ip地址,这一步必须完成,不然后面能做但达不到预期,操作指令如下:
set interfaces ethernet eth0 address ip/mask
接下来将设置这种NAT:
# show nat source rule
...
rule 20 {
outbound-interface eth0
source {
address 192.168.30.0/24
}
translation {
address 217.68.208.222-217.68.208.225
}
}
[edit]
配置过程如下:
vyatta@shzjrouter# delete nat source rule 10
[edit]
vyatta@shzjrouter# set nat source rule 10
[edit]
vyatta@shzjrouter# set nat source rule 10 source address 192.168.30.0/24
[edit]
vyatta@shzjrouter# set nat source rule 10 outbound-interface eth0
[edit]
vyatta@shzjrouter# set nat source rule 10 translation address 217.68.208.222-217.68.208.225
[edit]
vyatta@shzjrouter# commit
[edit]
完成后,可以另开ssh窗口,切换到root用户下(sudo -i),使用'iftop -n -i eth0'就可以看到那一段的公网ip流量的情况了,基本上能均衡使用。
但也有一些情况下,如不间断的连接访问,vyatta就会维持所使用的公网ip与内网ip之间的映射关系。
参考文档:
Vyatta-NAT_6.5R1_v01.pdf
Source NAT (Many‐to‐Many)
In many-to-many translations, a number of private addresses are mapped to a number of public addresses. This provides a way of reducing the possibility of port exhaustions that is possible in a many-to-one scenario. For this reason, it can provide more capacity for outbound translations. Figure 2-3 shows a large private address space (a /8 network prefix, here represented as three /16 subnets) mapped to a small range of external addresses.
