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. The necessity of clustering

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

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, which distributes 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 clusters

  • 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". The CPU, memory and other resources of multiple servers are integrated through dedicated hardware and software to achieve computing capabilities that only large and supercomputers have.
  • For example, "cloud computing", "grid computing" and so on.

Three, load balancing cluster architecture and working mode

Load balancing structure

At the first level, 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.

Analysis of Load Balancing Cluster Working Mode

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

Linux Virtual Server is
a load balancing solution developed for the Linux kernel

  • Founded by Dr. Zhang Wensong in my country in May 1998
  • Official website: http://www.linuxvirtualserver.orgl
  • 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

Five, LVS load scheduling algorithm

1. Round Robin
● Assign the received access requests to each node (real server) in the cluster in turn, and treat each server equally, regardless of the actual number of connections and system load of the server

2. Weighted Round Robin
● Distribute requests according to the weight value set by the scheduler. Nodes with higher weight value will get the task first, and the more requests will be allocated.
● Ensure that the server with strong performance bears more access traffic

3. Least Connections
● Allocate according to the number of connections established by the real server, and prioritize the received access requests to the node with the least number of connections

4. Weighted least connections (Weighted East Connections)
● When the performance difference of server nodes is large, the weight can be automatically adjusted for the real server
● Nodes with higher performance will bear a larger proportion of the active connection load

Six, ipvsadm tool

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

Seven, NAT mode LVS load balancing 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

Environment configuration

Host operating system IP address Tools/software/installation packages
Load balancer CentOS7 Internal network: 192.168.109.7, external network: 12.0.0.1 ipvsadm
NFS server CentOS7 192.168.109.3 rpcbind、nfs-utils
Web node server 1 CentOS7 192.168.109.12 rpcbind、nfs-utils、httpd
Web node server 2 CentOS7 192.168.109.22 rpcbind、nfs-utils、httpd
Client Windows10 12.0.0.10 ——————

Note: The gateway of the load dispatcher can be set to its own IP or not. The gateways of the server and client must be set, and the address is the internal and external gateway addresses of the load dispatcher. (A gateway is required for cross-network access)
Insert picture description here

1. Deploy shared storage (NFS server: 192.168.109.3)

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/zq 192.168.109.0/24(rw,sync)
/opt/test 192.168.109.0/24(rw,sync)

exportfs -rv

Insert picture description here
Insert picture description here
Insert picture description here

2. Configure the node server (192.168.109.12, 192.168.109.22)

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.109.3

Same step for web server 1 and 2
Insert picture description here

Insert picture description here
Insert picture description here

Web node server 1: 192.168.109.12

mount.nfs 192.168.109.3:/opt/test1 /var/www/html
df -h
echo 'this is test1 web!' > /var/www/html/index.html

Insert picture description here
Insert picture description here

Web node server 1: 192.168.109.22

mount.nfs 192.168.109.3:/opt/test2 /var/www/html
df -h
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.109.7, outer gateway ens36: 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.109.0/24 -o ens36 -j SNAT --to-source 12.0.0.1

Insert picture description here
Insert picture description here

2) Load the LVS kernel module

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

Insert picture description here

3) Install the 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
Insert picture description here

4) Configure load distribution strategy (NAT mode only needs to be configured on the server, no special configuration is required for the node server)

#清除原有策略
ipvsadm -C

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

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

Insert picture description here
Insert picture description here

4. Test

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51616026/article/details/113663758