Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Load Balancing using iptables
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
ddaas
Tux's lil' helper
Tux's lil' helper


Joined: 28 Feb 2005
Posts: 106
Location: Germany

PostPosted: Mon Oct 01, 2007 10:03 am    Post subject: Load Balancing using iptables Reply with quote

Hi there,
I have 2 Internet connections and I want to implement the following on my linux router/firewall. It has 3 inferfaces (1xlan & 2xwan)

1. Load balancing some sort of traffic (ex http)
Web Traffic comming from the lan should balances in a round-robin fashion across wans connections.(per destination load balancing)

2. Some sort of traffic should always use one of the two connections.
Ex: icmp and tcp/ssh should always use wan1 and tcp/smtp,pop,imap should always use wan2.

O thought of something like this:
Code:
#!/bin/bash

iptables -t nat -A POSTROUTING -o eth1 -p tcp --dport 22 -s 10.0.0.0/24 -j SNAT --to-source 190.17.0.x
iptables -t nat -A POSTROUTING -o eth1 -p icmp  -s 10.0.0.0/24 -j SNAT --to-source 192.17.0.x


iptables -t nat -A POSTROUTING -o eth0 -p tcp  -m multiport --dports 25,110,143 -s 10.0.0.0/24 -j SNAT --to-source 89.0.x.x


echo "1" > /proc/sys/net/ipv4/ip_forward

What confuses me is that SNAT is done in POSTROUTING, this means after the routing process. How could I make the decision on what interface to send the packets based on protocol/ip/etc after the routing decision? Routing decision means choosind the outgoing interface based on some criteria (ex ip dest). right?

Please, help me understand this issue.

Many thanks
_________________
Best regards,
ddaas
Back to top
View user's profile Send private message
Dagger
Retired Dev
Retired Dev


Joined: 11 Jun 2003
Posts: 765
Location: UK

PostPosted: Mon Oct 01, 2007 10:53 am    Post subject: Reply with quote

1
well it's not that easy. First thing you need to have some kind of route protocol running on your gateway machine which will tell packets which route they should use.

2
SNAT is only changing SOURCE IP address, so it looks like your external IP address (which will allow packets flow in both directions) rather than 10.x.x.x SNAT has nothing to do with routing. SNAT can change the address of packet which has already been routed.
Using the rules
Quote:
Code:

iptables -t nat -A POSTROUTING -o eth1 -p tcp --dport 22 -s 10.0.0.0/24 -j SNAT --to-source 190.17.0.x
iptables -t nat -A POSTROUTING -o eth1 -p icmp  -s 10.0.0.0/24 -j SNAT --to-source 192.17.0.x

You can decide to forward/drop packets for specific protocol/address.
_________________
95% of all computer errors occur between chair and keyboard (TM)
Join the FSF as an Associate Member!
Post under CC license.
Back to top
View user's profile Send private message
ddaas
Tux's lil' helper
Tux's lil' helper


Joined: 28 Feb 2005
Posts: 106
Location: Germany

PostPosted: Mon Oct 01, 2007 11:52 am    Post subject: Reply with quote

Thanks for the answer.

1. Can't I tell the packets what route should they use? I mean some sort of static routes and the linux box should alternate these routes per-destination basis.
If this is not possible, is there any advantage of using a linux Router and 2 Internet connections?

2. I'll make some tests and if there are problems I'll post them here.


Thanks.
_________________
Best regards,
ddaas
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum