调度器会把用户的请求通过预设的iptables规则转发给后端的真实服务器。其中调度器有两个IP,一个是公网IP,一个是内网IP,而真实服务器只有内网IP。用户访问的时候请求的是调度器的公网IP,它会把用户的请求转发到真实服务器的内网IP上。这种模式的好处是节省公网IP,但是调度器会成为一个瓶颈。
其中调度器上有两块网卡,作为内网的这块网卡使用的是NAT的网络,而作为“公网”的网卡使用的是仅主机网络。需要注意,所谓的公网其实仅仅是模拟的,并不是真正意义上的公网。
模拟规则 | 主机名 | IP | 网关 |
---|---|---|---|
调度器 | dir | 192.168.200.130(内网,NAT模式) 192.168.100.130(外网,仅主机模式) |
192.168.200.2 |
真实服务器1 | rs1 | 192.168.200.131 | 192.168.200.130 |
真实服务器2 | rs2 | 192.168.200.132 | 192.168.200.130 |
1.三台服务器均关闭防火墙
# systemctl stop firewalld
# systemctl disable firewalld
# getenforce 0
# vim /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
三台服务器均安装iptables服务
# yum install -y iptables-services
# systemctl start iptables
# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
# iptables -F
# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
2.在dir上安装ipvsadm工具
# yum install -y ipvsadm
在dir上编写一个脚本:
# vim /usr/local/sbin/lvs_nat.sh
#! /bin/bash
# director 服务器上开启路由转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
# 关闭icmp的重定向
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
# 注意区分网卡名字
echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/ens34/send_redirects
# director 设置nat防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.200.0/24 -j MASQUERADE
# director设置ipvsadm
IPVSADM='/usr/sbin/ipvsadm'
$IPVSADM -C
$IPVSADM -A -t 192.168.147.144:80 -s wlc -p 300
$IPVSADM -a -t 192.168.147.144:80 -r 192.168.200.131:80 -m -w 1
$IPVSADM -a -t 192.168.147.144:80 -r 192.168.200.132:80 -m -w 1
执行脚本:
sh /usr/local/sbin/lvs_nat.sh
3.给rs1、rs2更改一个默认主页
# echo "rs1" > /usr/local/nginx/html/index.html //131上执行
# echo "rs2" > /usr/local/nginx/html/index.html //132上执行
4.在dir上分别访问两个rs
rs1:192.168.200.131
rs2:192.168.200.132
[root@dir ~]# curl 192.168.200.131
rs1
[root@dir ~]# curl 192.168.200.132
rs2
直接在dir上访问外网:
# curl 192.168.147.144
rs2
# curl 192.168.147.144
rs2
# curl 192.168.147.144
rs2
# curl 192.168.147.144
rs2
# curl 192.168.147.144
rs2
# curl 192.168.147.144
rs2
# curl 192.168.147.144
rs2
连续多次访问,一直请求在rs2上,是因为脚本中有设置-p参数,理论上在300秒内会一直请求在rs2上。重新编辑/usr/local/sbin/lvs_nat.sh脚本把-p参数删除,然后再次测试
# curl 192.168.147.144
rs2
# curl 192.168.147.144
rs1
# curl 192.168.147.144
rs2
# curl 192.168.147.144
rs1
# curl 192.168.147.144
rs2
# curl 192.168.147.144
rs1
这样就做到了均衡访问
如果出现以下问题
# curl 192.168.200.131
curl: (7) Failed connect to 192.168.200.131:80; Connection refused //拒绝访问
# curl 192.168.200.132
curl: (7) Failed connect to 192.168.200.132:80; Connection refused
原因可能有:
1.防火墙未完全关闭,查看防火墙是否全部关闭
systemctl stop firewalld //临时关闭防火墙
systemctl disable firewalld //永久关闭防火墙
setenforce 0
2.脚本中绑定的访问地址错误
# ipvsadm -At 192.168.10.10:80 -s rr
# ipvsadm -at 192.168.10.10:80 -r 10.0.0.3 -m
# ipvsadm -at 192.168.10.10:80 -r 10.0.0.4 -m
-at后面的IP应为外网IP