Efficient and versatile cluster LVS load balancing cluster (NAT mode)

One, cluster

1. The meaning of cluster

Cluster: Also called a cluster. It is composed of multiple hosts, but only appears as a whole externally, and only provides one access entry (domain name or IP), which is equivalent to a large computer.

2. Why are there clusters

Problems that have arisen In
Internet applications, as sites have increasingly higher requirements for hardware performance, response speed, service stability, and data reliability, a single server cannot meet the requirements for load balancing and high availability.

Solution Method
1: Use expensive minicomputers and mainframes. (High cost)
Method 2: Use multiple relatively inexpensive ordinary servers to build a service cluster. (Low cost)

Results
Most companies choose method two to integrate multiple servers, use LVS to achieve high server availability and load balancing, and provide the same services externally from the same IP address. And this is a cluster technology commonly used in enterprises-LVS (Linux Virtual Server, Linux Virtual Server).

Second, the classification of clusters

According to the target difference of the cluster, it can be divided into the following three types

1. Load balancing cluster

  • Improve the responsiveness of the application system, handle as many access requests as possible, reduce latency as the goal, and obtain high concurrency and high load (LB) overall performance.
  • The load distribution of LB relies on the offloading algorithm of the master node to share the access requests from the client to multiple server nodes, thereby alleviating the load of the entire system.
  • For example: "DNS polling", "reverse proxy", etc.

2. Highly available cluster

  • The goal is to improve the reliability of the application system, reduce the interruption time as much as possible, ensure the continuity of the service, and achieve the fault tolerance effect of high availability (HA).
  • The working mode of HA includes duplex and master-slave modes. Duplex means that all nodes are online at the same time; master-slave means that only the master node is online, but the slave node can automatically switch to the master node when a failure occurs.
  • For example: "Failover", "Dual machine hot standby", etc.

3. High performance cluster

  • With the goal of increasing the CPU computing speed of the application system, expanding hardware resources and analysis capabilities, it will obtain the equivalent of large-scale, supercomputer high-performance computing (HPC) capabilities.
  • High performance relies on "distributed computing" and "parallel computing". Through dedicated hardware and software, the CPU, memory and other resources of multiple servers are integrated to achieve computing capabilities that only large and supercomputers have.
  • For example, "cloud computing", "grid computing" and so on.

Supplement: Among the three types, load balancing clusters and high availability clusters are usually used in combination.

Three, load balancing cluster architecture and working mode

1. Load balancing structure

At the first layer, the load scheduler (Load Balancer or Director)
accesses the unique entrance of the entire cluster system, and uses the VIP address common to all servers, also known as the cluster IP address. Usually two schedulers, the main and backup schedulers, are configured to achieve hot backup. When the main scheduler fails, it can be smoothly replaced to the backup scheduler to ensure high availability.

The second layer,
the application services provided by the server pool (Server Pool) cluster are borne by the server pool, where each node has an independent RIP address (real IP), and only processes client requests distributed by the scheduler. When a node fails temporarily, the fault-tolerant mechanism of the load scheduler will isolate it and wait for the error to be eliminated before re-entering the server pool.

The third layer, shared storage (Share Storage)
provides stable and consistent file access services for all nodes in the server pool to ensure the unity of the entire cluster. Shared storage can use NAS devices or provide dedicated servers for NFS sharing services.
Insert picture description here

2. Analysis of load balancing cluster working mode

  • Load balancing cluster is currently the most commonly used cluster type in enterprises
  • The cluster load scheduling technology has three working modes, the modes are as follows

1) NAT mode-address translation

  • Network Address Translation, referred to as NAT mode
  • Similar to the private network structure of a firewall, the load scheduler serves as the gateway of all server nodes, that is, as the access entrance of the client, and also the access exit of each node in response to the client
  • The server node uses a private IP address and is located on the same physical network as the load scheduler. The security is better than the other two methods

2) TUN mode------IP tunnel

  • IP Tunnel, referred to as TUN mode
  • Adopting an open network structure, the load scheduler only serves as the client's access portal, and each node directly responds to the client through its own Internet connection, instead of going through the load scheduler
  • The server nodes are scattered in different locations in the Internet, have independent public IP addresses, and communicate with the load scheduler through a dedicated IP tunnel

3) DR mode-direct routing

  • Direct Routing, referred to as DR mode
  • It adopts a semi-open network structure, which is similar to the structure of the TUN model, but the nodes are not scattered everywhere, but are located on the same physical network as the scheduler
  • The load scheduler is connected to each node server through the local network, without the need to establish a dedicated IP tunnel

Four, LVS virtual server

1、Linux Virtual Server

  • Load balancing solution developed for Linux kernel
  • Founded by Dr. Zhang Wensong in China in May 1998
  • Official website: http://www.linuxvirtualserver.org/
  • LVS is actually equivalent to a virtualized application based on IP address, and proposes an efficient solution for load balancing based on IP address and content request distribution
  • LVS has now become a part of the Linux kernel, compiled as an ip_ vs module by default , and can be called automatically when necessary. In the CentOS 7 system, the following operations can manually load the ip_ vs module and view the version information of the ip_ vs module in the current system.
#加载ip_va模块
modprobe ip_vs
#确认内核对LVS的支持
cat /proc/net/ip_vs

Insert picture description here

  • To use the capabilities of LVS, you only need to install an LVS management tool: ipvsadm.

ipvsadm function and option description

Options Features
-A Add virtual server
-D Delete the entire virtual server
-s Specify load scheduling algorithm (polling: rr, weighted round-robin: wrr, least connection: lc, weighted least connection: wlc)
-a Indicates to add a real server (node ​​server)
-d Delete a node
-t Specify VIP address and TCP port
-r Specify RIP address and TCP port
-m Indicates the use of NAT cluster mode
-g Indicates the use of DR mode
-i Means to use TUN mode
-w Set the weight (when the weight is 0, the node is suspended)
-p 60 Means to keep a long connection for 60 seconds
-l List LVS virtual servers (default is to view all)
-n Display address, port and other information in digital form, often used in combination with the "-l" option. ipvsadm -ln

2. LVS load scheduling algorithm

1) Round Robin

  • The received access requests are distributed to each node (real server) in the cluster in turn in order, and each server is treated equally, regardless of the actual number of connections and system load of the server

2) Weighted Round Robin (Weighted Round Robin)

  • Distribute requests according to the weight value set by the scheduler. The node with the higher weight value will get the task first, and the more requests are allocated
  • Ensure that the server with strong performance bears more access traffic

3) Least Connections

  • Allocation is based on the number of connections established by the real server, and the received access requests are prioritized to the node with the least number of connections

4) Weighted Least Connections (Weighted L east Connections)

  • When the performance of server nodes differ greatly, the weight can be automatically adjusted for the real server
  • Nodes with higher performance will bear a greater proportion of active connection load

Five, LVS load balancing NAT mode cluster deployment

The LVS scheduler is used as the gateway of the Web server pool, LVS has two network cards, respectively connected to the internal and external networks, using the round-robin (rr) scheduling algorithm
Insert picture description here

Environment configuration

Host operating system IP address Service required
Load scheduler CentOS7 Internal gateway: 192.168.163.10
External gateway: 12.0.0.1
ipvsadm
NFS server CentOS7 192.168.163.11 rpcbind、nfs-utils
Web node server 1 CentOS7 192.168.163.12 rpcbind、nfs-utils、httpd
Web node server 2 CentOS7 192.168.163.13 rpcbind、nfs-utils、httpd
Client Windows10 12.0.0.10

The gateways of the server and client must be set, and the address is the internal and external gateway addresses of the load scheduler. (A gateway is required for cross-network access)

1. Deploy shared storage

NFS server: 192.168.163.11

systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0

yum -y install nfs-utils rpcbind

systemctl start rpcbind.service
systemctl start nfs.service

systemctl enable nfs.service
systemctl enable rpcbind.service

mkdir /opt/test1
mkdir /opt/test2

chmod 777 /opt/test1
chmod 777 /opt/test2

vim /etc/exports
/opt/test1 192.168.163.0/24(rw,sync)
/opt/test2 192.168.163.0/24(rw,sync)

exportfs -rv

Insert picture description here

2. Configure the node server

Web node server 1: 192.168.163.12
Web node server 2: 192.168.163.13
Both need to install the service, the steps are the same, put it together here

systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0

yum install httpd -y
systemctl start httpd.service
systemctl enable httpd.service

yum -y install nfs-utils rpcbind

systemctl start rpcbind
systemctl enable rpcbind

showmount -e 192.168.163.11

Insert picture description here

Web node server 1: 192.168.163.12

mount.nfs 192.168.163.11:/opt/test1 /var/www/html
echo 'this is test1 web!' > /var/www/html/index.html

Insert picture description here

Web node server 2: 192.168.163.13

mount.nfs 192.168.163.11:/opt/test2 /var/www/html
echo 'this is test2 web!' > /var/www/html/index.html

Insert picture description here

3. Configure the load scheduler

Load scheduler: inner gateway ens33: 192.168.163.10, outer gateway ens37: 12.0.0.1

systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0

Insert picture description here

(1) Configure SNAT forwarding rules

vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
或
echo '1' > /proc/sys/net/ipv4/ip_forward
sysctl -p

iptables -t nat -F
iptables -F
iptables -t nat -A POSTROUTING -s 192.168.163.0/24 -o ens37 -j SNAT --to-source 12.0.0.1

Insert picture description here

(2) Load LVS kernel module

#加载 ip_vs模块
modprobe ip_vs
#查看 ip_vs版本信息
cat /proc/net/ip_vs

Insert picture description here

(3) Install ipvsadm management tool

yum -y install ipvsadm

#启动服务前须保存负载分配策略
ipvsadm-save > /etc/sysconfig/ipvsadm
或
ipvsadm --save > /etc/sysconfig/ipvsadm

systemctl start ipvsadm.service

Insert picture description here

(4) Configure the load distribution strategy (the NAT mode only needs to be configured on the server, and the node server does not require special configuration)

#清除原有策略
ipvsadm -C

ipvsadm -A -t 12.0.0.1:80 -s rr
ipvsadm -a -t 12.0.0.1:80 -r 192.168.163.12:80 -m
ipvsadm -a -t 12.0.0.1:80 -r 192.168.163.13:80 -m
#启用策略
ipvsadm

#查看节点状态,Masq代表 NAT模式
ipvsadm -ln
#保存策略
ipvsadm-save > /etc/sysconfig/ipvsadm

Insert picture description here

4. Test results

A client with an IP of 12.0.0.10 uses a browser to visit http://12.0.0.1/, and constantly refresh the browser to test the load balancing effect. The refresh interval needs to be longer.
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51326240/article/details/113131214