[CentOS-es] Balanceo de carga con 2 ISP

ArcosCom Linux User linux en arcoscom.com
Dom Abr 27 18:29:19 UTC 2008


No he visitado el enlace que muestras, pero yo tenía el mismo problema que
tú hace tiempo.

Lo solucioné estableciento marcas para las interfaces, de forma que
agregué reglas para que la conexión se marcase en función del interfaz de
salida que se usase y que se enrutase hacia dicho interfaz de salida todo
lo que llevase dicha marca.

La idea es que un cliente hace una conexión msn y esta sale por wan0,
bien, en ese momento, si la conexión no tuviese marca pues establezco la
marca para la misma.

Al terminar de establecerse la conexión, sigue marcada, por lo tanto, una
regla en "ip route" que solo evalua la marca de la conexión, pues hará que
salga por wan0 o wanN (en función de la marca).

En la práctica, pues lo que tengo es esto:

=== REGLAS IPTABLES PARA EL ENRUTADO ===
Chain PREROUTING (policy ACCEPT 87M packets, 61G bytes)
num   pkts bytes target     prot opt in     out     source              
destination
1      18M   13G M_IFACE    all  --  *      *       0.0.0.0/0           
0.0.0.0/0
2     557K  335M M_TRAF_IN  all  --  *      *       0.0.0.0/0           
0.0.0.0/0
Chain M_IFACE (1 references)
num   pkts bytes target     prot opt in     out     source              
destination
1      18M   13G CONNMARK   all  --  *      *       0.0.0.0/0           
0.0.0.0/0           MARK match 0x0/0xf000 CONNMARK restore
2    9700K 8730M RETURN     all  --  *      *       0.0.0.0/0           
0.0.0.0/0           MARK match !0x0/0xf000
3    8558K 4457M M_IFACE_TRAF  all  --  *      *       0.0.0.0/0          
 0.0.0.0/0           MARK match 0x0/0xf000
4     138K 9370K MARK       all  --  eth3   *       0.0.0.0/0           
0.0.0.0/0           MARK match 0x0/0xf000 state NEW MARK or 0x8000
5     138K 9370K CONNMARK   all  --  *      *       0.0.0.0/0           
0.0.0.0/0           MARK match !0x0/0xf000 CONNMARK save
6    8558K 4457M RETURN     all  --  *      *       0.0.0.0/0           
0.0.0.0/0
Chain M_IFACE_TRAF (2 references)
num   pkts bytes target     prot opt in     out     source              
destination
Chain POSTROUTING (policy ACCEPT 93M packets, 65G bytes)
num   pkts bytes target     prot opt in     out     source              
destination
1      19M   14G M_IFACE_OUT  all  --  *      *       0.0.0.0/0           
0.0.0.0/0
2     614K  350M M_TRAF_OUT  all  --  *      *       0.0.0.0/0           
0.0.0.0/0
Chain M_IFACE_OUT (1 references)
num   pkts bytes target     prot opt in     out     source              
destination
1      15M 7096M CONNMARK   all  --  *      *       0.0.0.0/0           
0.0.0.0/0           MARK match 0x0/0xf000 CONNMARK restore
2    9534K 7306M RETURN     all  --  *      *       0.0.0.0/0           
0.0.0.0/0           MARK match !0x0/0xf000
3    9937K 6825M M_IFACE_TRAF  all  --  *      *       0.0.0.0/0          
 0.0.0.0/0           MARK match 0x0/0xf000
4     415K   24M MARK       all  --  *      eth3    0.0.0.0/0           
0.0.0.0/0           MARK match 0x0/0xf000 state NEW MARK or 0x8000
5     415K   24M CONNMARK   all  --  *      *       0.0.0.0/0           
0.0.0.0/0           MARK match !0x0/0xf000 CONNMARK save
6    9937K 6825M RETURN     all  --  *      *       0.0.0.0/0           
0.0.0.0/0
=== REGLAS DE ENRUTAMIENTO ===
0:      from all lookup local
50:     from all lookup main
100:    from all fwmark 0x8000/0xf000 lookup uno
150:    from 212.59.210.142/24 lookup uno
200:    from all lookup defecto
32766:  from all lookup main
32767:  from all lookup default
=== TABLAS DE RUTAS ===
=== MAIN ===
212.59.210.0/24 dev eth3  proto kernel  scope link  src 212.59.210.142
10.1.1.0/24 dev zlan0  proto kernel  scope link  src 10.1.1.6
169.254.0.0/16 dev zlan0  scope link
=== eth3 TABLA 150 ===
default via 212.59.210.1 dev eth3  proto static  src 212.59.210.142
prohibit default  proto static  metric 1
=== TABLA 200 (defecto) ===
default via 212.59.210.1 dev eth3  proto static

Bien, para conseguir esto, pues un script genérico (en el cuál configuro N
interfaces wan) genera los siguientes comandos:

/sbin/iptables -t mangle -N M_IFACE
/sbin/iptables -t mangle -N M_IFACE_TRAF
/sbin/iptables -t mangle -A M_IFACE -m mark --mark 0x0000/0xf000 -j
CONNMARK --restore-mark
/sbin/iptables -t mangle -A M_IFACE -m mark ! --mark 0x0000/0xf000 -j RETURN
/sbin/iptables -t mangle -A M_IFACE -m mark --mark 0x0000/0xf000 -j
M_IFACE_TRAF
/sbin/iptables -t mangle -N M_IFACE_OUT
/sbin/iptables -t mangle -A M_IFACE_OUT -m mark --mark 0x0000/0xf000 -j
CONNMARK --restore-mark
/sbin/iptables -t mangle -A M_IFACE_OUT -m mark ! --mark 0x0000/0xf000 -j
RETURN
/sbin/iptables -t mangle -A M_IFACE_OUT -m mark --mark 0x0000/0xf000 -j
M_IFACE_TRAF
/sbin/iptables -t mangle -A M_IFACE -m mark --mark 0x0000/0xf000 -i eth3
-m state --state NEW -j MARK --or-mark 0x8000
/sbin/iptables -t mangle -A M_IFACE_OUT -m mark --mark 0x0000/0xf000 -m
state --state NEW -o eth3 -j MARK --or-mark 0x8000
/sbin/iptables -t nat -A POSTROUTING -o eth3 -j MASQUERADE
/sbin/iptables -t mangle -A M_IFACE -m mark ! --mark 0x0000/0xf000 -j
CONNMARK --save-mark
/sbin/iptables -t mangle -A M_IFACE -j RETURN
/sbin/iptables -t mangle -I PREROUTING -j M_IFACE
/sbin/iptables -t mangle -A M_IFACE_OUT -m mark ! --mark 0x0000/0xf000 -j
CONNMARK --save-mark
/sbin/iptables -t mangle -A M_IFACE_OUT -j RETURN
/sbin/iptables -t mangle -I POSTROUTING -j M_IFACE_OUT
/sbin/ip rule add prio 50 table main
/sbin/ip rule add prio 100 fwmark 0x8000/0xf000 table 150
/sbin/ip rule add prio 150 from $MIWANIPNET0/$MIWANIPMASK0 table 150
/sbin/ip route add default via $MIGW0 dev eth3 src $MIWANIP0 proto static
table 150
/sbin/ip route append prohibit default table 150 metric 1 proto static
/sbin/ip rule add prio 200 table 200
/sbin/ip route add default table 200 proto static nexthop via $MIGW0 dev
eth3 weight 1
/sbin/ip route flush cache

Como puedes observar uso -j MARK --or-mark debido a que el tráfico también
lo marco para QoS y la marca debida a la selección de WAN no me pise la
marca debida al control de QoS, es por ello que te aparecen las máscaras
en el ip route y en el iptables.

Observa que el marcado lo hago cuando se establece la conexión, o sea,
usando -m state --state NEW, ya que es cuando hay que marcarlo para que
toda la conexión vaya siempre por el mismo GW, que es lo que dice la regla
del fwmark.

Por último indicarte que para poder usar máscaras con iproute, tuve que
compilarme la versión 2.6.20, ya que la que venía con CentOS no lo
permitía.

Bueno, ya nos comentarás cómo te ha ido.

Salu2

El Vie, 18 de Abril de 2008, 23:27, Gino Francisco Alania Hurtado escribió:
> A ver muchachos un apoyo con este tema :
>
> http://www.esdebian.org/staticpages/index.php?page=20040921021605992
>
> Ejecute casi todo este script para poder tener dos proveedores pasando por
> mi
> squid para que balance el acceso a mi red local de 1000 hosts
>
> El tema es que funciona perfecto excepto para el caso del msn , se
> desconecta
> continuamente cada 5 a 10 minutos.
> Alguna sugerencia , idea de como librarme de este inconveniente
>
> slds
>
> ----------------------------------
> Gino Alania Hurtado
> Nitcom Labs (http://www.nitcom.com)
>
> _______________________________________________
> CentOS-es mailing list
> CentOS-es en centos.org
> http://lists.centos.org/mailman/listinfo/centos-es
>




Más información sobre la lista de distribución CentOS-es