lvs配置之NAT模式

2021/5/6 18:28:46

本文主要是介绍lvs配置之NAT模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

LVS NAT模式配置

环境

系统 IP
Red Hat8 DR  dip:192.168.100.131 vip:192.168.18.250
Red Hat8 RS1  192.168.100.132
Red Hat8 RS2  192.168.100.133

 

准备工作

DR上要有两块网卡

一张是nat模式(对内网)一张是仅主机模式(对外网)

 

 准备工作

//安装ipvsadm
[root@DR ~]# dnf -y install ipvsadm
[root@RS1 ~]# dnf -y install ipvsadm
[root@RS2 ~]# dnf -y install ipvsadm

//RS1.RS2安装httpd
#dnf -y install httpd

//RS1.RS2.DR关闭防火墙和selinux
#systemctl stop firewalld
#setenforce 0

//RS1.RS2设置开机自启并立即启动httpd
#systemctl enable --now httpd

//创建测试页(如果想直观查看测试结果可以做这一步,否则可以跳过)
[root@RS1 ~]# echo "RS1" > /var/www/html/index.html
[root@RS2 ~]# echo "RS2" > /var/www/html/index.html

部署环境(DR网卡部分)

//开启IP转发功能
[root@DR ~]# vim /etc/sysctl.conf
...
net.ipv4.ip_forward = 1
[root@DR ~]# sysctl -p
net.ipv4.ip_forward = 1

//配置dip
[root@DR ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens160 

TYPE="Ethernet"
BOOTPROTO="static"
NAME="ens160"
UUID="bcaa49a1-416e-423f-aee9-4c3a2c829950"
DEVICE="ens160"
ONBOOT="yes"
IPADDR=192.168.100.131
NETMASK=255.255.255.0

//配置vip
[root@DR ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens224 

TYPE="Ethernet"
BOOTPROTO="static"
NAME="ens224"
UUID="bcaa49a1-416e-423f-aee9-4c3a2c829950"
DEVICE="ens224"
ONBOOT="yes"
IPADDR=192.168.18.250
NETMASK=255.255.255.0

//重载网卡
[root@DR ~]# systemctl restart NetworkManager
[root@DR ~]# ifdown ens160;ifup ens160
[root@DR ~]# ifdown ens224;ifup ens224

//查看配置
[root@DR ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:92:d4:ab brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.131/24 brd 192.168.100.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe92:d4ab/64 scope link 
       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:92:d4:b5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.18.250/24 brd 192.168.18.255 scope global noprefixroute ens224
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe92:d4b5/64 scope link 
       valid_lft forever preferred_lft forever

[root@DR ~]# ip route list
192.168.18.0/24 dev ens224 proto kernel scope link src 192.168.18.250 metric 101 
192.168.100.0/24 dev ens160 proto kernel scope link src 192.168.100.131 metric 100 
//配置ipvsadm
[root@DR ~]# ipvsadm -A -t 192.168.18.250:80 -s rr
[root@DR ~]# ipvsadm -a -t 192.168.18.250:80 -r 192.168.100.132:80 -m
[root@DR ~]# ipvsadm -a -t 192.168.18.250:80 -r 192.168.100.133:80 -m

//保存配置到文件里
[root@DR ~]# ipvsadm -S > /etc/sysconfig/ipvsadm

配置环境(RS网卡部分)

//RS1配置网卡,将网关指向dip
[root@RS1 network-scripts]# vim /etc/sysconfig/network-scripts/ifcfg-ens160 

TYPE="Ethernet"
BOOTPROTO="static"
NAME="ens160"
UUID="bcaa49a1-416e-423f-aee9-4c3a2c829950"
DEVICE="ens160"
ONBOOT="yes"
IPADDR=192.168.100.132
GATEWAY=192.168.100.131
NETMASK=255.255.255.0

//RS2配置网卡,将网关指向dip
[root@RS2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens160 

TYPE="Ethernet"
BOOTPROTO="dhcp"
NAME="ens160"
UUID="bcaa49a1-416e-423f-aee9-4c3a2c829950"
DEVICE="ens160"
ONBOOT="yes"
IPADDR=192.168.100.133
NETMASK=255.255.255.0
GATEWAY=192.168.100.131

//重载网卡(两台RS都执行)
# systemctl restart NetworkManager

[root@RS1 ~]# ifdown ens160;ifup ens160
[root@RS2 ~]# ifdown ens160;ifup ens160

测试

[root@DR ~]# 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.18.250:80 rr
  -> 192.168.100.132:80           Masq    1      1          2         
  -> 192.168.100.133:80           Masq    1      0          2 

 两边的RS都有被访问到,功能实现



这篇关于lvs配置之NAT模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程