LVS NAT模式实现三台RS的轮询访问

节点规划:

配置RS:

RS1-RS3的网关配置均为 192.168.163.8

配置RS1:
复制代码
[root@localhost ~]# hostnamectl hostname rs1
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.163.7/24 ipv4.gateway 192.168.163.8 connection.autoconnect yes
[root@localhost ~]# nmcli c up ens160
[root@rs1 ~]# dnf install nginx -y
[root@rs1 ~]# echo $(hostname -I) > /usr/share/nginx/html/index.html
[root@rs1 ~]# systemctl start nginx
[root@rs1 ~]# curl localhost
192.168.163.7
配置RS2:
复制代码
[root@localhost ~]# hostnamectl hostname rs2
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.163.17/24 ipv4.gateway 192.168.163.8 connection.autoconnect yes
[root@localhost ~]# nmcli c up ens160
[root@rs2 ~]# dnf install nginx -y
[root@rs2 ~]# echo $(hostname -I) > /usr/share/nginx/html/index.html
[root@rs2 ~]# systemctl start nginx
[root@rs2 ~]# curl localhost
192.168.163.17
配置RS3:
复制代码
[root@localhost ~]# hostnamectl hostname rs3
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.163.27/24 ipv4.gateway 192.168.163.8 connection.autoconnect yes
[root@localhost ~]# nmcli c up ens160
[root@rs3 ~]# dnf install nginx -y
[root@rs3 ~]# echo $(hostname -I) > /usr/share/nginx/html/index.html
[root@rs3 ~]# systemctl start nginx
[root@rs3 ~]# curl localhost
192.168.163.27

配置LVS:

lvs服务器有两块网卡,第一块网卡采用仅主机模式,IP 地址为 192.168.110.200,第二块网卡采用 NAT 模式,IP 地址为192.168.163.8

修改虚拟网络及主机名:
复制代码
[root@localhost ~]# nmcli c modify 'Wired connection 1' connection.id ens224
[root@localhost ~]# nmcli c show
NAME    UUID                                  TYPE      DEVICE 
ens224  716944d5-683d-3297-9f0b-39530d65c935  ethernet  ens224 
ens160  c6adadcb-89ef-3ed8-a265-09260f58abe1  ethernet  ens160 
lo      3919112a-6bb6-4ad3-a213-3ce8394ca9a9  loopback  lo
[root@localhost ~]# hostnamectl hostname lvs
配置仅主机模式网卡:
复制代码
[root@lvs ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.110.200/24 ipv4.gateway 192.168.110.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
[root@lvs ~]# nmcli c up ens160
安装软件:
复制代码
[root@lvs ~]# dnf install ipvsadm -y
配置NAT模式网卡:
复制代码
[root@lvs ~]# nmcli c modify ens224 ipv4.method manual ipv4.addresses 192.168.163.8/24 ipv4.gateway 192.168.163.2 connection.autoconnect yes
[root@lvs ~]# nmcli c up ens224 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)

配置客户端:

复制代码
[root@localhost ~]# hostnamectl hostname client
[root@client ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.10.100/24 ipv4.gateway 192.168.10.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
[root@client ~]# nmcli c up ens160
[root@client ~]# nmcli d show ens160 
GENERAL.DEVICE:                         ens160
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:0C:29:DF:3E:09
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     ens160
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnecti>
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         192.168.110.100/24
IP4.GATEWAY:                            192.168.110.2
IP4.ROUTE[1]:                           dst = 192.168.110.0/24, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]:                           dst = 0.0.0.0/0, nh = 192.168.110.2, mt = 100
IP4.DNS[1]:                             223.5.5.5
IP6.ADDRESS[1]:                         fe80::20c:29ff:fedf:3e09/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 1024

启动ipvsadm服务:

复制代码
[root@lvs ~]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@lvs ~]# systemctl start ipvsadm

[root@lvs ~]# ipvsadm -C
[root@lvs ~]# ipvsadm -A -t 192.168.110.200:80 -s rr
[root@lvs ~]# ipvsadm -a -t 192.168.110.200:80 -r 192.168.163.7:80 -m
[root@lvs ~]# ipvsadm -a -t 192.168.110.200:80 -r 192.168.163.17:80 -m
[root@lvs ~]# ipvsadm -a -t 192.168.110.200:80 -r 192.168.163.27:80 -m
[root@lvs ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.110.200:80 rr
  -> 192.168.163.7:80             Masq    1      0          0         
  -> 192.168.163.17:80            Masq    1      0          0         
  -> 192.168.163.27:80            Masq    1      0          0         

配置完成后,重启服务

复制代码
[root@lvs ~]# systemctl restart ipvsadm

重启好后再配置内核转发参数net.ipv4.ip_forward=1

复制代码
[root@lvs ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
[root@lvs ~]# sysctl -p
net.ipv4.ip_forward = 1
相关推荐
Forget_855023 分钟前
Linux的例行性工作
linux·运维·服务器
用手码出世界39 分钟前
自定义minshell
linux·服务器
心随雪冻1 小时前
18.PCIe总线入门理解与Linux上PCIe设备配置与使用
linux·arm开发·嵌入式硬件
m0_745364241 小时前
LVS-DR模式配置脚本
linux·运维·服务器·github·lvs
ABILI .1 小时前
RestTemplate远程调用接口方式
java·服务器·前端
不知名。。。。。。。。1 小时前
Linux--命令行操作
linux·运维·服务器
老年DBA2 小时前
Linux Namespace(网络命名空间)系列三 --- 使用 Open vSwitch 和 VLAN 标签实现网络隔离
linux·运维·服务器·网络
MarkHD3 小时前
第五天 Linux基础命令强化 - Shell与Python交互 - Vim基础操作
linux·python·交互
筑梦之路3 小时前
CentOS 8 Stream 配置在线yum源参考 —— 筑梦之路
linux·centos
无名之逆3 小时前
[特殊字符] Hyperlane:Rust 高性能 Web 框架的终极选择 [特殊字符]
服务器·开发语言·前端·网络·后端·http·rust