Ubuntu + LVS to build four layers to achieve the load balance of the custom protocol

Source: Internet
Author: User
Tags chmod

One, overview:

The experimental environment is Ubuntu 14, the kernel already has the LVS module

Load machine: 10.10.2.176 (this machine needs to install IPVSADM), need to build a virtual server on the machine, its IP is 10.10.2.177

Real Server 1:10.10.2.190

Real Server 2:10.10.2.191

Real Server 3:10.10.2.192


When the request passes through virtual server, it is forwarded directly to the real machine for processing


Second, the steps

1, on the 10.10.2.176 machine, install the IPVSADM:

sudo apt-get install Ipvsadm

2, on the 10.10.2.176 machine, write the following script to configure the script as wsdc_lvs.sh:

#!/bin/bash

# for SERVER PORTAL #
vip=10.10.2.177
rs1=10.10.2.190
rs2=10.10.2.191
rs3= 10.10.2.192

/sbin/ifconfig eth0:0 $vip netmask 255.255.255.0 broadcast $vip
/sbin/route add-host $vip Dev eth0:0
echo "1" >/proc/sys/net/ipv4/ip_forward

/sbin/ipvsadm-c
#/sbin/ipvsadm-a-T $vip: 80-s RR
#/ Sbin/ipvsadm-a-T $VIP: 8080-s RR
/sbin/ipvsadm-a-T $vip: 8111-s RR

/sbin/ipvsadm-a-T $vip: 8111-r $rs 1:811 1-g
/sbin/ipvsadm-a-t $vip: 8111-r $rs 2:8111-g
/sbin/ipvsadm-a-t $vip: 8111-r $rs 3:8111-g ipvsadm

3, run the configuration script on the 10.10.2.176 machine

sudo chmod a+x wsdc_lvs.sh
sudo./wsdc_lvs.sh

4, on the three real servers, write the following script:

#!/bin/bash
vip=10.10.2.177
/sbin/ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $VIP
/sbin/route Add-host $VIP 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
/sbin/sysctl-p

sudo chmod a+x wsdc_lvs.sh
sudo./wsdc_lvs.sh

Third, test

Write your own program based on the custom protocol and test it.


Iv. Appendix

Appendix LVS Parameter Introduction
Some of the parameters are as follows: Man Ipvsadm can see all.
-A,--add-service add A virtual Service
-E,--edit-service edit a virtual service
-D,--delete-service delete a virtual service
-C,--clear clear the Virtual server table.
-R,--restore
-S,--save
-A,--add-server add a real server to a virtual service
-E,--edit-server edit a real server in a virtual service
-D,--delete-server Remove a real server from a virtual service
-L,-l,--list list the Virtual server table if no argument is specified
-T,--tcp-service use TCP service.
-U,--udp-service use UDP service


-s Specifies the algorithm used by the service, the commonly used algorithm parameters are as follows:


RR Wheel Call (Round Robin)
The scheduler assigns external requests sequentially to the real servers in the cluster through a "wheel call" scheduling algorithm, which treats each server equally regardless of the actual number of connections and system load on the server.
WRR weighted wheel called (Weighted Round Robin)
The scheduler dispatches the access request according to the different processing ability of the real server through the "Weighted round call" scheduling algorithm. This ensures that the processing capacity of the server to handle more access traffic. The scheduler can automatically inquire about the load of the real server and adjust its weights dynamically.
LC Minimum Link (least connections)
The scheduler dynamically dispatches network requests to the server with the fewest number of links that have been established through the least-connection scheduling algorithm. If the real server of the cluster system has similar system performance, the "Minimal connection" scheduling algorithm can be used to balance the load well.
WLC weighted Minimum link (weighted least connections)
In the case of the server performance difference in the cluster system, the scheduler uses the "Weighted least link" scheduling algorithm to optimize the load balancing performance, and the server with higher weights will bear a large proportion of the active connection load. The scheduler can automatically inquire about the load of the real server and adjust its weights dynamically.
LBLC minimal links based on locality (locality-based least connections)
The "least local link" scheduling algorithm is based on the load balance of target IP address, and is mainly used in cache cluster system. The algorithm finds the most recently used server for the destination IP address based on the requested destination IP address. If the server is available and is not overloaded, send the request to the server, if the server does not exist, or if the server is overloaded and the server is half of the workload, select an available server with the "least link" principle , the request is sent to the server.
LBLCR-based least-localized links with replication (locality-based least connections with Replication)
The "Local least link" scheduling algorithm with replication is also a load balance for target IP address, which is mainly used in cache cluster system. It differs from the LBLC algorithm in that it maintains mappings from one destination IP address to a group of servers, while the LBLC algorithm maintains mappings from one destination IP address to a single server. The algorithm finds the corresponding server group for the target IP address according to the target IP address of the request. According to the "minimum connection" principle, select a server from the server group, if the server is not overloaded, send the request to the server, if the server overload; Select a server from this cluster by the "minimum Connection" principle, Add the server to the server group and send the request to the server. Also, when the server group has not been modified for some time, the busiest server is removed from the server group to reduce the degree of replication.
DH Target Address hash (destination hashing)
The destination hash scheduling algorithm finds the corresponding server from a statically allocated hash table as the hash key (hash key) according to the requested destination IP address, and if the server is available and not overloaded, sends the request to the server, or returns empty.
SH Source Address hash (source hashing)
The "Source address hash" Scheduling algorithm based on the requested source IP address, as the hash key (hash key) from the statically allocated hash table to find the corresponding server, if the server is available and not overloaded, send the request to the server, otherwise return empty.


-A represents adding a real server to a service
-r Specifies the IP address of real server
-W indicates weight
-G Indicates the use of DR,-M for Nat, and-I for tunneling mode.



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.