LVS cluster
One, build LVS-NAT cluster
1. Preparation
Host | operating system | Virtual network card | IP address | Gateway |
---|---|---|---|---|
LVS load balancing scheduler | Centos7 | VM1 ,2 |
192.168.1.10 ,192.168.100.10 |
Act as a gateway |
Web-AAA | Centos7 | VM1 | 192.168.1.3 | 192.168.1.10 |
Web-BBB | Centos7 | VM1 | 192.168.1.4 | 192.168.1.10 |
NFS shared storage | Centos7 | VM1 | 192.168.1.110 | 192.168.1.10 |
Extranet client | Win7 | VM2 | 192.168.100.20 | 192.168.100.10 |
2. Configure LVS server
1) Load the module
[root@localhost ~]# modprobe ip_vs
2) Confirm the kernel's support for LVS
[root@localhost ~]# modprobe ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
3) Enable routing and forwarding
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@localhost ~]# sysctl -p
4) Use ipvsadm tool (LVS cluster management tool)
挂载光盘,并配置yum源
[root@localhost ~]# yum -y install ipvsadm
To create a virtual server, the VIP must be the real IP address of the machine
[root@localhost ~]# ipvsadm -C (清除原有的策略)
[root@localhost ~]# ipvsadm -A -t 192.168.100.10:80 -s rr
- -A add virtual server
- -t VIP address and port number
- -s load scheduling algorithm (polling rr, weighted polling wrr, least connection lc, weighted least connection wlc)
add server node Web-AAA, Web-BBB
[root@localhost ~]# ipvsadm -a -t 192.168.100.10:80 -r 192.168.1.3:80 -m -w 1
[root@localhost ~]# ipvsadm -a -t 192.168.100.10:80 -r 192.168.1.4:80 -m -w 1
- -a Add real server
- -t vip address and tcp port number
- -r rip address and port number
- -m Use nat cluster default (-g DR mode, -i TUN mode)
- -w Set the weight (0 is to suspend the node) to
save the strategy, and set the boot to start
[root@localhost ~]# ipvsadm-save
[root@localhost ~]# systemctl enable ipvsadm
3. NFS shared storage server configuration
挂载光盘,并配置yum源
1) Install nfs-utils and rpcbind software packages
[root@localhost ~]# yum -y install nfs-utils rpcbind
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
2) Set up a shared directory
[root@localhost ~]# mkdir -p /opt/web1 /opt/web2
3) Set up nfs shared files
将文件夹web1,web2分别共享给web1,web2主机 使用,允许读写操作
[root@localhost ~]# vi /etc/exports
添加:
/opt/web1 192.168.1.3(rw,sync,no_root_squash)
/opt/web2 192.168.1.4(rw,sync,no_root_squash)
Note:
rw 读写
sync 同步写入
no_root_squash 客户机以root身份访问时赋予本地root权限
4) Start the NFS service program. Note: Start rpcbind first, then nfs
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# netstat -anpu | grep rpcbind
5) View the NFS shared directory published by this machine
[root@localhost ~]# showmount -e
4.Web-AAA host
挂载光盘,并配置yum源
1) Install httpd service
[root@Web-AAA ~]# yum -y install httpd
2) Configure nfs shared storage client
[root@Web-AAA ~]# yum -y install rpcbind nfs-utils
[root@Web-AAA ~]# systemctl enable rpcbind
[root@Web-AAA ~]# systemctl start rpcbind
[root@Web-AAA ~]# showmount -e 192.168.1.110
[root@Web-AAA ~]# mount 192.168.1.110:/opt/web1 /var/www/html/
[root@Web-AAA ~]# echo "<h1>this is web1</h1>" > /var/www/html/index.html
[root@Web-AAA ~]# echo "192.168.1.110:/opt/web1 /var/www/html nfs defaults,_netdev 0 0" > /etc/fstab
[root@Web-AAA ~]# tail -1 /etc/fstab
[root@Web-AAA ~]# systemctl enable httpd
[root@Web-AAA ~]# systemctl start httpd
5.Web-BBB host
挂载光盘,并配置yum源
1) Install httpd service
[root@Web-BBB ~]# yum -y install httpd
2) Configure nfs shared storage client
[root@Web-BBB ~]# yum -y install rpcbind nfs-utils
[root@Web-BBB ~]# systemctl enable rpcbind
[root@Web-BBB ~]# systemctl start rpcbind
[root@Web-BBB ~]# showmount -e 192.168.1.110
[root@Web-BBB ~]# mount 192.168.1.110:/opt/web2 /var/www/html/
[root@Web-BBB ~]# echo "<h1>this is web2</h1>" > /var/www/html/index.html
[root@Web-BBB ~]# echo "192.168.1.110:/opt/web2 /var/www/html nfs defaults,_netdev 0 0" > /etc/fstab
[root@Web-BBB ~]# tail -1 /etc/fstab
[root@Web-BBB ~]# systemctl enable httpd
[root@Web-BBB ~]# systemctl start httpd
6. External network client test
访问:http://192.168.100.10
多访问几次,不然看不到效果
2. Direct routing mode (LVS-DR)
1. Preparation
You can continue to use the above experimental environment, but do the following modifications
将调度器的外网网卡删掉,就是VM2那块网卡
将Win客户机的VM2改为VM1,IP改为:192.168.1.88
2. Configure the load scheduler
1) Configure Virtual IP Address (VIP)
采用虚接口的方式,为ens33绑定VIP地址,以便响应群集访问
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# rm -rf ifcfg-ens37
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vi ifcfg-ens33:0
First delete the UUID configuration items, and then change the following configuration items to the following
NAME="ens33:0"
DEVICE="ens33:0"
IPADDR="192.168.1.188"
[root@localhost network-scripts]# systemctl restart network
[ root@localhost network-scripts]# ifconfig ens33:0
2) Adjust the /proc response parameters
对于DR群集模式来说,用于LVS负载调度器和各节点需要公用VIP地址,应该关闭linux内核重定向参数
[root@localhost ~]# vi /etc/sysctl.conf
删除原有的:net.ipv4.ip_forward = 1
添加:
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]# sysctl -p
3) Configure load distribution strategy
[root@localhost ~]# ipvsadm -C
[root@localhost ~]# ipvsadm -A -t 192.168.1.188:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.1.188:80 -r 192.168.1.3 -g -w 1
[root@localhost ~]# ipvsadm -a -t 192.168.1.188:80 -r 192.168.1.4 -g -w 1
[root@localhost ~]# ipvsadm-save
3. Configure the web node server
Web-AAA和WebBBB服务器操作都如下:
1) Configure the virtual ip address VIP
使用DR模式,节点服务器,也需要配置VIP地址,使用这个地址做web响应的源地址
[root@Web-AAA ~]# cd /etc/sysconfig/network-scripts/
[root@Web-AAA network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@Web-AAA network-scripts]# vi ifcfg-lo:0
修改:
修改DEVICE=lo:0
NETMASK=255.255.255.255 \\子网掩码必须为255.255.255.255
IPADDR=192.168.1.188 \\改为VIP地址
[root@Web-AAA network-scripts]# systemctl restart network
[root@Web-AAA network-scripts]# ip a
Add a routing record for this machine to limit the VIP data locally to avoid communication chaos
[root@Web-AAA ~]# yum -y install net-tools
[root@Web-AAA ~]# vi /etc/rc.local
添加:
/sbin/route add -host 192.168.1.188 dev lo:0
[root@Web-AAA ~]# route add -host 192.168.1.188 dev lo:0
2) Adjust the kernel parameters
[root@Web-AAA ~]# vi /etc/sysctl.conf
添加:
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@Web-AAA ~]# sysctl -p
3) Install httpd to create a test web page, and then mount the NFS share
4) Enable httpd service.
Since the experiment in Figure 1 is used, it has already been done, so we will not do steps 3 or 4
Test the LVS cluster
在win7客户机上将VM2改为VM1,并设置IP为192.168.1.88
测试时频繁刷新页面
You can also use the script to test:
5) View the LVS strategy on the LVS scheduler
- Masq-address masquerading NAT mode
- Route-DR mode
- Weight—weight