实验——如何搭建 LVS-NAT 负载均衡集群

一、实验准备

一台调度器:配置双网卡 VM1:192.168.100.21
NAT网卡:20.0.0.21
二台WEB服务器集群池:192.168.100.22、192.168.100.23
这边网关必须指向192.168.100.21这个地址
一台NFS共享服务器:192.168.100.24

二、步骤

2.1 调度器设置(192.168.100.21)

2.1.1 基础设置

'###虚拟机添加网卡###'
一块网卡NAT配置DHCP模式
[root@localhost ~]# nmcli connection
NAME                UUID                                  TYPE      DEVICE 
ens33               45a23a13-7c7c-4535-893a-5cda88b35616  ethernet  ens33  
virbr0              b746477f-6012-40a1-88aa-fa768ce5f621  bridge    virbr0 
Wired connection 1  58d1aa03-0f23-3fdc-b121-8fee0f982981  ethernet  e[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[root@localhost network-scripts]# vi ifcfg-ens36ns36
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens36
UUID=58d1aa03-0f23-3fdc-b121-8fee0f982981
DEVICE=ens36
ONBOOT=yes
IPADDR=192.168.100.21
NETMASK=255.255.255.0
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# cd ~
[root@localhost ~]# yum -y install ipvsadm
[root@localhost ~]# ipvsadm -v
ipvsadm v1.27 2008/5/15 (compiled with popt and IPVS v1.2.1)
[root@localhost ~]# modprobe ip_vs       ###确认内核对LVS的支持
[root@localhost ~]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActCon

2.1.2 创建虚拟服务器

[root@localhost ~]# ipvsadam -A -t 20.0.0.21:80 -s rr

2.1.3 添加服务器节点

[root@localhost ~]# ipvsadm -a -t 20.0.0.21:80 -r 192.168.100.22:80 -m
[root@localhost ~]# ipvsadm -a -t 20.0.0.21:80 -r 192.168.100.23:80 -m

2.1.4 保存LVS策略

[root@localhost ~]# ipvsadm-save > /opt/ipvsadm
[root@localhost ~]# cat /opt/ipvsadm 
-A -t localhost.localdomain:http -s rr
-a -t localhost.localdomain:http -r 192.168.100.22:http -m -w 1
-a -t localhost.localdomain:http -r 192.168.100.23:http -m -w 1

2.1.5 开启调度服务器路由转发功能

[root@localhost ~]# vi /etc/sysctl.conf
'###添加以下内容###'
net.ipv4.ip_forward = 1
[root@localhost ~]# sysctl -p         ###开启路由转发
net.ipv4.ip_forward = 1

2.2 存储服务器配置(192.168.100.24)

[root@localhost ~]# rpm -q nfs-utils       ###如果没装,yum -y install nfs-utils
nfs-utils-1.3.0-0.61.el7.x86_64
[root@localhost ~]# rpm -q rpcbind        ###如果没装,yum -y install rpcbind
rpcbind-0.2.0-47.el7.x86_64
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# echo "this is www.51xit.top" > /opt/51xit/index.html
[root@localhost ~]# echo "this is www.52xit.top" > /opt/52xit/index.html

2.3 WEB1服务器配置(192.168.100.22)

'防火墙和和核心防护关闭'
[root@localhost ~]# yum -y install nfs-utils       ###必须要安装nfs-utils,否则mount不识别nfs格式
[root@localhost ~]# showmount -e 192.168.100.24        ###查看共享
Export list for 192.168.100.24:
/opt/52xit 192.168.100.0/24
/opt/51xit 192.168.100.0/24
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# mount 192.168.100.24:/opt/51xit /var/www/html         ###临时挂载
[root@localhost ~]# vi /etc/fstab       ###永久挂载
192.168.100.24:/opt/51xit /var/www/html nfs defaults,_netdev 0 0       ###开机自动挂载,注意格式对齐
[root@localhost ~]# init 6      ###记得重启,要不然不生效(如果没有临时挂载)
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd

2.4 WEB2服务器配置(192.168.100.23)

[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# mount 192.168.100.24:/opt/52xit /var/www/html/
[root@localhost ~]# ll /var/www/html/
total 4
-rw-r--r-- 1 root root 22 Sep 21 20:13 index.html
[root@localhost ~]# vi /etc/fstab       ###永久挂载
192.168.100.24:/opt/52xit /var/www/html nfs defaults,_netdev 0 0       ###开机自动挂载,注意格式对齐
[root@localhost ~]# init 6      ###记得重启,要不然不生效(如果没有临时挂载)
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd

三、测试

在网页输入如下:
192.168.100.22
在这里插入图片描述
192.168.100.23
在这里插入图片描述
20.0.0.21
在这里插入图片描述
再次刷新网页
在这里插入图片描述
测试成功

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页