LVS/NAT 配置

参考  http://it165.net/admin/html/201401/2248.html

前期准备

需求:三台服务器一台作为director, 两台作为real server


Director 有一个外网ip (192.168.1.190)用桥接模式 和一个内网ip(192.168.11.190)NAT模式,

两个real server上只有内网ip(192.168.11.160)NAT模式和(192.168.11.20)NAT模式

并且需要把两个real server的内网网关设置为director的内网ip(192.168.11.190)

开始实验

两个real server 上都安装httpd: yum install -y nginx
Director上安装ipvsadm:  yum install -y  ipvsadm nginx
Direcotr 上 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/eth0/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/eth1/send_redirects

# director 设置nat防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.11.0/24  -j MASQUERADE
# director设置ipvsadm
IPVSADM='/sbin/ipvsadm'
$IPVSADM -C
$IPVSADM -A -t 192.168.1.190:80 -s rr
$IPVSADM -a -t 192.168.1.190:80 -r 192.168.11.160:80 -m
$IPVSADM -a -t 192.168.1.190:80 -r 192.168.11.20:80   -m
View Code

直接运行这个脚本就可以完成lvs/nat的配置了: 
sh /usr/local/sbin/lvs_nat.sh 


通过浏览器测试两台机器上的web内容,为了区分开,我们可以把nginx的默认页修改一下:
rs1上: echo "rs1rs1" >/usr/share/nginx/html/index.html
rs2上: echo "rs2rs2" >/usr/share/nginx/html/index.html

 

测试一:

[root@dir ~]# curl 192.168.1.190
rs1rs1
[root@dir ~]# curl 192.168.1.190
rs2rs2
[root@dir ~]# curl 192.168.1.190
rs1rs1
[root@dir ~]# curl 192.168.1.190
rs2rs2
[root@dir ~]# curl 192.168.1.190
rs1rs1
[root@dir ~]# curl 192.168.1.190
rs2rs2
View Code

测试二:

vim /usr/local/sbin/lvs_nat.sh

sh /usr/local/sbin/lvs_nat.sh

[root@dir ~]# curl 192.168.1.190
rs1rs1
[root@dir ~]# curl 192.168.1.190
rs1rs1
[root@dir ~]# curl 192.168.1.190
rs2rs2
[root@dir ~]# curl 192.168.1.190
rs1rs1
[root@dir ~]# curl 192.168.1.190
rs1rs1
[root@dir ~]# curl 192.168.1.190
rs2rs2
View Code

 

转载于:https://www.cnblogs.com/hyit/articles/5036657.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值