linux运维—LVS(NAT模式)

NAT方式原理图

在这里插入图片描述

  • 当用户请求到达Director Server,此时请求的数据报文会先到内核空间的PREROUTING链。 此时报文的源IP为CIP,目标IP为VIP
  • PREROUTING检查发现数据包的目标IP是本机,将数据包送至INPUT链
  • IPVS比对数据包请求的服务是否为集群服务,若是,修改数据包的目标IP地址为后端服务器IP,然后将数据包发至POSTROUTING链。 此时报文的源IP为CIP,目标IP为RIP
  • POSTROUTING链通过选路,将数据包发送给Real Server
  • Real Server比对发现目标为自己的IP,开始构建响应报文发回给Director Server。 此时报文的源IP为RIP,目标IP为CIP
  • Director Server在响应客户端前,此时会将源IP地址修改为自己的VIP地址,然后响应给客户端。 此时报文的源IP为VIP,目标IP为CIP
LVS-NAT模型的特性
  • RS应该使用私有地址,RS的网关必须指向DIP
  • DIP和RIP必须在同一个网段内
  • 请求和响应报文都需要经过Director Server,高负载场景中,Director Server易成为性能瓶颈
  • 支持端口映射
  • RS可以使用任意操作系统
  • 缺陷:对Director Server压力会比较大,请求和响应都需经过director server

以下实验在7.3主机上进行

调度器设置:

1、设置两个不同网段的ip,一个用来接收客户端的请求,另一个作为真实服务器的网关。

[root@toto1 ~]# ip addr add 172.25.254.110/24 dev eth0   # 添加ip
[root@toto1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:5e:44:1d brd ff:ff:ff:ff:ff:ff
    inet 172.25.47.110/24 brd 172.25.47.255 scope global eth0   # 作为内网地址,与真实服务器交流
       valid_lft forever preferred_lft forever
    inet 172.25.254.110/24 scope global eth0   # 作为对外的地址,接收客户端的请求
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe5e:441d/64 scope link 
       valid_lft forever preferred_lft forever

2 安装lvs管理工具——ipvsadm服务

[root@toto1 ~]# yum install ipvsadm -y

3 添加vip以及真实服务器到调度器

[root@toto1 ~]# ipvsadm -A -t 172.25.254.110:80 -s rr
#-A: 添加vip。 -t: tcp协议 。-s: lvs调度算法 rr 轮叫调度 
[root@toto1 ~]# ipvsadm -a -t 172.25.254.110:80 -r 172.25.47.120:80 -m  
[root@toto1 ~]# ipvsadm -a -t 172.25.254.110:80 -r 172.25.47.130:80 -m  
# 添加真实服务器,使用nat模式的时候,后面的参数为 -m
[root@toto4 ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  toto4:http rr
  -> toto2:http                   Masq    1      0          0         
  -> toto3:http                   Masq    1      0          0       

4 开启调度器的内核路由功能

[root@toto4 ~]# sysctl -a |grep ip_forward
net.ipv4.ip_forward = 0
[root@toto4 ~]# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1

5 加载模块

modprobe iptable_nat

如果不加载此模块,也可以在第一次访问时成功,但是会在再次访问时出现延迟过长,或访问超时现象

真实服务器设置:

两台真实服务器需要安装httpd服务并设置号资源,设定网关为调度器的对内ip

[root@toto2 ~]# yum install httpd -y  # 安装服务
[root@toto2 ~]# echo toto2 > /var/www/html/index.html   # 设定服务器资源页面
[root@toto2 ~]# systemctl start httpd  #启动服务
[root@toto2 ~]# systemctl enable httpd   # 设置服务开机自动启动

[root@toto2 ~]# route -n 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.47.110   0.0.0.0         UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
172.25.47.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
[root@toto3 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:30:0c:b2 brd ff:ff:ff:ff:ff:ff
    inet 172.25.47.130/24 brd 172.25.47.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe30:cb2/64 scope link 
       valid_lft forever preferred_lft forever
测试:
[root@foundation47 ~]# curl 172.25.254.110
toto3
[root@foundation47 ~]# curl 172.25.254.110
toto2
[root@foundation47 ~]# curl 172.25.254.110
toto3
[root@foundation47 ~]# curl 172.25.254.110
toto2
[root@foundation47 ~]# curl 172.25.254.110
toto3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值