First, load Balancing cluster introduction
Implement load Balancing mainstream open source software LVs, keepalived, Haproxy, nginx, etc.
1), wherein LVS belongs to 4 layer (network OSI 7 layer model), Nginx belongs to 7 layer, Haproxy can be considered as 4 layer, also can be used as 7 layer
2), keepalived load balancing function is actually the LVS
3), LVS this 4-layer load balancer can be distributed in addition to 80 other ports communication, such as MySQL, and nginx only support Http,https,mail,haproxy also support MySQL this
4), compared to the 4 layer of LVS more stable, can withstand more requests, and nginx this 7-layer more flexible, to achieve more personalized requirements
Ii. introduction of LVS
1), LVS is developed by the Chinese Zhangwensong, is the world famous software, the popularity of as much as Apache httpd, based on TCP/IP Routing and forwarding, stability and high efficiency
2), the latest version of LVS based on Linux kernel 2.6, has not been updated for many years
3), LVS has three common patterns: NAT, DR, IP Tunnel
4), there is a core role in the LVS architecture called the Dispatcher (Load balance), it is used to distribute the user's request, there are many server processing user requests (Real server, referred to as RS)
5), LVS NAT mode
This mode is implemented with iptables NAT table, after the user's request to the dispatcher, through the preset iptables rules, forwarding the requested packet to the back-end RS, 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 dispenser becomes the bottleneck, usually only 10 machines left;
In NAT mode, only the Distributor has a public IP, so it is more economical to save public IP resources.
6), LVS IP tunnel mode
This mode requires a common IP configuration on the Distributor and all RS, we call it VIP
The target IP requested by the client is the VIP, and after the dispatcher receives the request packet, the packet is processed and the target IP is changed to the IP of Rs so that the packet is on the RS
After 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
7. 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.
After 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 be considered as its own.
Three, LVS scheduling algorithm
1), poll Round-robin RR
The user requests to come over, it distributes the request evenly to the RS.
2), weighted polling Weight round-robin WRR
One more weight than RR, the higher the weight, the more the number of requests are allocated.
3), Minimum connection least-connection LC
Send the new request to the minimum connected Rs.
4), weighted minimum connection Weight least-connection WLC
5), minimum connection based on locality locality-based Least Connections Lblcnat Mode construction – Prep work
6), with replication based on local minimum connection locality-based Least Connections with Replication LBLCR
7), Target address hash dispatch Destination Hashing DH
8), source address hash dispatch source Hashing sh
Four, LVS Natnat mode construction
1), preparatory work
Three machines
Dispatcher, also known as the Scheduler (dir)
Three machines
Dispatcher, also known as the Scheduler (dir)
Intranet: 1.31, Extranet: 189.89 (VMware Host-only mode)
Rs1
Intranet: 1.12, set gateway to 1.2
Rs2
Intranet: 1.29, set Gateway to 1.2
Execute on all three machines
Systemctl Stop Firewalld; SYSTEMC Disable FIREWALLD
Systemctl start iptables-services; Iptables-f; Service Iptables Save
2), install Ipvsadm on dir
Yum Install-y Ipvsdam
To write a script on Dir, the vim/usr/local/sbin/lvs_nat.sh//content is as follows
#! /bin/bash#director 服务器上开启路由转发功能echo 1 > /proc/sys/net/ipv4/ip_forward #关闭icmp的重定向echo 0 > /proc/sys/net/ipv4/conf/all/send_redirectsecho 0 > /proc/sys/net/ipv4/conf/default/send_redirects#注意区分网卡名字,阿铭的两个网卡分别为ens33和ens37echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirectsecho 0 > /proc/sys/net/ipv4/conf/ens37/send_redirects#director 设置nat防火墙iptables -t nat -Fiptables -t nat -Xiptables -t nat -A POSTROUTING -s 192.168.133.0/24 -j MASQUERADE#director设置ipvsadmIPVSADM=‘/usr/sbin/ipvsadm‘$IPVSADM -C$IPVSADM -A -t 192.168.147.144:80 -s wlc -p 3$IPVSADM -a -t 192.168.147.144:80 -r 192.168.133.132:80 -m -w 1$IPVSADM -a -t 192.168.147.144:80 -r 192.168.133.133:80 -m -w 1
Nat Mode effect test
Installation of Nginx on both RS
Set two RS homepage, make a distinction, that is to say, directly curl two RS IP, get different results
Browser access 192.168.142.147, multiple visits several times to see the difference in results
Introduction of load Balancing cluster and LVS introduction, scheduling algorithm, LVS NAT mode construction