实验——如何搭建 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模式
[[email protected] ~]# 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[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[[email protected] 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
[[email protected] network-scripts]# systemctl restart network
[[email protected] network-scripts]# cd ~
[[email protected] ~]# yum -y install ipvsadm
[[email protected] ~]# ipvsadm -v
ipvsadm v1.27 2008/5/15 (compiled with popt and IPVS v1.2.1)
[root[email protected] ~]# modprobe ip_vs       ###确认内核对LVS的支持
[[email protected] ~]# 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 创建虚拟服务器

[[email protected] ~]# ipvsadam -A -t 20.0.0.21:80 -s rr

2.1.3 添加服务器节点

[[email protected] ~]# ipvsadm -a -t 20.0.0.21:80 -r 192.168.100.22:80 -m
[[email protected] ~]# ipvsadm -a -t 20.0.0.21:80 -r 192.168.100.23:80 -m

2.1.4 保存LVS策略

[[email protected] ~]# ipvsadm-save > /opt/ipvsadm
[[email protected] ~]# 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 开启调度服务器路由转发功能

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

2.2 存储服务器配置(192.168.100.24)

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

2.3 WEB1服务器配置(192.168.100.22)

'防火墙和和核心防护关闭'
[[email protected] ~]# yum -y install nfs-utils       ###必须要安装nfs-utils,否则mount不识别nfs格式
[[email protected] ~]# 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
[[email protected] ~]# yum -y install httpd
[[email protected] ~]# mount 192.168.100.24:/opt/51xit /var/www/html         ###临时挂载
[[email protected] ~]# vi /etc/fstab       ###永久挂载
192.168.100.24:/opt/51xit /var/www/html nfs defaults,_netdev 0 0       ###开机自动挂载,注意格式对齐
[[email protected] ~]# init 6      ###记得重启,要不然不生效(如果没有临时挂载)
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# systemctl enable httpd

2.4 WEB2服务器配置(192.168.100.23)

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

三、测试

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

猜你喜欢

转载自blog.csdn.net/ZG_66/article/details/108708785

相关文章

今日推荐