• Willkommen im Linux Club - dem deutschsprachigen Supportforum für GNU/Linux. Registriere dich kostenlos, um alle Inhalte zu sehen und Fragen zu stellen.

Load Balancing über iptables

HltmaN

Newbie
Hallo zusammen,

ich möchte gerne eine Lastverteilung über iptables erzielen. Allerdings verstehe ich nicht ganz warum es bei mir so nicht funktioniert. Über VMware habe ich versucht die Lastverteilung zu simulieren und manchmal scheint der ping zu funktionieren aber in 80% der Fälle nicht. An den privaten IPs kann es wohl kaum liegen. Kann mir bitte jemand weiterhelfen?

Code:
Daten:
ISP 1:
IP: 172.16.1.2
NET: 172.16.1.0/24

ISP 2:
IP: 172.16.0.2
NET: 172.16.0.0/24

Local:
eth0
NET: 192.168.3.0

über eth1 wird ISP1 erreicht 
eth1 IP: 172.16.1.1
über eth2 wird ISP2 erreicht
eth2 IP: 172.16.0.1

Zunächst habe ich in /etc/iproute2/rt_tables die 2 Tabellen angelegt:
250     ISP1
249     ISP2

Konfiguration:

Code:
#!/bin/bash

# delete all
sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F

# standard rules
sudo iptables -P INPUT   ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT  ACCEPT

# NAT
sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
sudo iptables -t nat -A POSTROUTING -o eth2 -j MASQUERADE

# chain which marks a packet (MARK) and its connection (CONNMARK) with mark 1 (for ISP1)
iptables -t mangle -N MARK-ISP1
iptables -t mangle -A MARK-ISP1 -j MARK --set-mark 1
iptables -t mangle -A MARK-ISP1 -j CONNMARK --save-mark
 
# chain which marks a packet (MARK) and its connection (CONNMARK) with mark 2
iptables -t mangle -N MARK-ISP2
iptables -t mangle -A MARK-ISP2 -j MARK --set-mark 2
iptables -t mangle -A MARK-ISP2 -j CONNMARK --save-mark
 
# If the packet is not NEW, there must be a connection for it, so get the connection
# mark and apply it to the packet
 
# packets from dev network
iptables -t mangle -A PREROUTING -i eth0 -m conntrack --ctstate ESTABLISHED,RELATED -j CONNMARK --restore-mark
 
# on the other hand, it the state is NEW, we have to decide where to send it
# Use the statistics match in nth mode
 
# dev network
iptables -t mangle -A PREROUTING -i eth0 -m conntrack --ctstate NEW -m statistic --mode nth --every 2 --packet 0 -j MARK-ISP1
iptables -t mangle -A PREROUTING -i eth0 -m conntrack --ctstate NEW -m statistic --mode nth --every 2 --packet 1 -j MARK-ISP2
 
# add local routes too
ip route flush table ISP1
ip route add table ISP1 default dev eth1 via 172.16.1.2
ip route add table ISP1 172.16.1.0/24 dev eth1 src 172.16.1.1                           
ip route add table ISP1 172.16.0.0/24 dev eth2 src 172.16.0.1                           
ip route add table ISP1 192.168.3.0/24 dev eth0 src 192.168.3.1                         
 
ip route flush table ISP2
ip route add table ISP2 default dev eth2 via 172.16.0.2
ip route add table ISP2 172.16.1.0/24 dev eth1 src 172.16.1.1                           
ip route add table ISP2 172.16.0.0/24 dev eth2 src 172.16.0.1                           
ip route add table ISP2 192.168.3.0/24 dev eth0 src 192.168.3.1                          

ip rule del from all fwmark 2 2>/dev/null
ip rule del from all fwmark 1 2>/dev/null
ip rule add fwmark 1 table ISP1
ip rule add fwmark 2 table ISP2
ip route flush cache

for i in /proc/sys/net/ipv4/conf/*/rp_filter; do echo 0 > "$i"; done
echo 1 > /proc/sys/net/ipv4/ip_forward

Vielen Dank schonmal.

Gruß HltmaN
 
OP
H

HltmaN

Newbie
Vielen Dank für deine Antwort spoensche ;)

Ich werd es morgen mal versuchen... Aber sonst glaubst du dass es so funktionieren sollte?
 
OP
H

HltmaN

Newbie
Ich habe es gerade nochmal versucht. Teilweise funktioniert der ping, aber in 70% der Fälle erscheint: From 172.16.0.2 Destination host unreachable oder es kommt garnichts zurück!!!

Der Code sieht jetzt wie folgt aus:

Code:
#!/bin/bash

for i in /proc/sys/net/ipv4/conf/*/rp_filter; do echo 0 > "$i"; done
echo 1 > /proc/sys/net/ipv4/ip_forward

# add local routes too
ip route flush table ISP1
ip route add table ISP1 default dev eth1 via 172.16.1.2
ip route add table ISP1 172.16.1.0/24 dev eth1 src 172.16.1.1                           
ip route add table ISP1 172.16.0.0/24 dev eth2 src 172.16.0.1                           
ip route add table ISP1 192.168.3.0/24 dev eth0 src 192.168.3.1                         
 
ip route flush table ISP2
ip route add table ISP2 default dev eth2 via 172.16.0.2
ip route add table ISP2 172.16.1.0/24 dev eth1 src 172.16.1.1                           
ip route add table ISP2 172.16.0.0/24 dev eth2 src 172.16.0.1                           
ip route add table ISP2 192.168.3.0/24 dev eth0 src 192.168.3.1                         

ip rule del from all fwmark 2 2>/dev/null
ip rule del from all fwmark 1 2>/dev/null
ip rule add fwmark 1 table ISP1
ip rule add fwmark 2 table ISP2

# delete all
sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F

# standard rules
sudo iptables -P INPUT   ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT  ACCEPT

# NAT
sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
sudo iptables -t nat -A POSTROUTING -o eth2 -j MASQUERADE

# chain which marks a packet (MARK) and its connection (CONNMARK) with mark 1 (for ISP1)
iptables -t mangle -N MARK-ISP1
iptables -t mangle -A MARK-ISP1 -j MARK --set-mark 1
iptables -t mangle -A MARK-ISP1 -j CONNMARK --save-mark
 
# chain which marks a packet (MARK) and its connection (CONNMARK) with mark 2
iptables -t mangle -N MARK-ISP2
iptables -t mangle -A MARK-ISP2 -j MARK --set-mark 2
iptables -t mangle -A MARK-ISP2 -j CONNMARK --save-mark
 
# If the packet is not NEW, there must be a connection for it, so get the connection
# mark and apply it to the packet
 
# packets from dev network
iptables -t mangle -A PREROUTING -i eth0 -m conntrack --ctstate ESTABLISHED,RELATED -j CONNMARK --restore-mark
 
# on the other hand, it the state is NEW, we have to decide where to send it
# Use the statistics match in nth mode
 
# dev network
iptables -t mangle -A PREROUTING -i eth0 -m conntrack --ctstate NEW -m statistic --mode nth --every 2 --packet 0 -j MARK-ISP1
iptables -t mangle -A PREROUTING -i eth0 -m conntrack --ctstate NEW -m statistic --mode nth --every 2 --packet 1 -j MARK-ISP2
 

ip route flush cache
 

spoensche

Moderator
Teammitglied
Du weisst, dass du mit
Code:
echo 0 > /proc/sys/net/conf/*/rp_filter
die Spoofing Protection deaktivierst?

https://blog.netways.de/2011/09/29/einfache-lastverteilung-unter-linux-ohne-loadbalancer/

http://www.policyrouting.org/PolicyRoutingBook/ONLINE/CH05.web.html#5.5
 
OP
H

HltmaN

Newbie
Ne das wusste ich nicht, hab nur in nem tutorial gelesen, dass man des machen muss ;)

Hast du eine Idee wie man des realisieren könnte oder wie wo der Fehler liegt?
 

spoensche

Moderator
Teammitglied
HltmaN schrieb:
Ne das wusste ich nicht, hab nur in nem tutorial gelesen, dass man des machen muss ;)

Das Tutorial legst du besser in den Papierkorb.

HltmaN schrieb:
Hast du eine Idee wie man des realisieren könnte oder wie wo der Fehler liegt?

Ja, ich weiss wo der Fehler liegt. Wenn du dem 2. Link gefolgt wärst und ein wenig gelesen hättest, wüsstest du es auch.;)

Mit den fwmarks kannst du deine Lastverteilung nicht realisieren, weil dir u.a. die Priorisierung der Routen fehlt. Du hast die beiden Provider nicht von einander getrennt. Also Routen für ISP2 haben nichts in der Routing Tabelle von ISP1 verloren.

Beispiel für ISP1 und 192,168.3.0/24:
Code:
ip rule add to 172.16.1.0/24 table ISP1 prio 9000
ip rule add from 192.168.3.0/24 table ISP1 prio 10000
ip route add default via 192.168.3.1 table ISP1

Für das Balancing musst du u.U. mit tc Filtern arbeiten.
 
OP
H

HltmaN

Newbie
Danke für deine Antwort ;) so wie ich das gesehen habe kann man auch bei fw-Mark rules prios setzen. Hab mir gerade mal das tc tool angeschaut, da braucht man wohl einige zeit bis man sich eingearbeitet hat. Hast du nicht zufällig schon ein fertiges Script oder könntest es schnell schreiben? Wäre echt sehr nett ;)

Gruß HltmaN
 

spoensche

Moderator
Teammitglied
HltmaN schrieb:
Danke für deine Antwort ;) so wie ich das gesehen habe kann man auch bei fw-Mark rules prios setzen. Hab mir gerade mal das tc tool angeschaut, da braucht man wohl einige zeit bis man sich eingearbeitet hat. Hast du nicht zufällig schon ein fertiges Script oder könntest es schnell schreiben? Wäre echt sehr nett ;)

Das Thema ist auch recht komplex. Nein, habe leider kein fertiges Script.
 
Oben