Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2269109
  • 博文数量: 168
  • 博客积分: 6641
  • 博客等级: 准将
  • 技术积分: 1996
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-02 11:49
文章存档

2020年(4)

2019年(6)

2017年(1)

2016年(3)

2015年(3)

2014年(8)

2013年(2)

2012年(12)

2011年(19)

2010年(10)

2009年(3)

2008年(17)

2007年(80)

分类: LINUX

2008-11-21 18:42:59

基于NAT、DR方式的LVS负载均衡简记
 
2008-11-22 TsengYia#126.com http://tsengyia.blog.chinaunix.net/
 
################################################################
系统环境:RHEL5 [ 2.6.18-8.el5xen ]

软件环境:
    ipvsadm-1.24-8.1.i386
    httpd-2.2.3-6.el5
 
    在RHEL5系统中,默认已编译好实现IP Virtual Server功能的内核模块,可以使用“modprobe -l | grep ipvs”查看。
##########################################################################
 
◆ 安装ipvsadm管理工具
 
shell> mount /dev/cdrom /media/cdrom        #//挂载RHEL5安装光盘
shell> rpm -ivh /media/cdrom/Cluster/ipvsadm-1.24-8.1.i386.rpm
 
 
-------------------------------------------------------------
◆ 方式一:基于NAT(地址转换)的负载均衡
 
 
    基于地址转换的方式,LVS主节点负责入站数据的负载分配(按一定算法分流到后端的各内容服务器),各内容服务器的出站数据也经过LVS主节点。通常LVS主节点作为各内容服务器的网关。
 
    1、配置Linux Virtual Server主节点
      1) 修改proc参数
shell> vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
net.ipv4.conf.eth1.send_redirects = 0
shell> sysctl -p
 
      2) 添加负载均衡规则
shell> ipvsadm -A -t 173.17.17.1:80 -s rr        #//添加VirtIP虚拟IP,使用轮询算法(Round Robin)分配负载
shell> ipvsadm -a -t 173.17.17.1:80 -r 192.168.1.17:80 -m -w 1 -p 60       #//-m 表示使用nat方式
shell> ipvsadm -a -t 173.17.17.1:80 -r 192.168.1.18:80 -m -w 1 -p 60       #//-w 1 表示服务器的权重
shell> ipvsadm -a -t 173.17.17.1:80 -r 192.168.1.19:80 -m -w 1 -p 60       #//-w 0 将该rserver设置为停止状态,以便于维护
shell> ipvsadm -a -t 173.17.17.1:80 -r 192.168.1.20:80 -m -w 1 -p 60       #//-p 60 设置连接保持超时(同一客户端连接同一个r-server)
 
      3) 保存规则并设置服务状态(LVS测试正常后再进行此步骤)
shell> ipvsadm-save > /etc/sysconfig/ipvsadm
shell> chkconfig --level 35 ipvsadm on
 
    2、配置各Real Server节点(以Web服务为例)
        配置多台主机提供httpd服务,网关指向LVS主节点(192.168.1.1),各服务器的Web服务保持镜像同步(过程略)。
        ----测试阶段可以使用不同内容的web服务,以查看效果。
 
 
-------------------------------------------------------------
◆ 方式二:基于DR(直接路由)的负载均衡
 
 
    基于直接路由的方式,LVS主节点只负责入站数据的负载分配(按一定算法分流到后端的各内容服务器),各内容服务器的出站数据经各自的网关服务器发往Internet(无需再经过LVS主节点)。
 
    1、配置Linux Virtual Server主节点
      1) 修改proc参数
shell> vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
net.ipv4.conf.eth1.send_redirects = 0
shell> sysctl -p
 
      2) 添加负载均衡规则
shell> ipvsadm -A -t 173.17.17.1:80 -s rr        #//添加VirtIP虚拟IP,使用轮询算法(Round Robin)分配负载
shell> ipvsadm -a -t 173.17.17.1:80 -r 173.17.17.17:80 -g -w 1 -p 60       #//-g 表示使用dr方式
shell> ipvsadm -a -t 173.17.17.1:80 -r 173.17.17.18:80 -g -w 1 -p 60
shell> ipvsadm -a -t 173.17.17.1:80 -r 173.17.17.19:80 -g -w 1 -p 60
shell> ipvsadm -a -t 173.17.17.1:80 -r 173.17.17.20:80 -g -w 1 -p 60
 
      3) 添加虚拟IP地址
shell> vi /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0
BROADCAST=173.17.17.255
IPADDR=173.17.17.1
NETMASK=255.255.255.0
ONBOOT=yes
TYPE=Ethernet
shell> service network restart
 
      4) 保存规则并设置服务状态(LVS测试正常后再进行此步骤)
shell> ipvsadm-save > /etc/sysconfig/ipvsadm
shell> chkconfig --level 35 ipvsadm on
 
    2、配置各Real Server节点(以Web服务为例)
        配置多台主机提供httpd服务,各服务器保持镜像同步(过程略)。
        ----测试阶段可以使用不同内容的web服务,以查看效果。
 
      1) 修改proc参数
shell> vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
shell> sysctl -p
 
      2) 添加虚拟地址及路由记录
shell> vi /opt/add_vip.sh
#!/bin/bash
ifconfig lo:0 173.17.17.1 netmask 255.255.255.255 up
route add -host 173.17.17.1 dev lo:0
shell> chmod +x /opt/add_vip.sh
shell> /opt/add_vip.sh
shell> echo "/opt/add_vip.sh"  >> /etc/rc.local
 

-------------------------------------------------------------
◆ 验证LVS
    1、在173.17.17.1服务器上执行“ipvsadm -L -n”命令,
    基于NAT方式时显示如下:
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  173.17.17.1:80 rr
  -> 192.168.1.17:80             Masq    1      0          0
  -> 192.168.1.18:80             Masq    1      0          0
  -> 192.168.1.19:80             Masq    1      0          0
  -> 192.168.1.20:80             Masq    1      0          0
    基于DR方式时显示如下:
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  173.17.17.1:http rr
  -> 173.17.17.17:80             Masq    1      0          0
  -> 173.17.17.18:80             Masq    1      0          0
  -> 173.17.17.19:80             Masq    1      0          0
  -> 173.17.17.20:80             Masq    1      0          0
 
    2、使用外部客户端浏览器多次访问,并检查4台真实web服务器的访问日志
        如果添加realserver时设置了-p 60参数,则同一个客户端每次刷新页面时,连接的是同一个real server,否则会轮流更替。
        设置连接保持(-p)参数是为了确保ftp、ssl等连接的稳定和可靠性。
阅读(2252) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~