VMware+Redhat9环境下实现LVS负载均衡系统(二、LVS配置脚本)

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

    执行这项工作的一种方式就是使用LVS发行版中包括的配置脚本(有关如何使用这样方法来配置LVS集群的描述,请参阅htt://www.linuxvirtualserver.org上的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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值