Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32459
  • 博文数量: 12
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-02 21:50
文章分类

全部博文(12)

文章存档

2011年(1)

2009年(11)

我的朋友

分类: 系统运维

2009-04-04 11:26:26

按照上篇文章VMware+Redhat9环境下实现LVS负载均衡系统(一、内核补丁及管理软件)的方法为负载均衡器打好了系统补丁,安装好了管理软件。现在需要告诉负载均衡器如何使用在前一步骤中编译和安装的ipvsadm工具将数据包转发到集群节点(真实服务器 RealServer)。

    执行这项工作的一种方式就是使用LVS发行版中包括的配置脚本(有关如何使用这样方法来配置LVS集群的描述,请参阅 htt://上的LVS HOWTO)。但在这里,我们将用自己配置的脚步来创建自己的LVS集群,以便可以学习更多的关于ipvsadm工具的内容。

    VIP=192.168.138.100

    DIP=10.1.1.1

    RIP=10.1.1.2; 127.0.0.1( 把负载均衡器也作为一台真实服务器)

    创建如下所示的/etc/init.d/lvs脚本

#!/bin/bash
#
# LVS script
#
# chkconfig: 2345 99 90
# description: LVS sample script
#
start(){
 # Bring up the VIP (Normally this should be under Heartbeat's control)
 /sbin/ifconfig eth0 192.168.138.100 netmask 255.255.255.0 up
 #Since this if the Director we must be able to forward packets
 echo 1 > /proc/sys/net/ipv4/ip_forward

 # Clear all iptables rules.
 /sbin/iptables -F
 # Reset iptables counters.
 /sbin/iptables -Z
 # Clear all ipvsadm rules/services.
 /sbin/ipvsadm -C
 # Add an IP virtual service for VIP 192.168.138.100 port 80
 /sbin/ipvsadm -A -t 192.168.138.100:80 -s rr
 # Now director packets for this VIP to the real server IP (RIP) inside the cluster
 /sbin/ipvsadm -a -t 192.168.138.100:80 -r 10.1.1.2 -m
 # And send requests to the locally running Apache server on the Director
 /sbin/ipvsadm -a -t 192.168.138.100:80 -r 127.0.0.1 -m 
}
stop(){
 # Stop forwardding packets
 echo 0 > /proc/sys/net/ipv4/ip_forward
 # Reset ipvsadm
 /sbin/ipvsadm -C
 # Bring down the VIP interface
 ifconfig eth0 down
}
case "$1" in
start)
 start
 ;;

stop)
 stop
 ;;
restart)
 stop
 start
 ;;

*)
 echo "Usage: $0 {start|stop|restart}"
 ;;

esac

    就可以用/etc/init.d/lvs start命令启动lvs,用/etc/init.d/lvs stop命令停止lvs,用/etc/init.d/lvs restart 命令重启lvs。

    当然也可执行 chkconfig --add lvs 来添加服务,这样就只要执行service lvs start命令就可以启动lvs了。

 

    在本例中,负载均衡器中也作为了一台真实服务器,当然也可以不这么做,那么就把“ /sbin/ipvsadm -a -t 192.168.138.100:80 -r 127.0.0.1 -m ”去掉。

----------------------------------附录----------------------------------------------

 

ipvsadm的参数----------调度方法

-s rr               ---------- 循环

-s wrr            ----------带权循环

-s lc               ----------最小连接

-s wlc            ---------- 带权最小连接

-s lblc            ----------基于位置的最小连接

-s lblcr           ----------基于位置的最小连接(带复制)

-s dh             ----------目的地址散列

-s sh             ---------- 源地址散列

-s sed           ----------最短期望延时

-s nq             ----------无须队列等待

 

ipvsadm的参数----------转发方式

-g                  ----------LVS-DR

-i                   ----------LVS-TUN

-m                 ----------LVS-NAT

报告输出         ----------转发方式

Masp             ----------LVS-NAT

Route            ----------LVS-DR

Tunnel           ----------LVS-TUN


阅读(691) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~