Introduction to load Balancing clusters
? Mainstream open source software LVs, keepalived, Haproxy, Nginx, etc.
? where LVs belongs to Layer 4 (Network OSI 7 layer model), Nginx belongs to 7 layer, Haproxy can be considered as 4 layer, also can be used as 7 layer
The Keepalived load balancing function is actually LVS.
? LVS this 4-tier load balancer can distribute other ports except 80, such as MySQL, while Nginx supports only Http,https,mail,haproxy and MySQL.
In comparison, LVS is more stable than 4 layers, can withstand more requests, and nginx is more flexible in 7 layers, which can achieve more individualized requirements.
LVS Introduction
? LVS is developed by Chinese Zhangwensong
HTTPd is as popular as Apache, with TCP/IP-based routing and forwarding, high stability and efficiency
? The latest version of LVS is based on Linux kernel 2.6 and has not been updated for many years
? LVS has three common patterns: NAT, DR, IP Tunnel
? A core role in the LVS architecture is called the Dispatcher (Load balance), which is used to distribute the user's requests, as well as many servers that handle user requests (Real server, RS)
Nat Mode
? This mode uses the Iptables NAT table to achieve
After the user's request to the dispatcher, the requested packet is forwarded to the back-end RS via a preset iptables rule.
? RS needs to set the gateway as the Distributor's intranet IP
? The data packets that are requested by the user and the packets returned to the user are all passed through the dispatcher, so the dispatcher becomes the bottleneck
In NAT mode, only the Distributor has a public IP, so it is more economical to save public IP resources.
IP tunnel mode
? This mode requires a common IP configuration on the Distributor and all RS, we call it VIP
The target IP of the client request is VIP, and after the dispatcher receives the request packet, it will make a processing of the packet and change the target IP to RS IP so that the packet is on the RS
When the RS receives the packet, it restores the original packet so that the target IP is the VIP, because the VIP is configured on all RS, so it will consider itself
LVS Dr Mode
? This mode also requires a common IP configuration on the Distributor and all RS, which is the VIP
Unlike IP tunnel, it modifies the MAC address of the packet to the MAC address of the RS.
When the RS receives the packet, it restores the original packet so that the target IP is the VIP, because the VIP is configured on all RS, so it will consider itself
The scheduling algorithm of LVS
Poll Round-robin RR
? Weighted polling Weight Round-robin WRR
? Minimum connection Least-connection LC
? weighted minimum connection Weight least-connection WLC
? Minimum connection based on locality locality-based Least Connections LBLC
? Local minimum connection with replication locality-based Least Connections with Replication LBLCR
? target Address Hash dispatch Destination Hashing DH
? Source Address Hash dispatch source Hashing sh
LVS NAT Mode Setup
Prepare 3 Machines
A machine is a dispatcher, also called a scheduler (dir)
A machine 192.168.177.7 external network address is 192.168.153.55
B Machine 192.168.177.100
C Machine 192.168.177.200
b machine and C machine gateways need to be set to 192.168.177.7
Shut down the firewall, three machines execute the following command
systemctl stop firewalldsystemc disable firewalldsystemctl start iptablesiptables -Fservice iptables savesetenforce 0
Installing Ipvsdam on the Distributor
yum install -y ipvsdam
Editing scripts on the dispenser
vim /usr/local/sbin/lvs_nat.sh
Add the following "
#!/bin/bash
Turn on routing forwarding on #director server
Echo 1 >/proc/sys/net/ipv4/ip_forward
#关闭icmp的重定向
Echo 0 >/proc/sys/net/ipv4/conf/all/send_redirects
Echo 0 >/proc/sys/net/ipv4/conf/default/send_ Redirects
#注意区分网卡名字, my two NICs are ens33 and ENS37
Echo 0 >/proc/sys/net/ipv4/conf/ens33/send_redirects
Echo 0 >/proc/sys/net/ipv4/conf/ens37/send_redirects
#director set NAT firewall
iptables-t nat-f
iptables-t nat-x
Iptables-t nat-a postrouting-s 192.168.177.0/24-j Masquerade
#director设置ipvsadm
ipvsadm= '/usr/sbin/ Ipvsadm '
$IPVSADM-C
$IPVSADM-A-T 192.168.153.55:80-s wlc-p 3
$IPVSADM-T 192.168.153.55:80-r 192.168.177 .100:80-m-W 1
$IPVSADM-T 192.168.153.55:80-r 192.168.177.200:80-m-W 1
Both RS are required to install RS, and set two RS home page, make a distinction, that is to say directly curl two RS IP, get different results
This is the result of my experiment, the successful distribution
[Email protected] yang]# Curl 192.168.153.55
Backup backup this is backup.
[Email protected] yang]# Curl 192.168.153.55
Master Master This is master
[Email protected] yang]# Curl 192.168.153.55
Backup backup this is backup.
[Email protected] yang]# Curl 192.168.153.55
Master Master This is master
[Email protected] yang]# Curl 192.168.153.55
Backup backup this is backup.
Load Balancing cluster Introduction, LVS Introduction, LVS scheduling algorithm, LVS NAT mode construction