溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

LVS負(fù)載均衡之NAT模式原理及配置詳細(xì)流程

發(fā)布時(shí)間:2020-08-03 17:47:01 來源:網(wǎng)絡(luò) 閱讀:205 作者:wx5d8a17c45cb5b 欄目:建站服務(wù)器

一、前言

? 上篇文章講述了LVS負(fù)載均衡相關(guān)理論知識(shí),今天主要來詳細(xì)地來對(duì)LVS工作模式之一的NAT模式進(jìn)行實(shí)驗(yàn)配置。

二、NAT模式理論回顧與簡(jiǎn)述

詳細(xì)原理可以參考:https://blog.51cto.com/14557673/2467243
? 首先我們要明確的是NAT模式的最大特點(diǎn)是什么?

? 可以這樣概述:LVS負(fù)載均衡之NAT模式(NAT充當(dāng)網(wǎng)關(guān))是一種基于網(wǎng)絡(luò)地址轉(zhuǎn)換技術(shù),通過負(fù)載均衡器實(shí)現(xiàn)高并發(fā)的數(shù)據(jù)請(qǐng)求和使用調(diào)度算法實(shí)現(xiàn)優(yōu)化服務(wù)響應(yīng)的進(jìn)出口相同的架構(gòu),具備高可用高安全性能。

? 而其最大劣勢(shì)在于數(shù)據(jù)的出入口都是在負(fù)載均衡器(NAT服務(wù)器上),這樣所造成的的后果就是無法支持高并發(fā)的數(shù)據(jù)請(qǐng)求(巨量),并且數(shù)據(jù)的響應(yīng)回傳過程加劇了這一弊病。所以才有了后續(xù)的改進(jìn)。

三、實(shí)例環(huán)境

? 首先我們需要四臺(tái)服務(wù)器:一臺(tái)負(fù)載均衡調(diào)度器、兩臺(tái)web(這里使用兩個(gè)Apache)服務(wù)器、一臺(tái)存儲(chǔ)服務(wù)器(NFS方式)。使用一臺(tái)Windows作為外網(wǎng)客戶主機(jī)進(jìn)行模擬。

? 架構(gòu)如下:4臺(tái)Centos7和一臺(tái)win10構(gòu)成

LVS負(fù)載均衡之NAT模式原理及配置詳細(xì)流程

? 網(wǎng)段ip地址分配如下表所示:

設(shè)備 ip地址
win10客戶機(jī) 10.0.0.10/24
負(fù)載調(diào)度器 外網(wǎng)卡:10.0.0.1/24 內(nèi)網(wǎng)卡:192.168.10.1/24
HTTP服務(wù)器1 192.168.10.10/24
HTTP服務(wù)器2 192.168.10.20/24
NFS存儲(chǔ)服務(wù)器 192.168.10.100/24

? 那么我們從外網(wǎng)的一臺(tái)客戶機(jī)進(jìn)行網(wǎng)站訪問,實(shí)際上訪問的是負(fù)載均衡器的外網(wǎng)卡地址,而對(duì)于服務(wù)器端客戶機(jī)是一無所知的。那么我們需要進(jìn)行內(nèi)網(wǎng)與外網(wǎng)的互通,那么就可以使用NAT方式實(shí)現(xiàn)互通。從而將服務(wù)請(qǐng)求發(fā)送給真實(shí)服務(wù)器,獲取所需資源后再返回給負(fù)載均衡器進(jìn)行NAT網(wǎng)絡(luò)地址轉(zhuǎn)換后將資源返回給客戶機(jī)。在生產(chǎn)環(huán)境中一般后臺(tái)的存儲(chǔ)服務(wù)器都是有多個(gè)備份的,并且資源都是一致的,不過我們?yōu)榱蓑?yàn)證調(diào)度算法輪循訪問的機(jī)制,我們就需要在兩個(gè)網(wǎng)站寫入不同的的內(nèi)容加以區(qū)分驗(yàn)證該調(diào)度算法。

? 下面請(qǐng)看具體的配置:

四、部署步驟和詳解

? 根據(jù)上面的架構(gòu)圖和地址分配我們完成這個(gè)實(shí)驗(yàn)需要進(jìn)行如下的步驟配置。

4.1NFS存儲(chǔ)服務(wù)器配置

? 1.查看相關(guān)軟件包nfs服務(wù)

[root@nfs ~]# rpm -q nfs-utils 
nfs-utils-1.3.0-0.48.el7.x86_64
[root@nfs ~]# rpm -q rpcbind 
rpcbind-0.2.0-42.el7.x86_64
[root@nfs ~]# mkdir /opt/ll /opt/cc #創(chuàng)建站點(diǎn)文件存儲(chǔ)目錄

? 2.掛載兩塊新的磁盤并且進(jìn)行設(shè)置

sdb磁盤配置:

[root@nfs ~]# fdisk /dev/sdb
歡迎使用 fdisk (util-linux 2.23.2)。

更改將停留在內(nèi)存中,直到您決定將更改寫入磁盤。
使用寫入命令前請(qǐng)三思。

Device does not contain a recognized partition table
使用磁盤標(biāo)識(shí)符 0x400f42da 創(chuàng)建新的 DOS 磁盤標(biāo)簽。

命令(輸入 m 獲取幫助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
分區(qū)號(hào) (1-4,默認(rèn) 1):
起始 扇區(qū) (2048-41943039,默認(rèn)為 2048):
將使用默認(rèn)值 2048
Last 扇區(qū), +扇區(qū) or +size{K,M,G} (2048-41943039,默認(rèn)為 41943039):
將使用默認(rèn)值 41943039
分區(qū) 1 已設(shè)置為 Linux 類型,大小設(shè)為 20 GiB

命令(輸入 m 獲取幫助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盤。
[root@nfs ~]# 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磁盤配置:

[root@nfs ~]# fdisk /dev/sdc
歡迎使用 fdisk (util-linux 2.23.2)。

更改將停留在內(nèi)存中,直到您決定將更改寫入磁盤。
使用寫入命令前請(qǐng)三思。

Device does not contain a recognized partition table
使用磁盤標(biāo)識(shí)符 0x1ef07039 創(chuàng)建新的 DOS 磁盤標(biāo)簽。

命令(輸入 m 獲取幫助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
分區(qū)號(hào) (1-4,默認(rèn) 1):
起始 扇區(qū) (2048-41943039,默認(rèn)為 2048):
將使用默認(rèn)值 2048
Last 扇區(qū), +扇區(qū) or +size{K,M,G} (2048-41943039,默認(rèn)為 41943039):
將使用默認(rèn)值 41943039
分區(qū) 1 已設(shè)置為 Linux 類型,大小設(shè)為 20 GiB

命令(輸入 m 獲取幫助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盤。
[root@nfs ~]# 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.對(duì)兩塊磁盤進(jìn)行掛載設(shè)置——使用永久掛載

[root@nfs ~]# vim /etc/fstab  #在該文件最后添加掛載信息,如下所示
[root@nfs ~]# 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
[root@nfs ~]# mount -a
[root@nfs ~]# df -hT
文件系統(tǒng)       類型      容量  已用  可用 已用% 掛載點(diǎn)
/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文件
[root@nfs network-scripts]# vim /etc/exports
[root@nfs 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.關(guān)閉防火墻配置網(wǎng)卡為僅主機(jī)模式并且設(shè)置靜態(tài)ip地址

[root@nfs ~]# systemctl stop firewalld.service 
[root@nfs ~]# setenforce 0
[root@nfs ~]# cd /etc/sysconfig/network-scripts/
[root@nfs network-scripts]# vim ifcfg-ens33
[root@nfs network-scripts]# systemctl restart network
[root@nfs 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.啟動(dòng)服務(wù)

[root@nfs network-scripts]# systemctl start nfs
[root@nfs network-scripts]# systemctl start rpcbind
[root@nfs network-scripts]# showmount -e    #查看掛載情況信息
Export list for nfs:
/opt/cc 192.168.10.0/24
/opt/ll 192.168.10.0/24

4.2兩臺(tái)web服務(wù)器配置

? 1.在兩臺(tái)Centos7虛擬機(jī)上安裝httpd網(wǎng)站服務(wù)

[root@localhost ~]# hostnamectl set-hostname web1
[root@localhost ~]# su
[root@web1 ~]# yum install -y httpd
...//省略部分內(nèi)容

[root@localhost ~]# hostnamectl set-hostname web2
[root@localhost ~]# su
[root@web2 ~]# yum install -y httpd
...//省略部分內(nèi)容

? 2.關(guān)閉兩個(gè)web網(wǎng)站服務(wù)器防火墻

[root@web1 ~]# systemctl stop firewalld.service 
[root@web1 ~]# setenforce 0

[root@web2 ~]# systemctl stop firewalld.service 
[root@web2 ~]# setenforce 0

? 3.配置網(wǎng)卡

#web1配置
[root@web1 ~]# cd /etc/sysconfig/network-scripts/
[root@web1 network-scripts]# vim ifcfg-ens33
[root@web1 network-scripts]# systemctl restart network
[root@web1 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配置
[root@web2 ~]# cd /etc/sysconfig/network-scripts/
[root@web2 network-scripts]# vim ifcfg-ens33
[root@web2 network-scripts]# systemctl restart network
[root@web2 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.啟動(dòng)兩個(gè)web網(wǎng)站服務(wù)并且驗(yàn)證掛載情況

#web1
[root@web1 network-scripts]# systemctl start httpd 
[root@web1 network-scripts]# netstat -natp | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      59242/httpd         
[root@web1 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
[root@web2 network-scripts]# netstat -natp | grep 80
[root@web2 network-scripts]# systemctl start httpd.service 
[root@web2 network-scripts]# netstat -natp | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      54271/httpd         
[root@web2 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.兩個(gè)服務(wù)器上分別提供站點(diǎn),寫入測(cè)試信息

#web1
[root@web1 network-scripts]#vim /etc/fstab
192.168.10.100:/opt/ll  /var/www/html   nfs     defaults._netdev    0   0
[root@web1 network-scripts]# cd /var/www/html/
[root@web1 html]# ls
[root@web1 html]# vim index.html
[root@web1 html]# cat index.html 
<head>this is ll web</head>

#web2
[root@web2 network-scripts]#vim /etc/fstab
192.168.10.100:/opt/cc  /var/www/html   nfs     defaults._netdev    0   0
[root@web2 network-scripts]# cd /var/www/html/
[root@web2 html]# ls
[root@web2 html]# vim index.html
[root@web2 html]# cat index.html 
<head>this is cc web</head>

4.3LVS負(fù)載均衡調(diào)度服務(wù)器配置

1.安裝環(huán)境包

[root@localhost ~]# hostnamectl set-hostname lvs
[root@localhost ~]# su
[root@lvs ~]# 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
正在解決依賴關(guān)系
--> 正在檢查事務(wù)
---> 軟件包 ipvsadm.x86_64.0.1.27-7.el7 將被 安裝
--> 解決依賴關(guān)系完成

依賴關(guān)系解決

=======================================================================================
 Package             架構(gòu)               版本                    源                大小
=======================================================================================
正在安裝:
 ipvsadm             x86_64             1.27-7.el7              base              45 k

事務(wù)概要
=======================================================================================
安裝  1 軟件包

總下載量:45 k
安裝大?。?5 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 檢索密鑰
導(dǎo)入 GPG key 0xF4A80EB5:
 用戶ID     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 指紋       : 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 
  驗(yàn)證中      : ipvsadm-1.27-7.el7.x86_64                                          1/1 

已安裝:
  ipvsadm.x86_64 0:1.27-7.el7                                                          

完畢!

2.配置雙網(wǎng)卡

添加網(wǎng)卡,然后進(jìn)行設(shè)置

[root@lvs ~]# cd /etc/sysconfig/network-scripts/
[root@lvs 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
[root@lvs network-scripts]# vim ifcfg-ens33 
[root@lvs network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
#配置網(wǎng)卡后重啟網(wǎng)絡(luò)
[root@lvs network-scripts]# systemctl restart network

兩個(gè)網(wǎng)卡信息如下:

[root@lvs 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.測(cè)試是否互通

root@lvs 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
[root@lvs 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.開啟路由轉(zhuǎn)發(fā)功能

[root@lvs network-scripts]# vim /etc/sysctl.conf #末尾加上
root@lvs 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.設(shè)置防火墻及其規(guī)則

[root@lvs 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)

此時(shí)防火墻是開啟狀態(tài),不需要將其關(guān)閉,我們可以使用iptables進(jìn)行設(shè)置

#清空轉(zhuǎn)發(fā)表
[root@lvs network-scripts]# iptables -F
#清空nat地址轉(zhuǎn)發(fā)表
[root@lvs network-scripts]# iptables -t nat -F
#配置轉(zhuǎn)發(fā)規(guī)則(四表五鏈)
[root@lvs network-scripts]# iptables -t nat -A POSTROUTING -o ens33 -s 192.168.10.0/24 -j SNAT --to-source 10.0.0.1
#加載路由轉(zhuǎn)發(fā)功能
[root@lvs network-scripts]# sysctl -p
net.ipv4.ip_forward = 1

6.加載LVS內(nèi)核模塊

[root@lvs network-scripts]# modprobe ip_vs #加載命令
[root@lvs 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
[root@lvs 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系統(tǒng)需要使用--save保存
[root@lvs network-scripts]# ipvsadm --save > /etc/sysconfig/ipvsadm
[root@lvs network-scripts]# systemctl start ipvsadm

7.定義腳本

[root@lvs network-scripts]# cd /opt/
[root@lvs opt]# vim nat.sh
[root@lvs opt]# chmod 777 nat.sh
[root@lvs 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   這里之前設(shè)置過,所以注釋了
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地址映射關(guān)系
ipvsadm -a      -t 10.0.0.1:80 -r 192.168.10.20:80 -m
ipvsadm #開啟

4.4使用外網(wǎng)客戶端進(jìn)行測(cè)試驗(yàn)證

我們可以用win10虛擬機(jī)或者一臺(tái)Centos7客戶機(jī)測(cè)試

但是都需要進(jìn)行網(wǎng)卡設(shè)置,設(shè)置為僅主機(jī)模式且ip地址為10.0.0.10(10.0.0.0網(wǎng)段就行),網(wǎng)關(guān)為lvs負(fù)載均衡器外網(wǎng)口ip地址,在客戶端的瀏覽器中訪問的也是這個(gè)地址;測(cè)試結(jié)果的圖如下所示:

1.網(wǎng)卡和網(wǎng)絡(luò)測(cè)試

LVS負(fù)載均衡之NAT模式原理及配置詳細(xì)流程

2.測(cè)試網(wǎng)站服務(wù)及輪循機(jī)制

訪問lvs外網(wǎng)網(wǎng)關(guān),lvs作為中間件或者說橋梁的角色,其實(shí)本質(zhì)上是訪問網(wǎng)站和存儲(chǔ)服務(wù)器

LVS負(fù)載均衡之NAT模式原理及配置詳細(xì)流程

刷新訪問切換服務(wù)器

LVS負(fù)載均衡之NAT模式原理及配置詳細(xì)流程

這邊在線網(wǎng)上實(shí)際上兩個(gè)存儲(chǔ)的頁(yè)面內(nèi)容是保持一致的,這里主要是為了測(cè)試輪循機(jī)制的效果,大家需要注意一下。

至此就是lvs負(fù)載均衡NAT模式的配置流程和測(cè)試方法,謝謝閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI