Load balancing and common implementation

First, what is load balancing

When a larger quantity per unit of time to access a server, the greater the pressure on the server. In order to avoid server crashes, allowing users to have a better experience, we have to share the server load balancing pressure by the way.

We can build many, many servers form a server cluster, when a user visits the site, to access an intermediate server, select the server in the middle so that the pressure of smaller servers in a server cluster, then the access request is introduced into the server. So since each user access, will ensure that each server in the server cluster pressure tends to balance, to share the pressure on the server, the server to avoid the crash.

Second, load balancing policy mechanism

Load balancing algorithm to consider their assigned either to achieve all depend on several algorithms below:

  1. Polling (default)
  2. Polling weight (the higher the weight, the greater the chance to enter)
  3. IP-Hash
  4. Fair
  5. URL-Hash

Polling (default)

Requests take turns to be allocated to each application server, allocation strategy is relatively simple.

Disadvantages: unevenness may occur, some server accepts requests heavy, heavy load pressure, some of the load is small, uncontrollable. In addition the need for synchronization between server session.

2. polling weight (the higher the weight, the greater the chance to enter)

Advantages: can be adjusted according to the situation. Controllable, still need session synchronization.

3.IP-Hash

Advantages: no need for synchronization session, will be fixed fixed IP access a single server.

Disadvantages: malicious attacks, cause a server overwhelmed. Different service providers and different oriented areas, IP may appear concentrated, resulting in uneven, uncontrollable.

4.Fair

This corresponds to an adaptive, load balancing will be allocated according to the speed of the server processing the request. The earliest the end of the process the request, and get the next request. It appears to be not very good. But generally do not use, saying it was taking into account the network instability. Remains to be studied. This session also needs to be synchronized.

5.URL-Hash

This hash is based on URL, so that some requests will always play a server. Conducive to the use of cache servers, but may be due to the uneven distribution of the hash value of the URL, as well as focusing on business caused some server stress, certain low load. This session also needs to be synchronized.

Third, to achieve load balancing in several ways

There are many ways to achieve load balancing:

  1. Load balancing software: such as the common Nginx, LVS.
  2. Hardware load balancing; buy the appropriate hardware.
  3. DNS load balancing: name resolution through DNS embodiment, a plurality of IP servers corresponding to a domain name.

In addition to hardware, here are some common ways, in addition to DNS DNS domain name resolution is implemented, other load balancing software are:

  1. HTTP redirection
  2. DNS resolution.
  3. Reverse Proxy (Nginx)
  4. IP Load Balancing
  5. Link layer load balancing (LVS)

1. HTTP redirection

HTTP redirection is a common server, when a user visits, which will be a cluster of servers according to a certain algorithm a real server IP address, which is a HTTP response header, in response to a status code (302), when when a user's browser receives this response, IP address of the real server will receive a raise and re-visit. As shown above, when the user accesses the domain name by DNS resolution obtained 114.100.20.200, then access 114.100.20.200, i.e. HTTP redirection response to a redirect 114.100.20.203, re-access the user's browser.

Here Insert Picture Description
Disadvantages:

  1. This approach requires the user's browser to access twice poor performance
  2. HTTP redirect the processing power of the server load balancing will become a bottleneck
  3. HTTP 302 redirect returned, it may cause the search engines to determine SEO cheating, lower search rankings

2.DNS DNS

We have a lot of servers IP, but can only use a domain name. When we visited the site through the domain name, you need to get the server through the DNS server IP address, we can set a certain algorithm on the DNS server, each get a different IP address to be accessed in order to achieve load balancing.

When a user accesses www.apusapp.com, the domain name corresponding to multiple IP addresses, obtain an IP address by the DNS server will resolve (see, get the IP address of a server cluster IP address of the server), user access to this IP address to achieve real service.
Here Insert Picture Description
advantage:

  1. The load balancing of work left to do a DNS server, eliminating the need for maintenance of the site management staff, easy to use, low cost, convenient and flexible.
  2. The server can be placed in any place.
  3. Meanwhile, DNS service can also be done based on the analysis of geographic location, allowing a nearest server's IP address back to improve performance

Disadvantages:

  1. .DNS multiple levels of service, with each level are likely to correspondence between the DNS cache, it is possible when a real server offline, modify the DNS server record, but some time before the entry into force during this period, its IP address is not available, and when domain names will still have access to this IP address, access will fail.
  2. DNS servers and real servers are completely separate, so DNS load balancing can not really monitor the current operating status of the server, its load balancing is not very good.

In fact, large sites will DNS load balancing as a first-class load balancing means, then a second-level load balancing inside the server, that is, we get through the DNS IP address is not the IP address of the real server, but the internal IP address of the server load balancing.

3. Reverse Proxy (Nginx)
Why is it called a reverse proxy: Proxy and Reverse Proxy: VPN service is what we commonly used as a proxy (forward proxy), will ask the user to the proxy server, proxy server to access the site to obtain data, after the proxy server and then return the data to the user. In this process, the application server does not know the user's presence. Just know that the proxy browser access.
Reverse proxy is a proxy server, the proxy server receives the user's request, forwarded to the real server, then back to the proxy server to give the user, in this process, the user does not know the existence of the real server.

Reverse proxy server manages a set of server, when a user visits, according to the proxy server load balancing algorithm to forward the request to the real server, the real server also return data through a reverse proxy server. Internal server does not provide external services, so no external IP, while the reverse proxy server requires two network cards, an IP access for external users to use, the other for internal use.

Here Insert Picture Description

Advantages: reverse proxy server is located in the application layer, and a load balancing scheme reverse proxy server is integrated with simple deployment.

Disadvantages: reverse proxy server handles requests and responses for all users, its performance may become a bottleneck server cluster

Note: Nginx is known reverse proxy service

4.IP Load Balancing

Advantage: IP load balancing to complete the process of data distribution in the kernel, the more balanced reverse proxy better processing performance.

Disadvantages: load balancing system bandwidth of the NIC becomes a bottleneck.

The link layer load balancing (LVS)

MAC Address: mac address associated with the card, numbered only with the serial number card manufacturers and related, basically every computer can be used as "identity card." By Ethernet MAC address to reach the corresponding computer card or routing data between frames

Here Insert Picture Description
Load balancing link layer address by modifying MAC frame to packet forwarding purposes. In this way, all the real servers and the load balancing servers have the same IP address, without modifying the destination address and source address of the IP packet, the MAC address by only modifying effect can be achieved, because the IP address and the actual processing of the request the same IP address of the real server, so no return to the load balancing server address swapping can be sent directly respond to give users browser, server load balancing to avoid transmission bottlenecks become possible.

Advantages: Avoid load balancing servers become the bottleneck bandwidth of the NIC, it is the most widely used as a load-balancing tool used by large sites.

Linux in the LVS (Linux Virtual Server) is a link layer load balancing open-source products

The use of routine

DNS load balancing is a geographic level, hardware load balancing corresponds to the cluster level, load balancing software corresponding to the machine level.
Here Insert Picture Description

Original link: https: //blog.csdn.net/bpb_cx/article/details/82771168
description link: https: //www.jianshu.com/p/c48af7936329
description link: https: //blog.csdn.net/JACKO_CHAN / article / details / 81060273

Published 67 original articles · won praise 32 · views 60000 +

Guess you like

Origin blog.csdn.net/weixin_43751710/article/details/104807125