NAT模式·LVS搭建
NET模式下,调度器需要有两个IP,一个公网IP一个内网IP,真实服务器只需要内网IP。此架构需要准备三台虚拟机。三台虚拟机的IP分配如下:
调度器dir:192.168.233.83(内网IP,vmware为NAT模式),192.168.133.0(公网IP,vmware仅主机模式)。
真实服务器rs1:192.168.233.84(内网IP)
真实服务器rs2:192.168.233.85(内网IP)
其中调度器上有两块网卡,作为内网的这块网卡使用的是NAT的网络,而作为“公网”的网卡使用的是仅主机网络。需要注意,所谓的公网其实仅仅是模拟的,并不是真正意义上的公网。在配置LVS之前,需要做一些准备工作。首先,真实服务器rs1(192.168.200.131)和rs2(192.168.200.132)上要把内网的网关设置为dir的内网IP(192.168.200.130),否则实验无法成功。
安装Nginx
Nginx安装
Nginx官网地址:nginx: download
这两个随便下载一个,传到你的服务器上
yum这几个包
[root@Nginx ~]# yum -y install pcre-devel openssl openssl-devel gcc gcc-c++
开启Nginx
[root@Nginx ~]# ll 总用量 1056 -rw-------. 1 root root 1311 12月 20 22:36 anaconda-ks.cfg -rw-r--r-- 1 root root 1073364 2月 28 17:32 Nginx-1.21.6.tar.gz [root@Nginx ~]# tar -zxvf Nginx-1.21.6.tar.gz [root@Nginx ~]# cd Nginx-1.21.6 [root@Nginx Nginx-1.21.6]# ./configure [root@Nginx Nginx-1.21.6]# make && make install [root@Nginx Nginx-1.21.6]# cd /usr/local/Nginx/sbin/ [root@Nginx sbin]# ./Nginx [root@Nginx sbin]# netstat -lntp |grep Nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4539/Nginx: master
访问服务器
用浏览器访问服务器,然后是这样的话,那么你成功了。 192.168.233.83:80端口
把三台服务器上的iptables规则清空并保存,命令如下:
# iptables -F; iptables -t nat -F; service iptables save //三台机器上都要执行
在dir上安装ipvsadm,这是实现LVS的核心工具:
[root@dir ~]# yum install -y ipvsadm
继续在dir上编写一个脚本:
[root@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.233.0/24 -j MASQUERADE # director设置ipvsadm IPVSADM='/usr/sbin/ipvsadm' $IPVSADM -C $IPVSADM -A -t 192.168.233.83:80 -s wlc -p 300 $IPVSADM -a -t 192.168.233.84:80 -r 192.168.200.131:80 -m -w 1 $IPVSADM -a -t 192.168.233.85:80 -r 192.168.200.132:80 -m -w 1
脚本编写完成后,直接执行,命令如下:
[root@dir ~]# bash /usr/local/sbin/lvs_nat.sh
下面测试LVS的效果,如果dir上有Nginx服务器,需要先把它关闭,否则会影响实验效果:
[root@dir ~]# killall Nginx
为了更容易方便区分,分别需要给84、85设置一个默认主页,命令如下:
[root@rs1 ~]# echo "rs1" > /usr/share/Nginx/html/index.html //84上执行 [root@rs2 ~]# echo "rs2" > /usr/local/Nginx/html/index.html //85上执行
在dir上分别访问两个rs,如下所示:
[root@dir ~]# curl 192.168.233.84 rs1 [root@dir ~]# curl 192.168.233.85 rs2
这样就区分了rs1和rs2,然后直接在dir上访问dir的外网(192.168.147.144),结果如下:
[root@dir ~]# curl 192.168.200.100 rs2 [root@dir ~]# curl 192.168.200.100 rs2 [root@dir ~]# curl 192.168.200.100 rs2 [root@dir ~]# curl 192.168.200.100 rs2 [root@dir ~]# curl 192.168.200.100 rs2 [root@dir ~]# curl 192.168.200.100 rs2 [root@dir ~]# curl 192.168.200.100 rs2
连续多次访问,一直请求在rs2上,是因为脚本中有设置-p参数,理论上在300秒内会一直请求在rs2上。重新编辑/usr/local/sbin/lvs_nat.sh脚本把-p参数删除,然后再次测试,结果如下:
[root@dir ~]# curl 192.168.200.100 rs2 [root@dir ~]# curl 192.168.200.100 rs1 [root@dir ~]# curl 192.168.200.100 rs2 [root@dir ~]# curl 192.168.200.100 rs1 [root@dir ~]# curl 192.168.200.100 rs2 [root@dir ~]# curl 192.168.200.100 rs1
这样就做到了均衡访问。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。