Implementing the NAT mode of LVS
The principle and characteristics of the detailed description of the LVS load balancing one: Lvs-nat, lvs-dr mode of the principle of detailed introduction
1. Experimental environment
IP Address Planning
Request IP address of Client Access service: VIP 12.0.0.1
Server |
IP |
system |
Director Server |
DIP 192.168.10.1 |
CentOS7 |
NFS Server |
192.168.10.50 |
RedHat6 |
Real Server1 |
RIP 192.168.10.51 |
CentOS7 |
Real Server2 |
RIP 192.168.10.52 |
CentOS7 |
In the configuration of the Director of the server to add two network cards, detailed steps can refer to the DNS separation resolution this Boven introduction, set up the external network ENS37 for VIP, intranet ens33 for dip, two real server gateway set as Director's intranet IP is dip.
In the configuration of the virtual machine, the DIP network connection is set to host only mode, and Real server is also configured for host mode only.
2. Installation and Configuration
Yum Install software
Method One: If the virtual Machine network is a host-only mode, there is no network, you can create a Yum repository locally and then install Yum.
Method Two: If the virtual Machine network is NAT mode, there is a network that can be installed online yum.
(1) Configuring Server for NFS
Install NFS software and start the NFS service
yum install nfs-utils -y #7系统版本需要安装nfs工具包service rpcbind start service nfs restart
Create a shared directory and give write permissions
mkdir /opt/wwwroot1 /opt/wwwroot2chmod 777 /opt/wwwroot1 /opt/wwwroot2
Editing a configuration file
Publishing shares
exportfs -rv
Shutting down the firewall
service iptables stop
(2) configuration of two real server servers
Installing the NFS Client
yum install nfs-utils -y systemctl start rpcbind.service systemctl start nfs.service
Viewing NFS Mounts
showmount -e 192.168.10.50
Real Server1 Mount NFS
#法一:直接挂载mount.nfs 192.168.10.50:/opt/wwwroot1 /var/www/html#法二:修改fatab文件挂载vim /etc/fstab 192.168.10.50:/opt/wwwroot1 /var/www/html nfs defaults,_netdev 0 0
Real Server2 Mount NFS
方法同Real Server1,将挂载目录/opt/wwwroot1改成/opt/wwwroot2,其余一样。
Installing httpd
yum install httpd -y
Create a test webpage and start the httpd service
#real server1创建测试网页echo "Server 192.168.10.51" > /var/www/html/index.html#real server2创建测试网页echo "Server 192.168.10.52" > /var/www/html/index.html
Turn off firewalls and security policies
systemctl stop firewalld.service systemctl disable firewalld.servicesetenforce 0
Test whether the Web page is open properly
firefox http://127.0.0.1/
(3) Configuring the Director Server server
Installing the IPVSADM management tool
yum install ipvsadm -y
Calling the LVS kernel module
modprobe ip_vs #加载LVS内核模块cat /proc/net/ip_vs #查看ip_vs版本信息
Turn on route forwarding
#法一:编辑sysctl.conf文件,永久路由转发vim /etc/sysctl.conf net.ipv4.ip_forward=1sysctl -p #保存#法二:直接编辑,临时路由转发echo "1" > /proc/sys/net/ipv4/ip_forward
Configuring Snat forwarding rules, setting up NAT firewall
iptables -F -t nat #清空nat防火墙iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o ens37 -j SNAT --to-source 12.0.0.1
Edit NAT on Director to implement load allocation script
# 设置 ipvsadmvim nat.sh #!/bin/bash ipvsadm-save > /etc/sysconfig/ipvsadm #保存策略 service ipvsadm start ipvsadm -C #清除内核虚拟服务器表中的所有记录 ipvsadm -A -t 12.0.0.1:80 -s rr #创建虚拟服务器 ipvsadm -a -t 12.0.0.1:80 -r 192.168.10.51:80 -m ipvsadm -a -t 12.0.0.1:80 -r 192.168.10.52:80 -m ipvsadm
Options for the IPVSADM management tool use:
- -A: Indicates adding a virtual server
- -T: Used to specify the VIP address and TCP port
- -S: Used to specify load balancing scheduling algorithm
- -A: Indicates adding a real server
- -R: Used to specify RIP address and TCP port
- -M: Indicates the use of NAT cluster mode
- -G: Indicates the use of Dr Cluster mode
- -I: means using Tun cluster mode
- -W: Used to set weights
Run directly after saving NAT script
chmod +x nat.sh./nat.sh
View rules for IPVSADM settings
ipvsadm -ln
3. Test the LVS Cluster
With Windows clients accessing http://12.0.0.1 directly, you will be able to see the content of the Web page provided by the real server.
First time visit:
Real Server connections Viewed:
Refresh one time:
Real Server connections Viewed:
Lvs-dr the case of the model configuration, see LVS Load Balancing Three: LVS-DR build a Web cluster, LVS combined with keepalived to build a highly available Web cluster
LVS Load Balancer II: Lvs-nat Building a Web cluster