Bandwidth Load Balancing for multiple ADSL connections in linux

Source: Internet
Author: User

This article introduces multiple ADSL instances in linux to achieve bandwidth load balancing. I hope this instance will be helpful to you.


Recently, the company has a need to use multiple adsl to connect to a linux server for NAT, and to achieve bandwidth load balancing. Search for some materials online. Record the key points in the process and keep them as backup.

1. You need to install rp-pppoe, which is required for dial-up.

2. Create a dial configuration file, take/etc/sysconfig/network-scripts/ifcfg-ppp309 as an example, according to the actual situation do not have to copy :)
Shell Command example

[Root @ ***** ~] # Cat/etc/sysconfig/network-scripts/ifcfg-ppp309
USERCTL = yes
BOOTPROTO = dialup
NAME = DSLppp309
DEVICE = ppp309
TYPE = xDSL
ONBOOT = no
PIDFILE =/var/run/pppoe-adsl309.pid
FIREWALL = NONE
PING =.
PPPOE_TIMEOUT = 80
LCP_FAILURE = 3
LCP_INTERVAL = 20
Clampm Ss = 1412
CONNECT_POLL = 6
CONNECT_TIMEOUT = 60
DEFROUTE = yes
SYNCHRONOUS = no
ETH = eth4 # Here I use the physical network card ETH4. Remember to insert the cat's network cable into this network card so that I can dial.
PROVIDER = DSLppp309
USER = XXXXX # ADSL account
PEERDNS = no
DEMAND = no

3. Create a dialing Password File
?
Shell Command example
 
[Root @ **** ~] # Cat/etc/ppp/chap-secrets
# Secrets for authentication using CHAP
# Client server secret IP addresses
####### Redhat-config-network will overwrite this part !!! (Begin )##########
####### Redhat-config-network will overwrite this part !!! (End )############
"ADSL account" * "password"

4. Place all the cats on different physical NICs. Add the configuration file and password at one time according to step 2 and 3.

5. Now test dial-up (then test different configuration files at a time, which is equivalent to testing whether all the accounts can dial up successfully. check whether all the cats are loaded with power and whether the network cable is plugged in)
?
Shell Command example
 
/Sbin/adsl-start/etc/sysconfig/network-scripts/ifcfg-ppp309

If the dial is successful, ifconfig displays information about ppp0 (note that no information about other physical NICS is provided here, you know, but it does not affect the effect in practice)
?
Shell Command example

 
[Root @ **** ~] # Ifconfig
Ppp0 Link encap: Point-to-Point Protocol
Inet addr: 121.34.103.105 P-t-P: 121.34.100.1 Mask: 255.255.255.255
Up pointopoint running noarp multicast mtu: 1492 Metric: 1
RX packets: 54809 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 57439 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 txqueuelen: 3
RX bytes: 47257780 (45.0 MiB) TX bytes: 4184643 (3.9 MiB)

Disconnect a dial-up connection
?
Shell Command example
 
/Sbin/adsl-stop/etc/sysconfig/network-scripts/ifcfg-ppp309

6. After all the dial-up operations are successful, create an iptables rule to allow the server to provide the NAT Function (here there are four ADSL, ppp0 ~ Ppp3)
?
Shell Command example

 
Iptables-t nat-F
Iptables-t nat-X
Iptables-t nat-P PREROUTING ACCEPT
Iptables-t nat-P POSTROUTING ACCEPT
Iptables-t nat-P OUTPUT ACCEPT
Iptables-t mangle-F
Iptables-t mangle-X
Iptables-t mangle-P PREROUTING ACCEPT
Iptables-t mangle-P INPUT ACCEPT
Iptables-t mangle-P FORWARD ACCEPT
Iptables-t mangle-P OUTPUT ACCEPT
Iptables-t mangle-P POSTROUTING ACCEPT
Iptables-F
Iptables-X
Iptables-P FORWARD ACCEPT
Iptables-P INPUT ACCEPT
Iptables-P OUTPUT ACCEPT
Iptables-t raw-F
Iptables-t raw-X
Iptables-t raw-P PREROUTING ACCEPT
Iptables-t raw-P OUTPUT ACCEPT
####
Modprobe ip_conntrack hashsize = 30000
Iptables-t nat-a postrouting-s 10.13.0.0/255.255.0.0-o ppp0-j MASQUERADE
Iptables-t nat-a postrouting-s 10.13.0.0/255.255.0.0-o ppp1-j MASQUERADE
Iptables-t nat-a postrouting-s 10.13.0.0/255.255.0.0-o ppp2-j MASQUERADE
Iptables-t nat-a postrouting-s 10.13.0.0/255.255.0.0-o ppp3-j MASQUERADE
Iptables-t nat-a postrouting-s 10.13.0.0/255.255.0.0-o eth0-j MASQUERADE)
Iptables-a forward-s 10.13.0.0/16-j ACCEPT
Iptables-a forward-d 10.13.0.0/16-j ACCEPT
###
Iptables-a input-s 10.13.0.0/16-p icmp-j ACCEPT
Iptables-a input-m state -- state RELATED, ESTABLISHED-j ACCEPT
Iptables-a input-s 127.0.0.1-j ACCEPT
Iptables-a input-d 127.0.0.1-j ACCEPT
Ip route

7. To date, there is still a problem that has not been solved. How can I achieve load balancing of bandwidth?
In fact, the main command can be done. You need to add multiple gateways ...... Hey
?
Shell Command example
 
Ip route replace default equalize nexthop dev ppp0 weight 1 nexthop dev ppp1 weight 1 nexthop dev ppp2 weight 1 nexthop dev ppp3 weight 1

But there is still a problem. What if the ADSL is DOWN and swollen?
Let's solve this problem.
How the script works: checks the status of the current ADSL connection every five seconds. If there is a disconnection, the line will be kicked out of the gateway collection (here 4 lines, if you have a few records, you can modify the script on your own. Do not copy the script ......)
?
Shell Command example

 
#! /Bin/bash
# Check ppp status
# Date: 2013-03-29
# By minyoni
#
#
######################################## ######################################## ##########################
Ip route replace default equalize nexthop dev ppp0 weight 1 nexthop dev ppp1 weight 1 nexthop dev ppp2 weight 1 nexthop dev ppp3 weight 1
 
ACTIVE_PPP_STATUS = "15"
While (true)
Do
PPP_STATUS = "0"
If ["'ifconfig | awk-F" [:] "'/ppp0/{print $1}'" = "ppp0"]; then
PPP_STATUS = 'expr $ PPP_STATUS + 1'
Fi
#
If ["'ifconfig | awk-F" [:] "'/ppp1/{print $1}'" = "ppp1"]; then
PPP_STATUS = 'expr $ PPP_STATUS + 2'
Fi
#
If ["'ifconfig | awk-F" [:] "'/ppp2/{print $1}'" = "ppp2"]; then
PPP_STATUS = 'expr $ PPP_STATUS + 4'
Fi
#
If ["'ifconfig | awk-F" [:] "'/ppp3/{print $1}'" = "ppp3"]; then
PPP_STATUS = 'expr $ PPP_STATUS + 8'
Fi
 
###
If [$ ACTIVE_PPP_STATUS-ne $ PPP_STATUS]; then
 
Case "$ PPP_STATUS" in
0)/root/check-ppp-status.sh
;;
1) ip route replace default via 120.197.94.1
;;
2) ip route replace default via 120.197.94.1
;;
3) ip route replace default equalize nexthop dev ppp0 weight 1 nexthop dev ppp1 weight 1
;;
4) ip route replace default via 120.197.94.1
;;
5) ip route replace default equalize nexthop dev ppp0 weight 1 nexthop dev ppp2 weight 1
;;
6) ip route replace default equalize nexthop dev ppp1 weight 1 nexthop dev ppp2 weight 1
;;
7) ip route replace default equalize nexthop dev ppp0 weight 1 nexthop dev ppp1 weight 1 nexthop dev ppp2 weight 1
;;
8) ip route replace default via 120.197.94.1
;;
9) ip route replace default equalize nexthop dev ppp0 weight 1 nexthop dev ppp3 weight 1
;;
10) ip route replace default equalize nexthop dev ppp1 weight 1 nexthop dev ppp3 weight 1
;;
11) ip route replace default equalize nexthop dev ppp0 weight 1 nexthop dev ppp1 weight 1 nexthop dev ppp3 weight 1
;;
12) ip route replace default equalize nexthop dev ppp2 weight 1 nexthop dev ppp3 weight 1
;;
13) ip route replace default equalize nexthop dev ppp0 weight 1 nexthop dev ppp2 weight 1 nexthop dev ppp3 weight 1
;;
14) ip route replace default equalize nexthop dev ppp1 weight 1 nexthop dev ppp2 weight 1 nexthop dev ppp3 weight 1
;;
15) ip route replace default equalize nexthop dev ppp0 weight 1 nexthop dev ppp1 weight 1 nexthop dev ppp2 weight 1 nexthop dev ppp3 weight 1
;;
Esac
Ip route flush cache
Fi
 
ACTIVE_PPP_STATUS = "$ PPP_STATUS"
Sleep 5
Done

Then, put it in the background for processing ......
?
Shell Command example
1
 
Nohup sh the name of the script you created &

8. Subsequent explanations ......
View Current Route status and gateway
?
Shell Command example
 
Ip route

View ADSL connections
Shell Command example
 
Ip link ls

In fact, you can modify the above script. If there is a connection exception, you can send an email or send an SMS alarm to check whether there are additional requirements.
Well, it's a good deal!

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.