Home  >  Article  >  Operation and Maintenance  >  There are several types of load balancing in linux

There are several types of load balancing in linux

青灯夜游
青灯夜游Original
2023-04-14 11:25:101794browse

Linux has 4 types of load balancing: 1. Layer 2 load balancing (mac), which uses a virtual mac address. External requests for virtual MAC addresses are received by the load balancer and the actual MAC address of the backend is allocated in response; 2. Three-layer load balancing (ip), using virtual IP address, external requests for virtual IP addresses, load balancing allocates the actual back-end IP address response after receiving the request; 3. Four-layer load balancing (tcp), starting from the fourth layer Starting from the "transport layer" layer, use "ip port" to receive requests; 4. Seven-layer load balancing (http).

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

In regular development and operation and maintenance work, load balancing services are often used, and often Four layers of load and seven layers of load will be mentioned. What exactly is layer four load? What is layer seven load? What is the difference between the two?

1. What is load balancing

1)Load Balancing (Load Balance) is built on the existing network structure On top of that, it provides a cheap, effective and transparent method to expand the bandwidth of network devices and servers, increase throughput, enhance network data processing capabilities, and improve network flexibility and availability. Load balancing has two meanings: First, a large number of concurrent access or data traffic is shared to multiple node devices for processing respectively, reducing the time users wait for responses; secondly, a single heavy load operation is shared to multiple nodes. Parallel processing is performed on each node device. After each node device completes the processing, the results are summarized and returned to the user. The system processing capability is greatly improved.

2) Simply speaking, it is : One is to forward a large amount of concurrent processing to multiple back-end nodes for processing, reducing work response time; the other is Forward a single heavy workload to multiple back-end nodes for processing, and then return it to the load balancing center before returning it to the user. Currently, load balancing technology is mostly used to improve the availability and scalability of Internet server programs such as Web servers, FTP servers, and other mission-critical servers.

2. Load balancing classification

The most common four-layer load balancingandseven-layer in development and operation and maintenance Load balancing.

1) Layer 2 load balancing (mac)

The Layer 2 load divided according to the OSI model generally uses a virtual mac address, and the external virtual machine After receiving the MAC address request, the load balancer allocates the actual MAC address response from the backend.

2) Three-layer load balancing (ip)

Generally adopts virtual IP address method. External requests for virtual IP addresses are received by load balancing and allocated to the actual backend. The IP address responds. (That is, forwarding of one IP to one IP, all ports are open)

3) Four-layer load balancing (tcp)

Based on three load balancing, That is, starting from the fourth layer "transport layer", use the "ip port" to receive the request and then forward it to the corresponding machine.

is IP port-based load balancing: based on the three-layer load balancing, by publishing the three-layer IP address (VIP) and then adding the four-layer port number to determine which traffic needs to be load balanced , perform NAT processing on the traffic that needs to be processed, forward it to the backend server, and record which server handles the TCP or UDP traffic. All subsequent traffic for this connection will be forwarded to the same server for processing.
The corresponding load balancer is called a four-layer switch (L4 switch), which mainly analyzes the IP layer and TCP/UDP layer to achieve four-layer load balancing. This type of load balancer does not understand application protocols (such as HTTP/FTP/MySQL, etc.).

The software that implements four-layer load balancing is:
F5: Hardware load balancer, which has good functions but high cost.
lvs: heavyweight four-layer load software
nginx: lightweight four-layer load software, with caching function, more flexible regular expressions
haproxy: simulates four-layer forwarding, more flexible

4) Seven-layer load balancing (http)

Start from the seventh layer "application layer", receive the request according to the virtual url or IP, host name, and then redirect to the corresponding Process server.

is load balancing based on virtual URL or host IP: on the basis of four-layer load balancing (it is absolutely impossible to have seven layers without four layers), and then consider the characteristics of the application layer, such as the same Web server load balancing, in addition to identifying whether traffic needs to be processed based on VIP plus port 80, can also determine whether to perform load balancing based on the seven-layer URL, browser category, and language. For example, if your web server is divided into two groups, one for Chinese language and one for English language, then the seven-layer load balancing can automatically identify the user language when the user accesses your domain name, and then select The corresponding language server group performs load balancing processing.

The corresponding load balancer is called a seven-layer switch (L7 switch). In addition to supporting four-layer load balancing, it also analyzes application layer information, such as HTTP protocol URI or Cookie information, to achieve seven-layer load balancing. This type of load balancer understands application protocols.

The software that implements seven-layer load balancing includes:
haproxy: generates load balancing technology, fully supports seven-layer proxy, session persistence, marking, and path transfer;
nginx: It only has better functions on http protocol and mail protocol, and its performance is similar to haproxy;
apache: poor function
Mysql proxy: its function is acceptable.

Generally speaking, lvs generally does 4-layer load; nginx does 7-layer load (it can also do 4-layer load, through the stream module); haproxy is more flexible, with 4-layer and 7-layer load It can be done in balance.

3. The difference between four-layer and seven-layer load balancing

1) Analysis from the technical principles

The so-called four-layer load balancing, that is, mainly based on the destination address and port in the message, plus the server selection method set by the load balancing device, determines the final selection. Internal server.

Taking common TCP as an example, when the load balancing device receives the first SYN request from the client, it selects the best server through the above method and modifies the target IP address in the message. (Changed to the backend server IP) and forwarded directly to the server. TCP connection establishment, that is, the three-way handshake is established directly between the client and the server, and the load balancing device only acts as a router-like forwarding action. In some deployment situations, in order to ensure that the server return packet can be correctly returned to the load balancing device, the original source address of the packet may be modified while forwarding the packet.

The so-called seven-layer load balancing, also known as "content switching", is mainly through the real content in the message. Meaningful application layer content, coupled with the server selection method set up by the load balancing device, determines the final internal server selection.

Taking common TCP as an example, if the load balancing device wants to select a server based on the real application layer content, it can only accept the connection (three-way handshake) between the final server and the client first. The client sends a message with real application layer content, and then based on the specific fields in the message and the server selection method set by the load balancing device, the final selected internal server is determined. The load balancing device in this case is more similar to a proxy server. The load balancing and front-end clients and back-end servers will establish TCP connections respectively. Therefore, from the perspective of this technical principle, seven-layer load balancing obviously has higher requirements for load balancing equipment, and the ability to handle seven layers will inevitably be lower than the four-layer mode deployment method.

Four-layer load balancing is executed at the intermediate transport layer, which handles the delivery of messages but does not consider the content of the messages. For example, TCP is the layer 4 protocol for Hypertext Transfer Protocol (HTTP) traffic on the network. During this process, Layer 4 load balancing forwards network packets to the upstream server, but does not inspect the contents of the packets and can only make limited routing decisions by examining the first few packets in the TCP stream.

Seven-layer load balancingDifferent from four-layer load balancing, it is executed on the high-level application layer and will process the actual content of each message. HTTP is the primary layer 7 protocol for website traffic on the web. Layer 7 load balancing routes network traffic in a more complex manner than layer 4 load balancing, and is especially suitable for TCP-based traffic (such as HTTP). Layer 7 load balancing terminates network traffic and reads messages in the server. It can make load balancing decisions based on message content (such as URL or cookie). Subsequently, the seven-layer load balancing establishes a new TCP connection with the selected server and writes the request to the server.

In simple terms, the difference between the two

- Seven-layer load balancing is basically based on http protocol, which is suitable for load balancing of web servers. (nginx)
- Four-layer load balancingMainly based on tcp protocol messages, you can do anything based on tcp/ip Load balancing of protocol software. (haproxy, LVS)
- The main difference between the two is that the levels of the messages used are different, and each has its own benefits.
- The benefit of seven-layer application load is to make the entire network more "intelligent." For example, user traffic visiting a website can forward requests for images to a specific image server and use caching technology through the seven-layer approach; requests for text can be forwarded to a specific text server and compression can be used. technology. Of course, this is just a small case of a seven-layer application. From a technical perspective, this method can modify the client's request and the server's response in any sense, greatly improving the flexibility of the application system at the network layer. Many functions deployed in the background, such as Nginx or Apache, can be moved to the load balancing device, such as header rewriting in client requests, keyword filtering or content insertion in server responses, and other functions.
- Layer 4 load balancing is mainly more flexible and can be used as a load balancer for a variety of software.

Let’s give an example to illustrate vividly: the four-layer load balancing is like the bank’s self-service queuing machine. Each customer who arrives at the bank selects the corresponding window to receive services according to the order of the queuing machine; while the seven-layer load balancing Qian Qing is like a bank lobby manager who first confirms the business that the customer needs to handle, and then arranges the number. In this way, customers who handle financial management, deposits and withdrawals, etc. will be coordinated and handled according to the bank's internal resources, speeding up the customer business process.

Benefits of seven-layer load balancing Seven-layer load balancing is more efficient than packet-based four-layer load balancing Takes up CPU, but rarely causes server performance degradation. Seven-layer load balancing allows the load balancer to make more informed decisions and make optimizations and changes to content such as compression, encryption, and more. Layer 7 load balancing can also use buffering to offload slow connections from upstream servers, thereby improving performance.

The component that performs Layer 7 load balancing is often called a reverse proxy server.

Seven-layer load balancing example As a simple example, suppose a user visits a high-traffic website. During the session, it may request Static content (such as images or videos), dynamic content (such as a news feed), or transactional information (such as order status), etc. Layer 7 load balancing allows the load balancer to route requests based on messages in the request itself, such as the content type. That is, requests for an image or video can be routed to a server that stores it and is highly optimized to serve multimedia content; requests for transactional information, such as discounted prices, can be routed to an application server responsible for managing pricing. With Layer 7 load balancing, network and application architects can create highly optimized server infrastructure or application delivery networks that scale efficiently while ensuring reliability.

Simple summary From the above comparison, it seems that the biggest difference between four-layer load and seven-layer load is the difference in efficiency and functionality. The four-layer load architecture design is relatively simple, without the need to parse specific message content, and will have relatively high network throughput and processing capabilities. The advantages of seven-layer load balancing are reflected in its multiple functions and flexible and powerful control. When designing a specific business architecture, the use of seven-layer or four-layer loads must be considered based on specific circumstances.

The data flow during load balancing all passes through the load balancer. How to solve the problem of the load balancer becoming a bottleneck?

By modifying the source address and destination address of the TCP message, the data returned from the web server is directly returned to the client. This is something that seven-layer load balancing cannot do because of the TCP three-way handshake. Established between the client and the load balancing server, the http protocol is based on the tcp protocol. After the tcp link is established, the http message is transmitted. Receiving the http message indicates that the load balancer and the client have established a tcp connection, and the web server and The client's tcp connection has not been established. How to return data to the client? The above method will cause problems: all hosts in the cluster have internal IP addresses and cannot communicate with the outside world.

Solution 1: If you can buy so many external IP addresses for use, then load balance them to the real ones when the tcp link is to be established. The web server allows the client and server to establish a TCP link.

Solution 2:
Quote: All computer problems can be solved by establishing a virtual layer.
You can virtualize all server host IPs into load balancing server IPs, so that all hosts in the server cluster can access the external network. Because the IP addresses (network layer, three layers) are the same, they can only pass through the second layer to identify the data flow direction, modify the MAC address of the destination host at the data link layer (layer 2), so that the request is sent to the web server, and then the tcp connection is actually established. Then, because the web server can connect to the Internet, it can directly return data to Client

2) Analyze the requirements of application scenarios

The benefits of seven-layer application load are to make the entire network more "intelligent" ". For example, user traffic visiting a website can forward requests for images to a specific image server and use caching technology through the seven-layer approach; requests for text can be forwarded to a specific text server and compression can be used. technology. Of course, this is just a small case of a seven-layer application. From a technical perspective, this method can modify the client's request and the server's response in any sense, greatly improving the flexibility of the application system at the network layer. Many functions deployed in the background, such as Nginx or Apache, can be moved to the load balancing device, such as header rewriting in client requests, keyword filtering or content insertion in server responses, and other functions.

Another feature that is often mentioned is security. The most common SYN Flood attack in the network is that hackers control many source clients and use false IP addresses to send SYN attacks to the same target. Usually, this attack will send a large number of SYN messages and exhaust related resources on the server to achieve Denial. of Service (DoS) purpose. It can also be seen from the technical principles that in the four-layer mode, these SYN attacks will be forwarded to the back-end server; in the seven-layer mode, these SYN attacks will naturally end on the load balancing device and will not affect the normal operation of the back-end server. . In addition, the load balancing device can set multiple policies at the seven-layer level to filter specific messages, such as SQL Injection and other application-level attack methods, to further improve the overall system security from the application level.

The current seven-layer load balancing mainly focuses on the application of HTTP protocol, so its application scope is mainly systems developed based on B/S such as numerous websites or internal information platforms. Layer 4 load balancing corresponds to other TCP applications, such as ERP and other systems developed based on C/S.

3) Issues to consider in seven-layer applications

3.1) Is it really necessary. Seven-layer applications can indeed improve traffic intelligence, but at the same time inevitably bring about problems such as complex device configuration, increased load balancing pressure, and complexity in troubleshooting. When designing the system, it is necessary to consider the mixed situation of simultaneous application of four layers and seven layers.

3.2) Whether security can really be improved. For example, the SYN Flood attack, the seven-layer mode does block these traffic from the server, but the load balancing device itself must have strong anti-DDoS capabilities, otherwise even if the server is normal and the load balancing device used as the central scheduling failure will cause the entire application to collapse.

3.3) Is there enough flexibility? The advantage of seven-layer applications is that they can make the traffic of the entire application intelligent, but the load balancing device needs to provide complete seven-layer functions to meet customers' application-based scheduling according to different situations. The simplest assessment is whether it can replace the scheduling function on the backend server such as Nginx or Apache. A load balancing device that can provide a seven-layer application development interface allows customers to set functions according to their needs, making it truly possible to provide powerful flexibility and intelligence.

4) Overall comparison

4.1) Intelligence
Layer 7 load balancing has all the functions of layer 7 of OIS, so in It can be more flexible in handling user needs. In theory, the seven-layer model can modify all user requests to the server. For example, add information to the file header and classify and forward according to different file types. The four-layer model only supports demand forwarding based on the network layer and cannot modify the content of user requests.

4.2) Security
Since the seven-layer load balancing has all the functions of the OSI model, it can more easily resist attacks from the network; in principle, the four-layer model will directly Forwarding the user's request to the backend node cannot directly resist network attacks.

4.3) Complexity
The four-layer model is generally a relatively simple architecture, easy to manage, and easy to locate problems; the seven-layer model architecture is more complex, and it is usually necessary to consider combining the four-layer model In mixed use cases, problem locating is more complicated.

4.4) Efficiency ratio
The four-layer model is based on lower-level settings and is usually more efficient, but has a limited application scope; the seven-layer model requires more resource consumption and is theoretically more efficient than The four-layer model has stronger functions, and the current implementation is more based on http applications.

4. Load balancing technical solution description

1) Software/hardware load balancing

Software load balancing solution refers to installing one or more additional software on the corresponding operating system of one or more servers to achieve load balancing, such as DNS Load Balance, CheckPoint Firewall-1 ConnectControl, Keepalive ipvs, etc. It The advantages are based on a specific environment, simple configuration, flexible use, low cost, and can meet general load balancing needs. Software solutions also have many disadvantages, because installing additional software on each server will consume an unlimited amount of system resources. The more powerful the module, the more it will be consumed. Therefore, when the connection request is particularly large, The software itself will become a key to the success or failure of the server; the scalability of the software is not very good and is limited by the operating system; bugs in the operating system itself often cause security issues.

Hardware load balancing solution is to install a load balancing device directly between the server and the external network. This device is usually a piece of hardware independent of the system, which is called load balancing. device. Since specialized equipment completes specialized tasks and is independent of the operating system, the overall performance has been greatly improved. Coupled with diversified load balancing strategies and intelligent traffic management, optimal load balancing requirements can be achieved. Load balancers come in various forms. In addition to being independent load balancers, some load balancers are integrated into switching devices and placed between the server and the Internet link. Some load balancers use two network adapters to connect this. The functions are integrated into the PC, one is connected to the Internet, and the other is connected to the internal network of the back-end server farm.

Comparison between software load balancing and hardware load balancing:

The advantages of software load balancing are clear demand environment, simple configuration, and flexible operation. It is low cost, not efficient, and can meet the needs of ordinary enterprises; the disadvantage is that it depends on the system and increases resource overhead; the quality of the software determines the performance of the environment; the security of the system and the stability of the software will affect the security of the entire environment.

The advantage of hardware load balancing is that it is independent of the system, the overall performance is greatly improved, and it is superior to the software method in terms of function and performance; intelligent traffic management, multiple strategies are optional, and can achieve the best load balancing effect; The disadvantage is that it is expensive.

2) Local/global load balancing

Load balancing is divided into Local Load Balance(Local Load Balance) based on the geographical structure of its application AndGlobal Load Balance (Global Load Balance, also called regional load balancing), local load balancing refers to load balancing the local server group, global load balancing refers to the load balancing of servers placed in different geographical locations. Load balancing among server groups with different network structures.

Local Load Balancing can effectively solve the problems of excessive data traffic and overloaded network, and there is no need to spend expensive expenses to purchase servers with excellent performance, make full use of existing equipment, and avoid A server single point of failure causes loss of data traffic. It has flexible and diverse balancing strategies to reasonably allocate data traffic to the servers in the server group for shared burden. Even if you expand and upgrade existing servers, you can simply add a new server to the service group without changing the existing network structure or stopping existing services.

Global Load Balancing is mainly used for sites that have their own servers in a multi-region. In order to enable global users to access the server closest to them with only one IP address or domain name, thus To obtain the fastest access speed, it can also be used by large companies with scattered subsidiaries and widely distributed sites to achieve unified and reasonable allocation of resources through the Intranet (internal Internet).

3) Load balancing at the network level

According to different bottlenecks where the network is overloaded, starting from different levels of the network, corresponding load balancing can be used technology to solve existing problems.

As bandwidth increases and data traffic continues to increase, the data interface in the core part of the network will face bottleneck problems. The original single line will be difficult to meet the demand, and line upgrades are too expensive or even difficult to achieve. At this time, you can consider using link aggregation (Trunking) technology.

Link aggregation technology (Layer 2 load balancing) uses multiple physical links as a single aggregated logical link. Network data traffic is shared by all physical links in the aggregated logical link. This logically increases the capacity of the link so that it can meet the increased demand for bandwidth.

Modern load balancing technology usually operates on the fourth or seventh layer of the network. Layer 4 load balancing maps a legally registered IP address on the Internet to the IP addresses of multiple internal servers, and dynamically uses one of the internal IP addresses for each TCP connection request to achieve load balancing. In layer 4 switches, this balancing technology is widely used. A destination address is a server group VIP (Virtual IP address) connection request data packet flows through the switch. The switch determines the source and destination IP addresses, TCP Or UDP port number and certain load balancing strategy, mapping between server IP and VIP, and selecting the best server in the server group to handle the connection request.

Seven-layer load balancing controls the content of application layer services, provides a high-level control method for access traffic, and is suitable for applications in HTTP server groups. Layer 7 load balancing technology checks the passing HTTP headers and performs load balancing tasks based on the information in the headers.

The advantages of seven-layer load balancing are shown in the following aspects:

1) By checking the HTTP header, HTTP400, 500 and 600 series errors can be detected information, thereby transparently redirecting connection requests to another server and avoiding application layer failures.
2) According to the type of data flowing through (such as determining whether the data packet is an image file, compressed file or multimedia file format, etc.), the data traffic can be directed to the server of the corresponding content for processing, increasing system performance.
3) According to the type of connection request, whether it is a static document request such as ordinary text or image, or a dynamic document request such as asp, cgi, etc., the corresponding request can be directed to the corresponding server for processing, improving the performance of the system and safety.

The shortcomings of seven-layer load balancing are reflected in the following aspects:

1) Seven-layer load balancing is limited by the protocols it supports (generally only HTTP), so This limits the breadth of its application.
2) The seven-layer load balancing check of HTTP headers will occupy a large amount of system resources and will inevitably affect the performance of the system. In the case of a large number of connection requests, the load balancing device itself can easily become a bottleneck of the overall network performance.

5. Load balancing strategy

In actual applications, you may not want to just distribute client service requests evenly to internal servers, regardless of whether the server is down. . Instead, we want the Pentium III server to accept more service requests than Pentium II. A server that handles fewer service requests can be assigned more service requests. A failed server will no longer accept service requests until the failure is restored, etc. wait. Choose an appropriate load balancing strategy so that multiple devices can complete tasks together and eliminate or avoid existing bottlenecks caused by uneven network load distribution, data traffic congestion, and long response times. In each load balancing method, there are corresponding load balancing strategies for load balancing at layers 2, 3, 4, and 7 of the OSI reference model based on different application requirements.

The advantages and disadvantages of load balancing strategies and the ease of implementation have two key factors: load balancing algorithm; method and ability to detect network system conditions.
Load balancing algorithm
1) Round Robin : Each request from the network is assigned to the internal server in turn, from 1 to N and then starts again. This balancing algorithm is suitable for situations where all servers in the server group have the same hardware and software configuration and the average service requests are relatively balanced.
2) Weighted Round Robin : According to the different processing capabilities of the server, different weights are assigned to each server so that it can accept service requests with corresponding weights. For example: the weight of server A is designed to be 1, the weight of B is 3, and the weight of C is 6, then servers A, B, and C will receive 10%, 30%, and 60% of service requests respectively. This balancing algorithm ensures that high-performance servers receive more utilization and prevents low-performance servers from being overloaded.
3) Random Balance (Random) : Randomly distribute requests from the network to multiple internal servers.
4) Weighted Random Balancing (Weighted Random): This balancing algorithm is similar to the weighted round-robin algorithm, but it is a random selection process when processing request sharing.
5) Response speed balancing (Response Time): The load balancing device sends a detection request (such as Ping) to each internal server, and then based on the fastest response time of each internal server to the detection request. Decide which server to respond to the client's service request. This balancing algorithm can better reflect the current running status of the server, but the fastest response time only refers to the fastest response time between the load balancing device and the server, not the fastest response time between the client and the server.
6) Least Connection Balance : The time that each client request service stays on the server may have a large difference. As the working time lengthens, if a simple round robin is used, Using circular or random balancing algorithms, the connection process on each server may be greatly different, and true load balancing is not achieved. The minimum number of connections balancing algorithm has a data record for each server that needs to be loaded internally, recording the number of connections currently being processed by the server. When there is a new service connection request, the current request will be assigned to the server with the least number of connections. The server makes the balance more consistent with the actual situation and the load is more balanced. This balancing algorithm is suitable for long-term processing request services, such as FTP.
7) Processing capacity balancing: This balancing algorithm will allocate service requests to the internal processing load (converted based on the server CPU model, number of CPUs, memory size, current number of connections, etc.) For lightweight servers, this balancing algorithm is relatively more accurate because it takes into account the processing power of the internal server and the current network operating conditions, especially when applied to layer 7 (application layer) load balancing.
8) DNS response balancing (Flash DNS) : On the Internet, whether it is HTTP, FTP or other service requests, the client usually finds the exact IP address of the server through domain name resolution. of. Under this balancing algorithm, load balancing devices located in different geographical locations receive a domain name resolution request from the same client, and resolve the domain name into the IP address of their corresponding server (that is, the load balancing device) at the same time. The IP address of the server in the same geographical location) and returns it to the client, the client will continue to request services with the first received domain name resolution IP address, and ignore other IP address responses. When this balancing strategy is suitable for global load balancing, it is meaningless for local load balancing.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of There are several types of load balancing in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does linux mem mean?Next article:What does linux mem mean?