LVS——nat模式

一、搭建nat模式下LVS的实验环境

1.创建四台虚拟机

client------客户端:192.168.134.111/24(nat模式)

LVS------调度器【双网卡】:192.168.134.112/24(nat模式)、172.25.254.111/24(仅主机模式)

RS1------服务器:172.25.254.10(仅主机模式)

RS2------服务器:172.25.254.20(仅主机模式)

2.给两台测试主机下载httpd服务
bash 复制代码
[root@RS1 ~]# dnf install httpd -y
[root@RS1 ~]# systemctl enable --now httpd


[root@RS2 ~]#  dnf install httpd -y
[root@RS2 ~]# systemctl enable --now httpd


(注:下载完httpd服务后别忘了启动该服务)
使用:
[root@LVS ~]# ss -tulpn | grep :80
tcp   LISTEN 0      511                *:80              *:*    users:(("httpd",               pid=30465,fd=4),("httpd",pid=30464,fd=4),("httpd",pid=30463,fd=4),("httpd",pid=3               0461,fd=4))

可以通过端口查看该服务是否开启
3.关闭RS1、RS2虚拟机的防火墙
bash 复制代码
[root@RS1 ~]# systemctl disable --now firewalld
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".

[root@RS2 ~]# systemctl disable --now firewalld
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
记得查看防火墙状态:
systemctl status firewalld
4.给RS1、RS2两台服务器开启web服务
bash 复制代码
[root@RS1 ~]# mkdir -p /var/www/html
[root@RS1 ~]# echo "RS1 -- 172.25.254.10" > /var/www/html/index.html


[root@RS2 ~]# mkdir -p /var/www/html
[root@RS2 ~]# echo "RS2 -- 172.25.254.20" > /var/www/html/index.html
5.保证LVS能访问到RS1、RS2两台服务器的web服务
bash 复制代码
[root@LVS ~]# curl 172.25.254.10
RS1 -- 172.25.254.10
[root@LVS ~]# curl 172.25.254.20
RS2 -- 172.25.254.20
6.修改LVS内核参数,打开内核路由功能,使系统内部不同网段网络可达(IP转发)
bash 复制代码
[root@LVS ~]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 0
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0
[root@LVS ~]# vim /etc/sysctl.conf
[root@LVS ~]# sysctl -p
net.ipv4.ip_forward = 1
7.接着为调度器LVS安装ipvsadm
bash 复制代码
[root@LVS ~]# dnf install ipvsadm -y

二、如果要使客户端到RS1、RS2这几台虚拟机可以互相通信,那么还需修改网关

bash 复制代码
RS1:
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
method=manual
address1=172.25.254.10/24,172.25.254.111
dns=8.8.8.8

RS2:
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
method=manual
address1=172.25.254.20/24,172.25.254.111
dns=8.8.8.8

记得修改完之后要
nmcli connection reload 
nmcli connection up eth0

效果为: