A simple example of Linux load balancer software LVS

Source: Internet
Author: User

A simple example of this article demonstrates the configuration of LVS (DR Mode) and testing.
The configuration environment used is as follows:
Director server:192.168.8.4
Real server1:192.18.8.5
Real server2:192.168.8.6
vip:192.168.8.200
The network topology diagram is as follows:

The VIP in Figure 2 refers to the virtual IP address, also called the LVS Cluster service IP, in DR, Tun mode,
The packet is returned directly to the user, so the address needs to be set on the director server and on each node of the cluster.
This IP is typically tied to a loopback address on a real server, such as lo:0.
On the director server, the virtual IP is bound on a real network interface device, such as eth0:0.
The Linux2.6 kernel supports the LVS function by default, the system is installed, and the following command can be used to check if kernel has supported the Ipvs module of LVS:
[[Email protected] ~] #modprobe-L |GREP Ipvs
/lib/modules/2.6.18-53.el5/kernel/net/ipv4/ipvs/ip_vs_sh.ko
/lib/modules/2.6.18-53.el5/kernel/net/ipv4/ipvs/ip_vs_rr.ko
If there is an output similar to the above, the system kernel already supports the Ipvs module by default. You can then install the Ipvs management software.

Install Ipvs management software on director serve
Download the corresponding ipvsadm-1.24 version and proceed with the installation:
Tar zxvf ipvsadm-1.24.tar.gz
CD ipvsadm-1.24
Make
Make install
Note: Error compilation information may occur during make because the compiler cannot find the corresponding kernel.
Follow these steps to compile normally:
[[Email protected] ~] #ln-S/usr/src/kernels/2.6.18-53.el5-i686//usr/src/linux
After the installation is complete, configure it via the IPVSADM command line

First, bind a virtual IP on the director server, which is used to provide services externally and execute the following command:
Ifconfig eth0:0 192.168.8.200 broadcast 192.168.8.200 netmask 255.255.255.255 up

In this way, a virtual device eth0:0 is bound on the eth0 device, and a virtual IP is set to 192.168.8.200,
That is, the IP address we planned above, and then specify the broadcast address is also 192.168.8.200, it is important to note that
The subnet mask here is 255.255.255.255.
Then assign a route to the device eth0:0 and execute the following command:
Route add-host 192.168.8.200 Dev eth0:0
Then enable the system's packet forwarding function, so that the system acts as a router, execute the following command:
[[Email protected] ~] #echo "1" >/proc/sys/net/ipv4/ip_forward
Directive, the value of the parameter is 1 o'clock IP forwarding is enabled and IP forwarding is forbidden for 0 o'clock. In fact, in DR Mode, it is not necessary to turn on the packet forwarding function of the system.
This operation is required in NAT mode.
Then start configuring Ipvs and do the following:
Ipvsadm-c
Ipvsadm-a-T 192.168.8.200:8080-s RR
Ipvsadm-a-T 192.168.8.200:8080-r 192.168.8.5:8080-g
Ipvsadm-a-T 192.168.8.200:8080-r 192.168.8.6:8080-g
In the above operation, the first line clears all records from the list of kernel virtual servers.
The second line is to add a new virtual IP record, specifying that the policy is polling.
Line third to fourth adds two new real server records to the new plus virtual IP record, and specifies that the LVS work mode is the direct route mode.
Finally, start the LVS service and do the following:
[[Email protected] ~] #ipvsadm
This way, the LVS configuration on the director server is complete.

Second, the Real server configuration
In the LVS DR and Tun mode, when the user's access request arrives at the real server, it is returned to the user directly,
Instead of passing through the front-end director Server, you need to increase the virtual VIP address on each real Server node,
So that the data can be directly returned to the user, the addition of the VIP address can be done by the following command:
Ifconfig lo:0 192.168.8.200 broadcast 192.168.8.200 netmask 255.255.255.255 up
Route add-host 192.168.8.200 Dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
Sysctl-p

A virtual IP address is bound on the loopback device, and its subnet mask is set to 255.255.255.255,
Maintain interoperability with the virtual IP on the director server, and then disable ARP requests for this computer.
Because the virtual IP, which is the VIP address above, is shared by the director server and all real servers,
If there is an ARP request VIP address, the Director server responds with all real servers, and the problem occurs.
Therefore, the real server needs to be prevented from responding to ARP requests.

Third, configure the Tomcat service on the real server, slightly.
To build an HTML file in 192.18.8.5, the contents are:
This is the test page
<br>
From server 192.168.8.5
To build an HTML file in 192.18.8.6, the contents are:
This is the test page
<br>
From server 192.168.8.6

Four Testing
The client is accessed through a VIP, such as the following URL
Http://192.168.8.200:8080/test/test.html
The returned content can be found to be provided alternately by 192.168.8.5 and 192.168.8.6.


A simple example of Linux load balancer software LVS

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.