A load balancing cluster, as the name suggests, is to let multiple servers share the pressure. Load balancing is an important module required for enterprise-level large-scale projects.
The current excellent open source software for implementing load balancing clusters include LVS, Nginx, haproxy, keepalived, etc., while excellent commercial load balancing devices include F5, NetScaler, etc. Commercial load balancing equipment is stable, but expensive.
Next, let's introduce the load balancing software LVS.
1. Introduction to LVS
LVS (Linux Virtual Server) is an open source load balancing software developed by domestic Daniel Zhang Wensong. It is a four-layer load balancing software, that is, the forwarding and routing of the TCP/IP protocol (IP+port) based on the fourth-layer transport layer of the OSI model. High stability and efficiency.
The LVS architecture is shown in the figure. The core role is the scheduler LB (Load Blancer), which is used to distribute user requests; and the remaining real server RS (Real Server), which is used to process user requests.
The LB is the gateway server, and the public IP is used for external services. It is also called VIP (Virtual IP) in the cluster, that is, virtual IP, or floating IP. The server relies on this IP to provide services to the outside world, but this IP is not the IP of the real server RS that actually handles user requests, so it is called a virtual IP.
Two, three LVS modes
LVS can be divided into three types according to different implementation methods: NAT mode, IP Tunnel (IP tunnel) mode, and DR mode.
(1) NAT mode
NAT (Network Address Translation, Network Address Translation), a solution to solve the problem of insufficient ipv4 addresses. The main idea is to divide the network into public and private networks, a LAN is controlled by a gateway, and the NAT gateway has two network cards, the public IP and the intranet IP, so that the hosts of a LAN only need to use one public IP, reducing resource occupation.
The NAT mode of LVS uses the NAT standard, and the scheduler LB is a NAT gateway, which forwards the user's request to the back-end RS through the preset iptables rules using the switch/router . The LB has two IPs, one is the public IP and the other is the internal IP, while the RS has only the internal IP. The user access address is the public IP of the LB. After receiving the user request, the LB will use the scheduling algorithm to calculate the request and forward it to the RS in the intranet. The source and destination addresses are forwarded through the intranet IP of the LB and the RS.
The advantage of this mode is to save public network IP, but the performance of LB is the bottleneck.
The architecture is shown in the figure.
(2) IP Tunnel mode
(3) DR mode
The principle is the same as the Tunnel mode. However, instead of using tunneling technology and IP encapsulation, the MAC address of the data frame is changed to the MAC address of the RS. The LB and RS must be in the same network segment before they can be directly forwarded through the MAC address. Finally, the RS directly returns the response result to the user.
The architecture is shown in the figure.
3. Summary
NAT mode is suitable for small server clusters, and has the advantage of saving public IP. The difference between TUN and DR is similar, and both can support large-scale clusters, but the disadvantage is that the public network IP is wasted.