Balanceo de Carga con dos ADSL y Debian Lenny
Hola, quién halla llegado a este artículo ya sabe lo que quiere, entonces no me tomaré el tiempo de explicar de que se trata el balanceo de carga de conexiones a internet.
Para nuestro caso contamos con lo siguiente:
Debian Lenny AMD64 (igual para i386)
Kernel 2.6.26-2-1-amd64
Tres tarjetas de red Ethernet
Dos conexiones ADSL
Switch o AP inalámbrico
Cerebro inquieto y paciente
Café, mucho café.
Bien, lo primero es describir como está la red y las conexiones, para ello tenemos el siguiente dibujito:
Lo único que hay que tener en cuenta para que esta configuración funcione es quitarle los gateway a todas las interfaces de red, quedando entonces su /etc/network/interfaces sin líneas que digan gateway. En el sentido mas estricto tendríamos que limpiar la tabla main y esas cosas pero en fin, no es necesario y se pueden obviar algunos mensajes de error.
Este es mi /etc/network/interfaces:
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5).
# The loopback network interface auto lo
iface lo inet loopback address 127.0.0.1 netmask 255.0.0.0
# The primary network interface # LAN
iface eth0 inet static address 192.168.1.1 netmask 255.255.255.0 auto eth0
# ADSL1
allow-hotplug eth1 iface eth1 inet static address 10.1.0.2 netmask 255.255.255.0 network 10.1.0.0 broadcast 10.1.0.255 #gateway 10.1.0.1 dns-nameservers 10.1.0.1 10.1.1.1 auto eth1 # ADSL2
iface eth2 inet static address 10.1.1.2 netmask 255.255.255.0 network 10.1.1.0 broadcast 10.1.1.255 #gateway 10.1.1.1 dns-nameservers 10.1.1.1 10.1.0.1 auto eth2
A continuación dos scripts usados suficientemente comentados, incluyendo el NAT para la red local (compartir internet) y redireccionamiento para proxy transparente (pueden quitarlo si prefieren), estos scripts deben ponerse a correr desde /etc/rc.local o como prefieran, primero el de rutas y luego el de NAT. Antes de ejecutar los scripts debemos agregar las tablas de rutas al archivo de rutas, esto se hace una sola vez:
echo 200 T1 >> /etc/iproute2/rt_tables echo 201 T2 >> /etc/iproute2/rt_tables echo 202 lento >> /etc/iproute2/rt_tables echo 203 rapido >> /etc/iproute2/rt_tables #!/bin/bash
IF0="eth0" # Interface conectada a la LAN IF1="eth1" # Interface conectada a ADSL1 Lenta IF2="eth2" # Interface conectada a ADSL2 Rápida IP0="192.168.1.1" # Gateway Local, IP Local IP1="10.1.0.2" # IP de la IF1
IP2="10.1.1.2" # IP de la IF2 P0_NET="192.168.1.0/24" # Red Local P1_NET="10.1.0.0/24" # Red para IF1 P2_NET="10.1.1.0/24" # Red para IF2 P1="10.1.0.1" # Gateway para ADSL1 P2="10.1.1.1" # Gateway para ADSL2
# Creo las tablas de rutas para las dos interfaces uplink echo "Creando las rutas para cada tabla"
ip route add ${P1_NET} dev ${IF1} src ${IP1} table T1 ip route add default via ${P1} table T1
ip route add ${P2_NET} dev ${IF2} src ${IP2} table T2 ip route add default via ${P2} table T2
echo "Hecho."
# Aseguro que cada red enviará sus solicitudes a la ip correcta echo "Asegurando la ruta para cada interface e IP"
ip route add ${P1_NET} dev ${IF1} src ${IP1} ip route add ${P2_NET} dev ${IF2} src ${IP2} echo "Hecho."
echo "Creando reglas para cada tabla" ip rule add from ${IP1} table T1 ip rule add from ${IP2} table T2 echo "Hecho."
# Hago el balanceo 1 a 2 echo "Haciendo el balanceo"
ip route add default scope global nexthop via ${P1} dev ${IF1} weight 1 nexthop via ${P2} dev ${IF2} weight 2
echo "Hecho."
# Agrego dos tablas de rutas estáticas para hacer marcado de paquetes sobre ellas echo "Creando tablas para marcado de paquetes ..."
# Enlace lento
ip rule add fwmark 1 table lento
ip route add table lento via ${P1} dev eth1
# Enlace rápido
ip rule add fwmark 2 table rapido
ip route add table rapido via ${P2} dev eth2
echo "Refrescando las rutas" ip route flush cache
echo "Hecho."
Y bien, noten que el balanceo se hizo de 1 a 2 para que la conexión rápida trabaje el doble de lo que trabaja la lenta, esto depende en si de lo bueno que sea cada isp y del uso que quieran hacer de cada conexión. Noten que se usa fwmark para marcar cada ruta y así poder usar esta marca en el marcado de paquetes. Existen otras técnicas mucho mas pulidas y con mejores resultados que esta, por ejemplo parchando el kernel para rutas dinámicas y cuanta cosa los gurús saben, pero a mi me va funcionando de 10 esta técnica. Así mismo se pueden usar marcas sobre las tablas T1 y T2, pero aquí están separadas por simple claridad.
Si todo está bien tendríamos las siguientes salidas:
edoras:~# ip route
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.1 10.1.0.0/24 dev eth1 proto kernel scope link src 10.1.0.2 10.1.1.0/24 dev eth2 proto kernel scope link src 10.1.1.2 default
nexthop via 10.1.0.1 dev eth1 weight 1 nexthop via 10.1.1.1 dev eth2 weight 2 edoras:~# ip route list table T1
10.1.0.0/24 dev eth1 scope link src 10.1.0.2 default via 10.1.0.1 dev eth1
edoras:~# ip route list table T2
10.1.1.0/24 dev eth2 scope link src 10.1.1.2 default via 10.1.1.1 dev eth2
edoras:~# ip route show table lento default via 10.1.0.1 dev eth1
edoras:~# ip route show table rapido default via 10.1.0.1 dev eth1
Con esto ya tenemos nuestras rutas y balanceo, en este punto el equipo que hace de router en el que estamos aplicando estas reglas debería navegar sin problemas. Pero nos falta el NAT para la red local y es aquí donde entra el otro script, noten como se usan las marcas:
echo "Limpiando Reglas Anteriores..." iptables -F iptables -X iptables -Z iptables -t nat -F iptables -t mangle -F
IF0="eth0" # Interface conectada a la LAN IF1="eth1" # Interface conectada a ADSL1 Lenta IF2="eth2" # Interface conectada a ADSL2 Rápida IP0="192.168.1.1" # Gateway Local, IP Local IP1="10.1.0.2" # IP de la IF1
IP2="10.1.1.2" # IP de la IF2 P0_NET="192.168.1.0/24" # Red Local P1_NET="10.1.0.0/24" # Red para IF1 P2_NET="10.1.1.0/24" # Red para IF2 P1="10.1.0.1" # Gateway para ADSL1 P2="10.1.1.1" # Gateway para ADSL2
# Ahora hago el NAT
echo "Activando NAT ..."
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s ${P0_NET} -o ${IF1} -j MASQUERADE iptables -t nat -A POSTROUTING -s ${P0_NET} -o ${IF2} -j MASQUERADE
# Redirecciono al Proxy
iptables -t nat -A PREROUTING -i ${IF0} -p tcp --dport http -j DNAT --to ${IP0}":8080"
iptables -t nat -A PREROUTING -i ${IF0} -p tcp --dport 81 -j DNAT --to ${IP0}":8080"
iptables -t nat -A PREROUTING -i ${IF0} -p tcp --dport 8080:8099 -j DNAT --to ${IP0}":8080"
iptables -t nat -A PREROUTING -i ${IF0} -p tcp --dport 3128:3130 -j DNAT --to ${IP0}":8080"
# Hago marcado de paquetes para asegurar que los paquetes de estos puertos encontrarán el camino de vuelta
echo "Realizando marcado de paquetes ..."
iptables -t mangle -A PREROUTING -i ${IF0} -s ${P0_NET} -p tcp --dport 1863 -m state --state NEW,ESTABLISHED,RELATED -j MARK --set-mark 2
iptables -t mangle -A PREROUTING -i ${IF0} -s ${P0_NET} -p tcp --dport 6667 -m state --state NEW,ESTABLISHED,RELATED -j MARK --set-mark 2
echo "Listo."
echo "Reglas Aplicadas" iptables -L -n -v
iptables -t nat -L -n -v iptables -t mangle -L -n -v
El buen observador se habrá dado cuenta que con la tabla mangle se marcan los paquetes que pertenecen a determinados protocolos y que esto le da la posibilidad de definir que servicios quiere a través de que enlace, por ejemplo se podría pensar en mandar el molesto P2P a través del enlace lento (tarea para el lector).
Creo que eso es todo, recuerden que a la primera no funciona, que es bueno que lean algo de LARTC para que aprendan sobre rutas y control de tráfico en Linux y que siempre habrá una forma mas elegante, eficiente y simple de hacer las cosas. También recuerden que pueden añadir un servidor dns local, un proxy
transparente y todo lo que quieran a esta configuración.
probar.
Aquí un interesante artículo sobre balanceo de carga manual (Definir varios Gateway), puede servir también de acuerdo a la necesidad.
Autor: Odair Trujillo
Comentarios (5)
RSS Comentarios
conexion con 2 adsl
escrito por estuardo gomez , diciembre 22, 2010
Hola buen dia, queria ver si me puedes ayudar, realice los pasos correspondientes para el balanceo de carga, copiando exactamente los pasos a seguir cambiando unicamente los datos de mi red. Al probar este tengo ping desde la pc cliente y puedo entrar al msn pero no logra cargar ninguna pagina web me da un error como que no tuviera los dns estos estan escritos en el cliente pero aun asi me da el mismo problema, espero me puedas ayudar.
Reporte el abuso Voto negativo Voto positivo Votos: +0
Que ocurre exactamente?
escrito por K-nábora Bufete Tecnológico , febrero 12, 2010 Hola Sergio
Que ocurre exactamente? No puedes navegar a traves del proxy? El proxy se ejecuta en el mismo firewall que hace el balanceo de carga?
Si nos das mas datos a lo mejor te podemos ayudar Un saludo
Reporte el abuso Voto negativo Voto positivo Votos: +0
problema con proxy
escrito por sergio morales , febrero 10, 2010
-buenas disculpa hice esta configuracion con 2adsl y el proxy no filtra la web no se que pueda estar haciendo mal habra alguna forma de saber que es lo que ocurre
Reporte el abuso Voto negativo Voto positivo Votos: +0 gracias
Hola Vicente
Gracias por tu aportación con ese enlace. La verdad es que, al contrario que con otro tipo de sistemas o soluciones basadas en Linux, existe muy poca información acerca de como aprovechar y exprimir al máximo las increibles funcionalidades de routing avanzado, y gestión QoS que nos ofrece el nucleo de Linux a través de la "suite" iproute2
How to load balance 4 Modem Cable
internet access
by Damia Soler
v1.0, Oct 2005
Thanks to LuisMi (Benagua organizator) and Nacho (Work Colegue)
Introduction
Every year I assist to Benaguasil party and colabotate with the organization on technical
issues, mainly Internet access, each year we have a different scenario, the sponsors one year
give us an ADSL, other year a Cable modem, even a wifi access. We always have a Linux
server that deals with the connection and give access to the LAN-party doing NAT,or
proxy.
This year (2005) we had four Cable Modems each one with 4MB download rate. The exact
equipment were four cable modems with one Ethernet port with DHCP assignement (with
no configuration options and no NAT option only single mode). We have a Linux server
with five ethernet adapters.
And we want to give this download thougput (4Mb x 4) to the LAN party.
Aproaches
We made a brainstorming thinking about how to use all four connections, some ideas, only
one of them developed (Are all other posibles?).
-We thought to use a comercial solution, like linkproof from radware, probably we could
obtain one borrowed, but we have no clear experience about that product with four
ethernets with dhcp on each port. But more important we want to make it work with free
software :-) only with the Linux server.
-SQUID: A network of squid caches, each one connected to a different Cable modem. Is
posible to one squid on a multihomed host to balance connections? All questions unknow.
-To distribute the hosts on LAN, giving each group of hosts a different default gateway.
The problem was that we need four host to NAT, due that Modem Cable don't allow NAT,
single IP for each one. We had only one PC, to do everything, with for Pcs, or NAT capable
routers this aproach could be achieved, but we didn't try due to lack of PCs.
-To statically NAT and route routing each group of client host for a specific gateway. This
is very similar to the case before, but done with only on Pc, with source routing and NAT. I
thought it is posible to do, buy we didn't make it works.
-To statically NAT and route routing each group of protocols for a specific gateway. This is
basically the same, but making policiy routing works giving each protocol a routing
gateway thought different cable modem, this solutions have an special characteristic, you
could assign web trafic a Cable modem, P2P other Cable modem, having some QOS a
service control.
-To dynamicaly balance route and NAT. Better that do assignments of clients or protocols,
we try this aproach that balance dinamically each line. This is what we are going to explain
how we did it :-)
Choosing distribution
I am a Debian fan, and then don't try to install any other distribution. Then we install
Debian sarge, however this tutorial have no very specific distribution issues, and I think it
could work on any distribution with iptables toools, etc..
The setup
We have five ethernet interfaces, one of them is the internal LAN interface with a static IP,
all others have dhcp ip assigned by the cable modem Operator Ono, our
# The loopback interface
# automatically added when upgrading auto lo
iface lo inet loopback
# automatically added when upgrading auto eth0
iface eth0 inet static
address 192.168.1.1 netmask 255.255.0.0 ##
auto eth1
iface eth1 inet dhcp ##
auto eth2
iface eth2 inet dhcp ##
auto eth3
iface eth3 inet dhcp ##
auto eth4
iface eth4 inet dhcp
Restating networking we had the ip assignement:
Interface IP
Netmask
default_gateway
eth0
192.168.1.1
255.255.0.0
eth1
81.203.149.126 255.255.224.0 81.203.144.1
eth2
81.203.144.81 255.255.224.0 81.203.144.1
eth3
81.203.128.69 255.255.224.0 81.203.128.1
eth4
81.203.144.88 255.255.224.0 81.203.144.1
To see the default route of each interface we use netstat -nrv and we saw that we have two
diffentet default gateways.
Then we create two scripts, that makes the line balancer works…
route del defaultroute del default route del default route del default
ip route add default equalize scope global \
nexthop via 81.203.144.1 dev eth1 weight 1 \ nexthop via 81.203.144.1 dev eth2 weight 1 \ nexthop via 81.203.128.1 dev eth3 weight 1 \ nexthop via 81.203.144.1 dev eth4 weight 1
IPTABLES=/sbin/iptables
$IPTABLES -t nat -A POSTROUTING -o $EXTIF1 -j SNAT --to-source 81.203.149.126
$IPTABLES -t nat -A POSTROUTING -o $EXTIF2 -j SNAT --to-source 81.203.144.81
$IPTABLES -t nat -A POSTROUTING -o $EXTIF3 -j SNAT --to-source 81.203.128.69
$IPTABLES -t nat -A POSTROUTING -o $EXTIF4 -j SNAT --to-source 81.203.144.88
To see thing woking, one useful tool is iptraf :-)
Problems and TODO
How can we see the default gateway assignement on each interface, how can we use it
autoamtically on our scripts.
How the balance is performed? Could be tuned or customized?
Links
At the moment:
Benaguasil party