The number of website visits has increased and the response speed has become slower.
consider:
Scale Up (that is, Scale vertically) scales vertically, scales up: machine hardware upgrades, increase configuration, such as adding CPU, memory. (It is often necessary to purchase new machines) -> Old machines cannot be used.
Scale Out (that is, Scale horizontally) scales horizontally and scales out: adding a new machine to the original web and mail system. –> The old machine can still function.
Load balancing technology serves for scale out.
DNS polling
Let the domain name of our website be mapped to multiple server IPs. The user faces the domain name of our system. Then we can use a polling method. When user 1’s machine performs domain name resolution, DNS returns IP1 and user 2’s When the machine is doing domain name resolution, DNS returns IP2.
There is a problem: DNS is a hierarchical system with a cache, and the client machine also has a cache. If a machine fails, the domain name resolution will still return the IP of the problematic machine, and an error will occur.
NAT principle of
load balancing LVS
Set up a load balancing server (Load Balancer, LB), all user requests are sent to him, and then it is sent to each server.
LB has two IPs, one for external (115.39.19.22) and one for internal (192.168.0.100). What the user sees is the external IP. There are three servers that actually provide services behind, called RS1, RS2, and RS3, and their gateways all point to LB.
Request process:
The user sends an HTTP request and wants to visit the home page of our website. This HTTP request is put in a TCP message, and then put in an IP datagram, the destination is our LB (115.39.19.22).
Load Balancer wants to send this data packet to RS1 (192.168.0.10), it needs to change the destination address and port of the IP datagram header to RS1:
After RS1 is processed, to return to the HTML of the homepage, the HTTP message must be encapsulated layer by layer and sent to the gateway LB:
After LB receives the returned datagram, it needs to replace the source address and source port with its own, and then send it to the client:
The client does not feel that there are several servers working behind it. It always thinks that only Load Balancer is working.
LB selects the real server forwarding there are many strategies, such as:
Polling: This is the simplest one, which is to rotate one by one.
Weighted round-robin: In order to deal with the good performance of some servers, they can be weighted a little higher and the probability of being selected is higher.
Least connection: which server handles few connections, send it to whom.
Weighted least connection: on the basis of the least connection, also add weight
Since IP datagrams will be fragmented and transmitted at the network layer, these packets will be messed up if they are distributed to different real servers by LB, so LB must maintain a table to record the client's data packets to which real server we forwarded to. On the server, so when the next packet arrives, we can forward it to the same server.
It can be seen that the load balancing software needs to be connection-oriented, that is, the fourth layer of the OSI network system, which can be called four-layer load balancing.
Existing problems: all traffic must pass through it, it must modify the data packets sent by the customer, and also modify the data packets sent to the customer.
The network access request message is relatively short and the response message often contains a large amount of data. This further intensifies the work of Load Balancer to modify data packets.
We handle requests and responses separately.
DR principle of load balancing LVS
In order to return data without returning to LB to modify the original address, each server address needs to be set to a public network address. Set as VIP (115.39.19.22).
So many servers have the same IP, so how to solve the forwarding problem?
The forwarding of IP datagrams actually needs to pass through the MAC address, so the ARP protocol of the data link layer can be used.
When the router needs to forward the data packet of 115.39.19.22, it sends an ARP broadcast, requesting the MAC address, and only allows Load Balancer to respond to the ARP request of this VIP address (115.39.19.22), RS1, RS2, RS3, to suppress the VIP ARP response for the address. LB gets this datagram. It can use a certain strategy to select a server from RS1, RS2, RS3, such as RS1 (192.168.0.10), and encapsulate the IP datagram intact into a data link layer packet (the destination is the MAC address of RS1) ), forward directly. RS1 (192.168.0.10) this server has received the data packet. After processing, RS1 can directly respond and send it back to the client without going through Load Balancer. Because my address is 115.39.19.22.