Use iptables and iproute2 to balance network load

Source: Internet
Author: User


Iptables and iproute2 are used to achieve network load balancing. Assume that there are two external interfaces in the Network: eth0 172.16.1.1/24, eth1 10.0.0.1/24, and the interface connecting to the internal network is eth2 192.168.1.1. Now we have designed such a policy to export all the web service data from the internal network to 172.16.1.1. Other data goes to the exit 10.0.0.1. # Set ifconfig eth0 172.16.1.1 netmask 255.255.255.0ifconfig eth1 10.0.0.1 netmask 255.255.255.0ifconfig eth2 192.168.1.1 netmask 255.255.255.0echo 1>; /proc/sys/net/ipv4/ip_forward www.2cto.com # mark the web service class package 100 # This step is critical. iproute2 toolkit is used to implement policy routing, however, the iproute2 toolkit cannot match based on the port. Therefore, use iptables to work with iptables-t mangle-a prerouting-p tcp -- dport 80-j MARK -- set-mark 100 # add multiple route tables. Assume that the gateway at 172.16.1.1 is 172.16.1.254ip route. add 0/0 via 172.16.1.254 table 100 www.2cto.com # Set the routing policy. For data packets with 100 mark bits, query route table no. 100 ip address rule add fwmark 100 table 100 # If NAT is not required, directly route iptables-t NAT-a postrouting-o eth2-j MASQUERADE
 

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.