62, Load Balancing cluster introduction, LVS Introduction, LVS scheduling algorithm, LVS NAT mode construction
First, load Balancing cluster introduction
Main open source software LVs, keepalived, Haproxy, Nginx, etc.
The LVS belongs to 4 layer (network OSI 7 layer model), Nginx belongs to 7 layer, Haproxy can be considered as 4 layer, also can be used as 7 layer.
OSI Introduction: The OSI adopts layered structured technology, which is divided into seven layers, the physical layer, the data link layer, the network layer, the transport layer, the conversation layer, the presentation layer, the application layer.
Keepalived load balancing function is actually LVS, built-in functions.
LVS This 4-tier load balancer can distribute other ports except 80, such as MySQL, while Nginx supports only Http,https,mail,haproxy and MySQL.
In comparison, the 4-layer LVS is more stable, can withstand more requests, and nginx this 7-layer more flexible, can achieve more personalized requirements.
Ii. introduction of LVS
LVS is developed by Chinese Zhangwensong
Popularity is no less than Apache httpd, TCP/IP-based routing and forwarding, high stability and efficiency
The latest version of LVS is based on Linux kernel 2.6 and has not been updated for many years
LVS has three common patterns: NAT, IP Tunnel, DR
The LVS architecture has a Core Role called Distribution Device (Load balance), which is used to distribute the user's requests, and there are many server that handles user requests (Real Server, abbreviation RS )
LVS NAT Mode
Load Balance: Dispenser
Process: The user sends the request to the dispatcher, the dispatcher distributes the task to the back-end RS Server, the RS server processes the results and then tells the dispatcher the results, and the distributor feeds back to the user.
Because the dispatcher needs to handle a lot of tasks, so the request volume can not be too large, the general scale of more than ten units, or 10 or less, otherwise it may be inadequate, unless the configuration is very high.
This model is implemented using the Iptables NAT table
After the user's request to the dispatcher, the requested packet is forwarded to the back-end RS via a preset iptables rule.
RS only needs to set the gateway as the Distributor's intranet IP, because it does not communicate with the outside world.
The data packets that are requested by the user and the packets returned to the user all need to go through the dispatcher, so the dispatcher becomes the bottleneck.
in NAT mode, only the distributor is required to have a public IP, so the comparison Save public IP Resources .
LVS IP tunnel mode
this model needs to have a Public IP configured on the Distributor and all RS, we call it VIP
IP Tunnel Implementation principle:
1. The client requests the target IP is the VIP, the dispatcher receives the request packet, will make a processing to the packet, will change the target IP to the IP of an RS, so the packet is on the RS.
2.rs after receiving the packet, the original packet will be restored, so that the target IP in the original package is a VIP, because all RS configured on the VIP, so it will be considered itself, the packet has a source IP, and then the RS processing good data, through the public IP directly send the results to the user So there's no bottleneck in the dispenser, and it's not that tired.
LVS Dr Mode
This mode also requires a common IP configuration on the Distributor and all RS, which is the VIP.
Here the dispatcher is called Director,nat mode and the IP tunnel mode is called load balance.
The Distributor and RS server need to have an intranet
Unlike IP tunnel, it will put the packet's MAC address Modify the MAC address to Rs.
After the RS receives the packet, it restores the original packet so that the target IP is the VIP, because the VIP is configured on all RS, so it thinks it is itself, then the RS handles the data and sends the result directly to the user via the public IP.
Three, LVS scheduling algorithm
Previous four common, emphasis:
Polling Round-robin RR
The dispatcher sends the user's request evenly to the RS.
Weighted polling Weight round-robin WRR
With the weight of polling, the configuration of the RS weight can give high points, assigned to more tasks.
Minimum Connection least-connection LC
Send user requests to RS servers with fewer requests, because RS is busy and will process faster.
Weighted Minimum connection Weight least-connection WLC
The minimum connection based on locality locality-based Least Connections LBLC
Locally-based minimal connection with replication locality-based Least Connections with Replication LBLCR
Destination Address hash dispatch Destination Hashing DH
Source Address hash Dispatch source Hashing sh
Four, LVS NAT mode construction
Preparatory work:
Three machines
1) Dispatcher, also called Scheduler (dir)
First change the network card 2 to only the host mode, and then into the virtual network editor to see the net address of the network segment, and then change the network configuration ens37, set up a good after starting the network card, with Windows cmdping this 232.1 to see if you can communicate.
Intranet ens33:93.130, extranet ens37:232.1 (ens37 to host mode only)
2) Rs1
Intranet: 93.131, set gateway to 93.130 (Distributor intranet IP)
Install a iptables-services.
3) rs2
Intranet: 93.132, set gateway to 93.130
Execute on all three machines
Systemctl Stop Firewalld; Systemctl Disable FIREWALLD
Systemctl start iptables;systemctl enable iptables; iptables-f; Service Iptables Save
Turn off SELinux.
Start building NAT Mode:
in the dir (distributor) installation Ipvsadm
Ipvsadm Tool: An important tool to implement LVS.
Yum Install-y Ipvsdam
Writing scripts on Dir, LVs is executed in a scripted manner for ease of maintenance.
# vim/usr/local/sbin/lvs_nat.sh//content is as follows
#! /bin/bash
# routing forwarding is enabled on the director server, which is the kernel parameter for routing and forwarding.
Echo 1 >/proc/sys/net/ipv4/ip_forward
# Turn off ICMP redirection
echo 0 >/proc/sys/net/ipv4/conf/all/send_redirects
echo 0 >/proc/sys/net/ipv4/conf/default/send_redirects
# Note the name of the network card, and the name of your network card is what you write.
echo 0 >/proc/sys/net/ipv4/conf/ens33/send_redirects
echo 0 >/proc/sys/net/ipv4/conf/ens37/send_redirects
# Director Set NAT firewall
Iptables-t nat-f
Iptables-t nat-x//emptying chain
Iptables-t nat-a postrouting-s 192.168.93.2/24-j Masquerade//can allow the same network segment of the intranet to the Internet.
# Director sets the IPVSADM function
ipvsadm= '/usr/sbin/ipvsadm '//A variable is set here, which refers to this variable ($IPVSADM).
$IPVSADM-c//purge rule
$IPVSADM-A-t 192.168.232.1:80-s WLC -P 3//The WLC here is an algorithm that can write RR etc;-a adds a rule that specifies whether NAT mode or IP tunnel mode, or Dr mode;-T followed by distribution IP;-S Specifies the algorithm for the external network of the device,-p specifies its timeout, 3 seconds to access the a server up, can be set 0, if set 0 cannot execute this script, remove this parameter.
$IPVSADM-T 192.168.232.1:80-r 192.168.93.131:80-m-W 1
$IPVSADM-T 192.168.232.1:80-r 192.168.93.132:80-m-W 1
Here are the two rules below:
-r: Specify RS Server
-M: Description is NAT mode
-W: Weight
Nat Mode effect test
# sh/usr/local/sbin/lvs_nat.sh//execute this script first, no output is no error
Installation of Nginx on both RS
Set two RS homepage, make a distinction, that is to say, directly curl two RS IP, get different results
# vim/usr/share/nginx/html/index.html//The two machines write different content to differentiate, I here because the third default page is nginx default virtual host, so in the default virtual host index.html change content:/data/ Wwwroot/default/index.html
Test: # Curl 192.168.232.1
Multiple visits look at the result differences several times, and you can find that access has always been rs1 and rs2 in switching access.
Extended
Three models of LVs detailed http://www.it165.net/admin/html/201401/2248.html
several algorithms of LVs http://www.aminglinux.com/bbs/thread-7407-1-1.html
about Arp_ignore and arp_announce http://www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
related to the LVS principle http://blog.csdn.net/pi9nc/article/details/23380589
62, load Balancing cluster introduction, LVS Introduction, LVS scheduling algorithm, LVS NAT mode construction