LVS负载均衡之NAT模式原理及配置详细流程
? 上篇文章讲述了LVS负载均衡相关理论知识,今天主要来详细地来对LVS工作模式之一的NAT模式进行实验配置。
二、NAT模式理论回顾与简述
详细原理可以参考:https://blog.51cto.com/14557673/2467243
? 首先我们要明确的是NAT模式的最大特点是什么?
? 可以这样概述:LVS负载均衡之NAT模式(NAT充当网关)是一种基于网络地址转换技术,通过负载均衡器实现高并发的数据请求和使用调度算法实现优化服务响应的进出口相同的架构,具备高可用高安全性能。
? 而其最大劣势在于数据的出入口都是在负载均衡器(NAT服务器上),这样所造成的的后果就是无法支持高并发的数据请求(巨量),并且数据的响应回传过程加剧了这一弊病。所以才有了后续的改进。
三、实例环境
? 首先我们需要四台服务器:一台负载均衡调度器、两台web(这里使用两个Apache)服务器、一台存储服务器(NFS方式)。使用一台Windows作为外网客户主机进行模拟。
? 架构如下:4台Centos7和一台win10构成
? 网段ip地址分配如下表所示:
设备 | ip地址 |
---|---|
win10客户机 | 10.0.0.10/24 |
负载调度器 | 外网卡:10.0.0.1/24 内网卡:192.168.10.1/24 |
HTTP服务器1 | 192.168.10.10/24 |
HTTP服务器2 | 192.168.10.20/24 |
NFS存储服务器 | 192.168.10.100/24 |
? 那么我们从外网的一台客户机进行网站访问,实际上访问的是负载均衡器的外网卡地址,而对于服务器端客户机是一无所知的。那么我们需要进行内网与外网的互通,那么就可以使用NAT方式实现互通。从而将服务请求发送给真实服务器,获取所需资源后再返回给负载均衡器进行NAT网络地址转换后将资源返回给客户机。在生产环境中一般后台的存储服务器都是有多个备份的,并且资源都是一致的,不过我们为了验证调度算法轮循访问的机制,我们就需要在两个网站写入不同的的内容加以区分验证该调度算法。
? 下面请看具体的配置:
四、部署步骤和详解
? 根据上面的架构图和地址分配我们完成这个实验需要进行如下的步骤配置。
4.1NFS存储服务器配置
? 1.查看相关软件包nfs服务
[ ~]# rpm -q nfs-utils nfs-utils-1.3.0-0.48.el7.x86_64 [ ~]# rpm -q rpcbind rpcbind-0.2.0-42.el7.x86_64 [ ~]# mkdir /opt/ll /opt/cc #创建站点文件存储目录
? 2.挂载两块新的磁盘并且进行设置
sdb磁盘配置:
[ ~]# fdisk /dev/sdb 欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。 Device does not contain a recognized partition table 使用磁盘标识符 0x400f42da 创建新的 DOS 磁盘标签。 命令(输入 m 获取帮助):n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response p 分区号 (1-4,默认 1): 起始 扇区 (2048-41943039,默认为 2048): 将使用默认值 2048 Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039): 将使用默认值 41943039 分区 1 已设置为 Linux 类型,大小设为 20 GiB 命令(输入 m 获取帮助):w The partition table has been altered! Calling ioctl() to re-read partition table. 正在同步磁盘。 [ ~]# mkfs.xfs /dev/sdb1 meta-data=/dev/sdb1 isize=512 agcount=4, agsize=1310656 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=5242624, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
sdc磁盘配置:
[ ~]# fdisk /dev/sdc 欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。 Device does not contain a recognized partition table 使用磁盘标识符 0x1ef07039 创建新的 DOS 磁盘标签。 命令(输入 m 获取帮助):n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response p 分区号 (1-4,默认 1): 起始 扇区 (2048-41943039,默认为 2048): 将使用默认值 2048 Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039): 将使用默认值 41943039 分区 1 已设置为 Linux 类型,大小设为 20 GiB 命令(输入 m 获取帮助):w The partition table has been altered! Calling ioctl() to re-read partition table. 正在同步磁盘。 [ ~]# mkfs.xfs /dev/sdc1 meta-data=/dev/sdc1 isize=512 agcount=4, agsize=1310656 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=5242624, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
? 3.对两块磁盘进行挂载设置——使用永久挂载
[ ~]# vim /etc/fstab #在该文件最后添加挂载信息,如下所示 [ ~]# tail /etc/fstab # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=3f9b526a-3a51-4f87-b68a-37292b4e2e59 / xfs defaults 0 0 UUID=33d508c7-a776-4d6a-9c9b-a51bf3855004 /boot xfs defaults 0 0 UUID=90be4302-e340-4fe3-9ed2-3c40e346979e /home xfs defaults 0 0 UUID=09112ee8-0d24-4c5e-83d2-08c1f16bc738 swap swap defaults 0 0 /dev/sdb1 /opt/ll xfs defaults 0 0 /dev/sdc1 /opt/cc xfs defaults 0 0 [ ~]# mount -a [ ~]# df -hT 文件系统 类型 容量 已用 可用 已用% 挂载点 /dev/sda2 xfs 15G 3.7G 12G 25% / devtmpfs devtmpfs 898M 0 898M 0% /dev tmpfs tmpfs 912M 0 912M 0% /dev/shm tmpfs tmpfs 912M 9.0M 903M 1% /run tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda5 xfs 11G 33M 11G 1% /home /dev/sda1 xfs 30G 174M 30G 1% /boot tmpfs tmpfs 183M 4.0K 183M 1% /run/user/42 tmpfs tmpfs 183M 16K 183M 1% /run/user/0 /dev/sdb1 xfs 20G 33M 20G 1% /opt/ll /dev/sdc1 xfs 20G 33M 20G 1% /opt/cc #编辑/etc/exports文件 [ network-scripts]# vim /etc/exports [ network-scripts]# cat /etc/exports /opt/ll 192.168.10.0/24(rw,sync,no_root_squash) /opt/cc 192.168.10.0/24(rw,sync,no_root_squash)
? 4.关闭防火墙配置网卡为仅主机模式并且设置静态ip地址
[ ~]# systemctl stop firewalld.service [ ~]# setenforce 0 [ ~]# cd /etc/sysconfig/network-scripts/ [ network-scripts]# vim ifcfg-ens33 [ network-scripts]# systemctl restart network [ network-scripts]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.10.100 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::9be8:a170:f918:1f5 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:0b:d9:36 txqueuelen 1000 (Ethernet) RX packets 1151 bytes 685357 (669.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 444 bytes 39849 (38.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
? 5.启动服务
[ network-scripts]# systemctl start nfs [ network-scripts]# systemctl start rpcbind [ network-scripts]# showmount -e #查看挂载情况信息 Export list for nfs: /opt/cc 192.168.10.0/24 /opt/ll 192.168.10.0/24
4.2两台web服务器配置
? 1.在两台Centos7虚拟机上安装httpd网站服务
[ ~]# hostnamectl set-hostname web1 [ ~]# su [ ~]# yum install -y httpd ...//省略部分内容 [ ~]# hostnamectl set-hostname web2 [ ~]# su [ ~]# yum install -y httpd ...//省略部分内容
? 2.关闭两个web网站服务器防火墙
[ ~]# systemctl stop firewalld.service [ ~]# setenforce 0 [ ~]# systemctl stop firewalld.service [ ~]# setenforce 0
? 3.配置网卡
#web1配置 [ ~]# cd /etc/sysconfig/network-scripts/ [ network-scripts]# vim ifcfg-ens33 [ network-scripts]# systemctl restart network [ network-scripts]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::bdab:b59b:d041:d8b0 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:e6:6d:eb txqueuelen 1000 (Ethernet) RX packets 726004 bytes 1067841474 (1018.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 345476 bytes 21387015 (20.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 #web2配置 [ ~]# cd /etc/sysconfig/network-scripts/ [ network-scripts]# vim ifcfg-ens33 [ network-scripts]# systemctl restart network [ network-scripts]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.10.20 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::bdab:b59b:d041:d8b0 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:e6:6d:eb txqueuelen 1000 (Ethernet) RX packets 726004 bytes 1067841474 (1018.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 345476 bytes 21387015 (20.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
? 4.启动两个web网站服务并且验证挂载情况
#web1 [ network-scripts]# systemctl start httpd [ network-scripts]# netstat -natp | grep 80 tcp6 0 0 :::80 :::* LISTEN 59242/httpd [ network-scripts]# showmount -e 192.168.10.100 Export list for 192.168.10.100: /opt/cc 192.168.10.0/24 /opt/ll 192.168.10.0/24 #web2 [ network-scripts]# netstat -natp | grep 80 [ network-scripts]# systemctl start httpd.service [ network-scripts]# netstat -natp | grep 80 tcp6 0 0 :::80 :::* LISTEN 54271/httpd [ network-scripts]# showmount -e 192.168.10.100 Export list for 192.168.10.100: /opt/cc 192.168.10.0/24 /opt/ll 192.168.10.0/24
5.两个服务器上分别提供站点,写入测试信息
#web1 [ network-scripts]#vim /etc/fstab 192.168.10.100:/opt/ll /var/www/html nfs defaults._netdev 0 0 [ network-scripts]# cd /var/www/html/ [ html]# ls [ html]# vim index.html [ html]# cat index.html <head>this is ll web</head> #web2 [ network-scripts]#vim /etc/fstab 192.168.10.100:/opt/cc /var/www/html nfs defaults._netdev 0 0 [ network-scripts]# cd /var/www/html/ [ html]# ls [ html]# vim index.html [ html]# cat index.html <head>this is cc web</head>
4.3LVS负载均衡调度服务器配置
1.安装环境包
[ ~]# hostnamectl set-hostname lvs [ ~]# su [ ~]# yum install -y ipvsadm 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.163.com * updates: mirrors.tuna.tsinghua.edu.cn 正在解决依赖关系 --> 正在检查事务 ---> 软件包 ipvsadm.x86_64.0.1.27-7.el7 将被 安装 --> 解决依赖关系完成 依赖关系解决 ======================================================================================= Package 架构 版本 源 大小 ======================================================================================= 正在安装: ipvsadm x86_64 1.27-7.el7 base 45 k 事务概要 ======================================================================================= 安装 1 软件包 总下载量:45 k 安装大小:75 k Downloading packages: 警告:/var/cache/yum/x86_64/7/base/packages/ipvsadm-1.27-7.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY ipvsadm-1.27-7.el7.x86_64.rpm 的公钥尚未安装 ipvsadm-1.27-7.el7.x86_64.rpm | 45 kB 00:00:00 从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 检索密钥 导入 GPG key 0xF4A80EB5: 用户ID : "CentOS-7 Key (CentOS 7 Official Signing Key) <>" 指纹 : 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 软件包 : centos-release-7-4.1708.el7.centos.x86_64 (@anaconda) 来自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : ipvsadm-1.27-7.el7.x86_64 1/1 验证中 : ipvsadm-1.27-7.el7.x86_64 1/1 已安装: ipvsadm.x86_64 0:1.27-7.el7 完毕!
2.配置双网卡
添加网卡,然后进行设置
[ ~]# cd /etc/sysconfig/network-scripts/ [ network-scripts]# ls ifcfg-ens33 ifdown-isdn ifup ifup-plip ifup-tunnel ifcfg-lo ifdown-post ifup-aliases ifup-plusb ifup-wireless ifdown ifdown-ppp ifup-bnep ifup-post init.ipv6-global ifdown-bnep ifdown-routes ifup-eth ifup-ppp network-functions ifdown-eth ifdown-sit ifup-ib ifup-routes network-functions-ipv6 ifdown-ib ifdown-Team ifup-ippp ifup-sit ifdown-ippp ifdown-TeamPort ifup-ipv6 ifup-Team ifdown-ipv6 ifdown-tunnel ifup-isdn ifup-TeamPort [ network-scripts]# vim ifcfg-ens33 [ network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36 #配置网卡后重启网络 [ network-scripts]# systemctl restart network
两个网卡信息如下:
[ network-scripts]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::7eb1:2dde:8a54:6927 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet) RX packets 397693 bytes 574961333 (548.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 92656 bytes 5683776 (5.4 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.10.1 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::e638:fc7c:8a5b:dc5d prefixlen 64 scopeid 0x20<link> ether 00:0c:29:56:d3:54 txqueuelen 1000 (Ethernet) RX packets 51 bytes 6809 (6.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 83 bytes 13712 (13.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3.测试是否互通
network-scripts]# ping 192.168.10.10 PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data. 64 bytes from 192.168.10.10: icmp_seq=1 ttl=64 time=0.552 ms 64 bytes from 192.168.10.10: icmp_seq=2 ttl=64 time=0.299 ms 64 bytes from 192.168.10.10: icmp_seq=3 ttl=64 time=0.255 ms ^C --- 192.168.10.10 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2001ms rtt min/avg/max/mdev = 0.255/0.368/0.552/0.132 ms [ network-scripts]# ping 192.168.10.20 PING 192.168.10.20 (192.168.10.20) 56(84) bytes of data. 64 bytes from 192.168.10.20: icmp_seq=1 ttl=64 time=0.536 ms 64 bytes from 192.168.10.20: icmp_seq=2 ttl=64 time=0.340 ms ^C --- 192.168.10.20 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 0.340/0.438/0.536/0.098 ms
4.开启路由转发功能
[ network-scripts]# vim /etc/sysctl.conf #末尾加上 network-scripts]# cat /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). net.ipv4.ip_forward=1
5.设置防火墙及其规则
[ network-scripts]# systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since 四 2020-02-20 09:34:20 CST; 8min ago Docs: man:firewalld(1)
此时防火墙是开启状态,不需要将其关闭,我们可以使用iptables进行设置
#清空转发表 [ network-scripts]# iptables -F #清空nat地址转发表 [ network-scripts]# iptables -t nat -F #配置转发规则(四表五链) [ network-scripts]# iptables -t nat -A POSTROUTING -o ens33 -s 192.168.10.0/24 -j SNAT --to-source 10.0.0.1 #加载路由转发功能 [ network-scripts]# sysctl -p net.ipv4.ip_forward = 1
6.加载LVS内核模块
[ network-scripts]# modprobe ip_vs #加载命令 [ network-scripts]# cat /proc/net/ip_vs #查看 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn [ network-scripts]# systemctl start ipvsadm Job for ipvsadm.service failed because the control process exited with error code. See "systemctl status ipvsadm.service" and "journalctl -xe" for details. #Centos7系统需要使用--save保存 [ network-scripts]# ipvsadm --save > /etc/sysconfig/ipvsadm [ network-scripts]# systemctl start ipvsadm
7.定义脚本
[ network-scripts]# cd /opt/ [ opt]# vim nat.sh [ opt]# chmod 777 nat.sh [ opt]# ./nat.sh IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP lvs:http rr -> 192.168.10.10:http Masq 1 0 0 -> 192.168.10.20:http Masq 1 0 0
nat.sh的脚本如下:
#!/bin/bash #echo "1" > /proc/sys/net/ipv4/ip_forward 这里之前设置过,所以注释了 ipvsadm -C #初始化 ipvsadm -A -t 10.0.0.1:80 -s rr #指定访问入口,rr指定的是轮循算法 ipvsadm -a -t 10.0.0.1:80 -r 192.168.10.10:80 -m #建立ip地址映射关系 ipvsadm -a -t 10.0.0.1:80 -r 192.168.10.20:80 -m ipvsadm #开启
4.4使用外网客户端进行测试验证
我们可以用win10虚拟机或者一台Centos7客户机测试
但是都需要进行网卡设置,设置为仅主机模式且ip地址为10.0.0.10(10.0.0.0网段就行),网关为lvs负载均衡器外网口ip地址,在客户端的浏览器中访问的也是这个地址;测试结果的图如下所示:
1.网卡和网络测试
2.测试网站服务及轮循机制
访问lvs外网网关,lvs作为中间件或者说桥梁的角色,其实本质上是访问网站和存储服务器
刷新访问切换服务器
这边在线网上实际上两个存储的页面内容是保持一致的,这里主要是为了测试轮循机制的效果,大家需要注意一下。
至此就是lvs负载均衡NAT模式的配置流程和测试方法,谢谢阅读!