作业
要求:使用LVS的 NAT 模式实现 3 台 RS 的轮询访问。IP地址和主机自己规划。
节点规划
主机 | 角色 | 系统 | 网络 | IP |
---|---|---|---|---|
client | client | redhat 9.5 | 仅主机 | 192.168.60.100/24 |
lvs | lvs | redhat 9.5 | 仅主机 NAT | 192.168.60.200/24 VIP 192.168.23.8/24 DIP |
nginx | rs1 | redhat 9.5 | NAT | 192.168.23.7/24 |
nginx | rs2 | redhat 9.5 | NAT | 192.168.23.17/24 |
nginx | rs3 | redhat 9.5 | NAT | 192.168.23.27/24 |
配置RS
RS需要配置为NAT模式,网关这里设置为 192.168.23.8
配置 RS1
[root@localhost ~]# hostnamectl hostname rs1
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.23.7/24 ipv4.gateway 192.168.23.8 connection.autoconnect yes
[root@localhost ~]# nmcli c up ens160
# 安装nginx
[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.72.7
配置 RS2
[root@localhost ~]# hostnamectl hostname rs2
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.23.17/24 ipv4.gateway 192.168.23.8 connection.autoconnect yes
[root@localhost ~]# nmcli c up ens160
# 安装nginx
[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.72.7
配置 RS3
[root@server ~]# hostnamectl hostname rs3
[root@server ~]# nmcli connection modify ens160 ipv4.method manual ipv4.addresses 192.168.23.27/24 ipv4.gateway 192.168.23.8 connection.autoconnect yes
[root@server ~]# nmcli connection up ens160
### 安装nginx
[root@rs3 ~]# dnf install -y nginx
# 修改默认访问页
[root@rs3 ~]# echo &(hostname -I) > /usr/share/nginx/html/index.html
[1] 2339
[1]+ Done echo
[root@rs3 ~]# systemctl start nginx
[root@rs3 ~]# curl localhost
192.168.23.27
配置 LVS
修改虚拟网络
LVS 服务器第一块网卡采用仅主机模式,IP 地址为 192.168.60.200,第二块网卡采用 NAT 模式,IP 地址为 192.168.23.8。
这里我们先在VMware中将 网络适配器 改为仅主机模式,然后再添加一块 NAT 模式的网卡。
[root@server ~]# hostnamectl hostname lvs
### 关闭防火墙
[root@lvs ~]# systemctl stop firewalld.service
[root@lvs ~]# ip a
1: ...
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:96:88:aa brd ff:ff:ff:ff:ff:ff
altname enp3s0
inet 192.168.60.128/24 brd 192.168.60.255 scope global dynamic noprefixroute ens160
valid_lft 1755sec preferred_lft 1755sec
inet6 fe80::20c:29ff:fe96:88aa/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:96:88:b4 brd ff:ff:ff:ff:ff:ff
altname enp19s0
inet 192.168.23.138/24 brd 192.168.23.255 scope global dynamic noprefixroute ens224
valid_lft 1755sec preferred_lft 1755sec
inet6 fe80::e817:bd6f:df08:c607/64 scope link noprefixroute
valid_lft forever preferred_lft forever
# 查看网络连接设备名称,注意网卡名称和驱动名称是不一样的
[root@lvs ~]# nmcli connection show
NAME UUID TYPE DEVICE
ens160 c1ddb566-f74f-3685-bcac-678da8f58efa ethernet ens160
lo d2977d80-8552-47ec-95dd-a87b7809be47 loopback lo
Wired connection 1 bfbd3c56-329a-38c2-89c1-bb9b514232a3 ethernet ens224
# 修改连接名称,以免每次都要输入'Wired connection 1'
[root@lvs ~]# nmcli c modify 'Wired connection 1' connection.id ens224
[root@lvs ~]# nmcli connection show
NAME UUID TYPE DEVICE
ens160 c1ddb566-f74f-3685-bcac-678da8f58efa ethernet ens160
ens224 bfbd3c56-329a-38c2-89c1-bb9b514232a3 ethernet ens224
lo d2977d80-8552-47ec-95dd-a87b7809be47 loopback lo
# 查看修改结果
[root@lvs ~]# nmcli connection show
NAME UUID TYPE DEVICE
ens160 c1ddb566-f74f-3685-bcac-678da8f58efa ethernet ens160
ens224 bfbd3c56-329a-38c2-89c1-bb9b514232a3 ethernet ens224
lo d2977d80-8552-47ec-95dd-a87b7809be47 loopback lo
软件安装
防止等会不能上外网,先把 ipvsamd 软件安装好。
[root@lvs ~]# dnf install ipvsadm -y
Updating Subscription Management repositories.
Unable to read consumer identity
Dependencies resolved.
=======================================================================================
Package Architecture Version Repository Size
=======================================================================================
Installing:
ipvsadm x86_64 1.31-6.el9 AppStream 54 k
Transaction Summary
=======================================================================================
...
Complete!
配置网卡
从 VMware 的 编辑 -> 虚拟网络适配器 选项中可以看到自己仅主机模式的子网IP,这里我的子网IP 为 192.168.60.0
# 仅主机模式网卡
[root@lvs ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.60.200/24 ipv4.gateway 192.168.60.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
[root@lvs ~]# nmcli c up ens160
# NAT 模式网卡
[root@lvs ~]# nmcli c modify ens224 ipv4.method manual ipv4.addresses 192.168.23.8/24 ipv4.gateway 192.168.23.2 connection.autoconnect yes
[root@lvs ~]# nmcli c up ens224
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
配置客户端
客户端的网络也采用仅主机模式,采用 60 网段
[root@server ~]# hostnamectl hostname client
[root@client ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.60.100/24 ipv4.gateway 192.168.60.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
[root@client ~]# nmcli c up ens160
启动服务
# 直接启动服务会报错
[root@lvs ~]# systemctl start ipvsadm.service
Job for ipvsadm.service failed because the control process exited with error code.
See "systemctl status ipvsadm.service" and "journalctl -xeu ipvsadm.service" for details.
# 需要保存当前的 IPVS 规则
[root@lvs ~]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@lvs ~]# systemctl start ipvsadm.service
规则匹配
# 此时客户端无法访问虚拟IP(VIP),既无法访问到后端 RS 服务器
[root@client ~]# curl 192.168.60.200
curl: (7) Failed to connect to 192.168.60.200 port 80: Connection refused
# 但是在 LVS 服务器中可以访问到后端 RS 服务器
[root@lvs ~]# curl 192.168.23.7
192.168.23.7
[root@lvs ~]# curl 192.168.23.17
192.168.23.17
[root@lvs ~]# curl 192.168.23.27
192.168.23.27
### 进行 LVS 规则匹配
## 配置一条规则
# -A 表示添加新的虚拟服务 -t 指定使用TCP连接(默认) 后面为当前 VIP 及端口
# -s rr 指定 负载均衡算法,rr 表示轮询,即轮流发送到所有真实服务器RS
[root@lvs ~]# ipvsadm -A -t 192.168.60.200:80 -s rr
## 添加规则
# -a 为存在的虚拟服务添加真实服务器RS -t 指定真实服务器RS的IP地址和端口
# -m 指定以 直接路由模式使用,表示LVS通过NAT转发流量到真正的服务
# -w 设置权重,表示该RS的负载均衡权重,用于 轮询策略
[root@lvs ~]# ipvsadm -a -t 192.168.60.200:80 -r 192.168.23.7:80 -m -w 1
[root@lvs ~]# ipvsadm -a -t 192.168.60.200:80 -r 192.168.23.17:80 -m -w 2
[root@lvs ~]# ipvsadm -a -t 192.168.60.200:80 -r 192.168.23.27:80 -m -w 3
## 查看规则列表
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.60.200:80 rr
-> 192.168.23.7:80 Masq 1 0 0
-> 192.168.23.17:80 Masq 2 0 0
-> 192.168.23.27:80 Masq 3 0 0
# 配置好后重启服务
[root@lvs ~]# systemctl restart ipvsadm.service
功能测试
# 重启完成后再次通过客户端测试,发现不报错但是没有数据返回
[root@client ~]# curl 192.168.60.200
### 启用IP转发,即启用 IPv4 的 IP 转发功能(1启用,0禁用)
# 将内核转发参数写进配置文件,重启后也能保持配置
[root@lvs ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
# 重新加载配置
[root@lvs ~]# sysctl -p
net.ipv4.ip_forward = 1
### 此时再进行访问,成功轮询
[root@client ~]# curl 192.168.60.200
192.168.23.7
[root@client ~]# curl 192.168.60.200
192.168.23.27
[root@client ~]# curl 192.168.60.200
192.168.23.17
[root@client ~]# curl 192.168.60.200
192.168.23.7
[root@client ~]# curl 192.168.60.200
192.168.23.27
[root@client ~]# curl 192.168.60.200
192.168.23.17
IP 转发的作用
启用 IP 转发允许 Linux 主机在两个或多个网络之间转发数据包。这对于以下场景至关重要:
路由器:使得 Linux 服务器可以作为路由器,转发数据包。
负载均衡器:在 LVS 等负载均衡配置中,能够将客户端请求分发到后端真实服务器。
网络安全:通常在 NAT(网络地址转换)场景中,需要 IP 转发来处理流量。