LB 负载均衡NAT模式 实验笔记

回复 收藏
应用环境:每台服务器都有一定的承载负荷上限,当单台服务器负荷达到上限,不能再给应用服务提供良好的性能体验时,就需要增加额外的服务器组成集群,分摊负载压力
常见负载均衡应用有nginx,nginx工作在第七层应用层。
LVS工作在第四层传输层。
Keepalive
负载均衡架构图


Director server:资源调度分发服务器,接收外部的用户访问请求,然后将请求按调度分发给下面的集群节点服务器,这样就起到了负载均衡的作用
LVS有三种模式:1. NAT模式

实验环境:
1.LB负载均衡服务器
计算机名:chiang139
网卡eth0
Ip:100.100.101.139
Netmask:255.255.254.0
Getewat:100.100.100.1    ##eth0模拟公网ip,接外网的网卡,
网卡eth1   
Ip:192.168.10.139  
Nemask:255.255.255.0
Gateway:不设置       ##eth1为内网网卡,与后端的服务器在同一网络,能互相访问

2.RS后端服务器a
计算机名:chiang137
网卡eth0     
Ip: 192.168.10.137
Netmask:255.255.255.0
Gateway:192.168.10.139    ##与LB调度服务器的内网ip同一个网络,能互相访问,网关为LB调度器的内网ip
RS后端服务器b
计算机名:chiang138
网卡eth0  
Ip:192.168.10.138
Netmask:255.255.255.0  
Gateway:192.168.10.139   ##与LB调度服务器的内网ip同一个网络,能互相访问,网关为LB调度器的内网ip

[root@chiang139 ~]# yum install -y ipvsadm    ##在LB调度服务器上安装ipvsadm,主要作用负责数据分发(负载均衡)
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.10.0/24  -j MASQUERADE
# director设置ipvsadm
IPVSADM='/sbin/ipvsadm'
$IPVSADM -C
$IPVSADM -A -t 100.100.101.139:80 -s rr
$IPVSADM -a -t 100.100.101.139:80 -r 192.168.10.138:80 -m
$IPVSADM -a -t 100.100.101.139:80 -r 192.168.10.137:80 -m

然后保存ipvsadm设置
[root@chiang139 ~]# service ipvsadm save
查看ipvsadm的设置规则,确定与lvs_nat.sh脚本里设置的一致
[root@chiang139 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  100.100.101.139:80 rr
  -> 192.168.10.137:80            Masq    1      0          0         
  -> 192.168.10.138:80            Masq    1      0          0

RS后端服务器a和b都安装上httpd并且运行,为了保证实验效果先将iptables和ip6tables都全部清空规则并停止服务
[root@chiang137 ~]# iptables -F
[root@chiang137 ~]# service iptables stop
[root@chiang138 ~]# iptables -F
[root@chiang138 ~]# service iptables stop

分别在RS后端服务器a和b上编辑测试页面,为了更好看出效果,内容区分一下
[root@chiang137 ~]# cat /var/www/html/index.html
this is chiang137 /var/www/html/index.htm
[root@chiang138 ~]# cat /var/www/html/index.html
this is chiang138 /var/www/html/index.htm

在LB负载均衡服务器上确定可以正确访问RS后端服务器的页面
[root@chiang139 ~]# curl 192.168.10.137
this is chiang137 /var/www/html/index.htm
[root@chiang139 ~]# curl 192.168.10.138
this is chiang138 /var/www/html/index.html

最后再确定负载均衡服务器上路由转发状态已经打开,脚本里echo 1 > /proc/sys/net/ipv4/ip_forward这个命令重启网络服务或主机后就会失效这个值就会变回0
[root@chiang131 ~]# cat /proc/sys/net/ipv4/ip_forward     
1
这里最好是编辑/etc/sysctl.conf更改下面的参数使得路由转发状态永久打开
sysctl -w net.ipv4.ip_forward=0改为sysctl -w net.ipv4.ip_forward=1
然后重新加载sysctl配置
[root@chiang139 ~]# sysctl -p

随意打开一个客户端,能访问到100.100.101.139负载均衡服务器外网ip的都可以,这里我用了一台其他网段的Linux主机
[root@server1721632 ~]# curl 100.100.101.131
this is chiang137 /var/www/html/index.htm
[root@server1721632 ~]# curl 100.100.101.131
this is chiang138 /var/www/html/index.html

测试成功
2016-01-14 01:15 举报
已邀请:
0

boy461205160

赞同来自:

支持原创
0

loading丶

赞同来自:

学习下

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: