Multi-link load balancing in Linux

Source: Internet
Author: User
Article title: multi-link load balancing in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
1.1 ---------------------- Multipath Routing
There are already many articles on multipath routing on the Internet. here I just want to point out that the following two patches must be installed to re-compile the kernel when applying multipath routing (at least the RH8 ip route package requires these two patches)
  
Name: iproute2-2.4.7-now-ss020116-try.tar.gz
URL: ftp://ftp.inr.ac.ru/ip-routing/
Function: solves the dead link problem.
  
Name: equalize_2.4.18.patch
URL: http://trash.net /~ Kaber/equalize/
Function: even if this patch is not available, ip route also has the equalize parameter, but the ping fails.
  
1.2.Multipath routing.
The gateway ip address obtained by multiple links of the same ISP may be the same. in this case, the multipath routing cannot work. In addition, it is said that multipath routing can only manage the outgoing package
  
  
2. 1. ------------------- Policy routing.
Principle:
The client ip address is randomly divided into N groups, each group corresponds to a link, and the group is completed by the shell script on the server. (This article does not involve the installation and configuration of dial-up software. please refer to other articles .)
Test environment:
RH8. No other patches are required.
Security questions:
There is no default route for the host that runs the program. External hosts cannot be accessed at all to completely prevent hacker attacks.
Disadvantages:
Not a real server load balancer, an IP address cannot occupy the bandwidth of the second link.
Link redundancy:
When a link is disconnected, you can convert the failed route to another line in the/etc/ppp/ip-down.local script.
In this script, $1 is the interface name, for example, ppp0
Ip route add default dev ppp1 table 201
  
Script:
-------------------------------------------------
# The IP address marked as fwmark 1 belongs to the table 201 address pool.
Ip rule add fwmark 1 table 201
Ip rule add fwmark 2 table 202
  
--------------------------------------------------
# Modify/etc/rc. local
Echo "enabling forwarding .."
Echo "1">/proc/sys/net/ipv4/ip_forward
  
Echo "enabling DynamicAddr .."
Echo "1">/proc/sys/net/ipv4/ip_dynaddr
Insmod ip_nat_ftp
Insmod ip_nat_irc
Insmod iptable_nat
Insmod ip_conntrack_irc
Insmod ip_conntrack_ftp
  
Iptables-t nat-a postrouting-o ppp0-j MASQUERADE
Iptables-t nat-a postrouting-o ppp1-j MASQUERADE
  
#201 clients in the address pool are allocated to ppp0
Ip route add default dev ppp0 table 201
Ip route add default dev ppp1 table 202
# Do not accept the default gateway of the ISP. Otherwise, the default gateway of the ISP will be deleted before this date.
  
----------------------------------------------------------
# Ip distribution main program. you need to change Linenum to the number of links according to the actual situation,
# And cat pool1 pool2> pooltmp
  
# Start the program by using the telnet client and use nohup TheScriptName & to prevent unexpected program suspension.
  
#! /Bin/bash
LineNum = 2
ME = $ (basename $0)
# Evaluate load on diffrent lines, return the less busy one.
#
Function EvaLoad ()
{
Fwmark = 1
Cnt = 1
Line = $ (wc-l pool1 | awk {print $1 })
While [$ Cnt-lt $ LineNum]
Do
Cnt =$ (expr $ Cnt + 1)
  
LineN = $ (wc-l pool $ Cnt | awk {print $1 })
If [$ Line-gt $ LineN]
Then
Line = $ LineN
Fwmark = $ Cnt
Fi
  
Done
  
}
  
# Start script
Function DispatcherStart ()
{
# Initialize ip address pool automatically
Cnt = 1
While [$ Cnt-le $ LineNum]
Do
  
Cat/dev/null> pool $ Cnt
Cnt =$ (expr $ Cnt + 1)
  
Done
Iptables-t mangle-F
  
# Contact ARP table to find new clients
While:
Do
Grep [0-9]/proc/net/arp | awk {print $1}> iparp
  
Cat pool1 pool2> pooltmp
  
Cat iparp | while read ClientIp
Do
Grep-w "$ ClientIp" pooltmp>/dev/null
If [$? -Ne 1]
Then
Continue
Fi
EvaLoad
Iptables-t mangle-a prerouting-s $ ClientIp/32-j MARK -- set-mark $ Fwmark
Echo $ ClientIp> pool $ Fwmark
Done
  
Sleep 30
Done
}
#
# ---------------- Main script
#
#
Case "$1" in
Start)
Echo-e "Bringing up Dispatcher Program \ n"
Echo ""
DispatcherStart
;;
  
Stop)
Echo-e "Shutting down Dispatcher program \ n"
  
Kill-9 $ (ps-ef | grep "$ ME" | awk {print $2 })
If [$? = 0]; then
Echo success
Else
Echo failure
Fi
Echo ""
;;
Restart)
$0 stop
$0 start
;;
  
*)
Echo "Usage: $ ME {start & | stop | restart }"
Exit 1
Esac
  
Exit 0
  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.