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
相关推荐
青梅橘子皮3 小时前
Linux---基本指令
linux·运维·服务器
REDcker4 小时前
Linux信号机制详解 POSIX语义与内核要点 sigaction与备用栈实践
linux·运维·php
cui_ruicheng5 小时前
Linux进程间通信(三):System V IPC与共享内存
linux·运维·服务器
蚰蜒螟5 小时前
深入 Linux 内核同步机制:从 futex 到 spinlock 的完整旅程
linux·windows·microsoft
运维全栈笔记5 小时前
Linux安装配置Tomcat保姆级教程:从部署到性能调优
linux·服务器·中间件·tomcat·apache·web
dllmayday6 小时前
Linux 上用终端连接 WiFi
linux·服务器·windows
ACP广源盛139246256736 小时前
IX8024与科学大模型的碰撞@ACP#筑牢科研 AI 算力高速枢纽分享
运维·服务器·网络·数据库·人工智能·嵌入式硬件·电脑
峥无7 小时前
Linux系统编程基石:静态库·动态库·ELF文件·进程地址空间全景图
linux·运维·服务器
用户2367829801688 小时前
从 chmod 755 说起:Unix 文件权限到底是怎么算的?
linux
Strugglingler8 小时前
【systemctl 学习总结】
linux·systemd·systemctl·journalctl·unit file