1. What is load balancing
- Load balancer is a device that acts as a reverse proxy to distribute network traffic or applications on a number of servers. Load balancer is used to increase usability of the application and its reliability. To improve the overall performance of the application by reducing the burden on the server related to the management and maintenance of applications and network sessions, as well as by performing application-specific tasks.
- Load balancers are usually divided into two main types: Layer 4 and Layer 7.
- Layer 4 load balancer handles the data found in network layer protocols and transport (IP, TCP, FTP, UDP).
- Layer 7 load balancer distributes requests based on data found in the application layer, protocol layers such as HTTP.
2. Overview of L4 and L7 Load Balancing
- Layer 4 load balancing works at the intermediate layer to the message transport layer and is not relevant to the content of the messages. Transmission Control Protocol (TCP) is the layer 4 protocol for hypertext transfer protocol (HTTP) traffic on the Internet. Layer 4 load balancers simply forward network packets to and from the upstream server without checking the contents of the data packets. You can make limited routing decisions by examining the first few packets in the TCP stream.
- Layer 7 load balancing works in high-end application layers, dealing directly with the actual content of each message. HTTP is the primary layer 7 protocol for regulating web traffic on the Internet. Layer 7 load balancers coordinate traffic in a more sophisticated way than layer 4 load balancers, especially for TCP based on traffic such as HTTP. A Layer 7 load balancer terminates the transport network and reads the message inside. It can determine load balancing based on the content of the message (URL or cookie, etc.). Then create a new TCP connection to the selected upstream server (or reuse if available using the HTTP keepalives method) and make a request to the server.
3. The difference between NAT and DSR
The following image illustrates the difference between NAT and DSR in load balancing:
- It is easy to see that clients (192.0.2.1) connect to the load balancing (LB) on VIP (virtual IP) (192.0.2.253).
- In the case of NAT, the LB NAT connects via server 1 or server 2. The server has LB as the default port, so the replies go out through the LB and then back to the client. IP-wise, the client sees 192.0.2.1:1024 -> 192.0.2.253:80, LB sees 192.0.2.1:1024 -> 192.0.2.253:80 -> 10.0.0.10:80 and the server sees 192.0.2.1:1024 -> 10.0.0.10:80.
- In the case of DSR, LB does not connect to NAT on the IP layer but forwards the packet to the affected server. In this case, the client, LB and server see 192.0.2.1:1024 -> 192.0.2.253:80. Of course, servers that need to accept traffic for VIPs should be configured on a loopback interface (not on a real interface). Now when the server replies, the traffic goes directly from the server through the router to the client and completely ignores LB.
4. Types and methods of load balancing algorithms
Some standard algorithms are as follows:
This method distributes the load on available servers randomly, selects a server via a random number generation, and sends the current connection to it. This method is commonly used and is useful on many balancing loads, except for cases where the server is down.
2. Round robin
Round Robin passes each new connection request to the next server in the system, the end result being even distribution of the connection across the servers. Round Robin works well in most configurations, but it may be better if the device you are balancing is not equivalent in processing speed, connection speed, memory.
3. Weighted round robin
With this method, the number of connections to each machine received over time is proportional to a weight ratio determined on each machine. This is an improvement in Round Robin because you can say “Machine # 3 can handle 2 loads of # 1 and # 2 machines,” and the load balancer sends two requests to # 3 for Each request to the other machine.
4. Least connections
With this method, the system transfers a new connection to the server that has the least number of current connections. The least connection is the best working method in an environment where the servers or other devices you are applying load balancing are likely to be similar. This is a dynamic load balancing method that distributes connections based on different aspects of server real-time performance analysis, such as the number of connections per node or the fastest response time at the node. current time.
5. Least response time
This method selects the server with the least number of active connections and the least average response time. The response time is also known as Time to First Byte, or TTFB, the time interval between sending a request packet to a server and receiving the first reply back. Transport delivery is based on the fastest response time from servers, which allows servers to respond quickly to client requests.
5.1 The Aplication is often used to create Load Balancer in Linux
- In Linux, we can use the Linux virtual server (LVS) as a load balancer, its allowing load balancing of network services like web and mail servers using Layer 4 Switching. This is extremely fast and allows small services to serve 10s or 100s of thousands simultaneously.
- Alternatively, we can use HAProxy, Pound, and Nginx to act as a Layer 7 reverse proxy.
6. Practice with LVS
In this test, I am using the Linux virtual server (LVS) on Ubuntu 12.04 to create a load balancing of two real servers (back end nodes, running Apache)
- Step 1: Install LVS:
- Step 2: Install TCP virtual service on 192.168.1.75 port 80, Use round-robin algorithm. Add 2 nodes running on apache 2.4
- Step 3: Confirm the IP address has been added
- Step 4: Access via CURL command line
- Step 5: Access via Web Browser
It can be seen that the server was converted when the user accessed the web page.