Linux学习-LVS服务器上Director和Real Server的配置脚本

Posted 丢爸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习-LVS服务器上Director和Real Server的配置脚本相关的知识,希望对你有一定的参考价值。

简单规划:
Director服务器IP:192.168.0.103
RS1服务器:192.168.0.109
RS2服务器:192.168.0.110
VIP:192.168.0.100
Director配置脚本如下:

#! /bin/bash
#
# LVS script for VS/DR
# 90为启动优先级,10为关闭优先级
# chkconfig: - 90 10

. /etc/init.d/functions

VIP=192.168.0.100
DIP=192.168.0.103
RIP1=192.168.0.109
RIP2=192.168.0.110
PORT=80
RSWEIGHT1=2
RSWEIGHT2=5

case "$1" in
start)
  if [ ! -e /var/lock/subsys/ipvsadm ]; then
    /sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
    /sbin/route add -host $VIP dev eth0:0

    #Director服务器必须打开ip_forward转换数据包功能
    echo 1 > /proc/sys/net/ipv4/ip_forward

    #清除iptables规则
    /sbin/iptables -F
    #将所有链中的数据包和字节计数器归零
    /sbin/iptables -Z
    #清除所有的ipvsadm规则和服务
    /sbin/ipvsadm -C
    #添加ipvsadm规则
    /sbin/ipvsadm -A -t $VIP:$PORT -s wlc
    #添加RealServer
    /sbin/ipvsadm -a -t $VIP:$PORT -r $RIP1 -g -w $RSWEIGHT1
    /sbin/ipvsadm -a -t $VIP:$PORT -r $RIP2 -g -w $RSWEIGHT2
    /bin/touch /var/lock/subsys/ipvsadm &> /dev/null
    echo "ipvsadm is start and configured..."
  else
    echo "ipvsadm is running..."
  fi
  ;;
 stop)
  #停止转发数据包
  echo 0 > /proc/sys/net/ipv4/ip_forward
  #清除ipvsadm规则
  /sbin/ipvsadm -C
  #停止 vip接口
  /sbin/ifconfig eth0:0 down
  /sbin/route del $VIP &> /dev/null
  /bin/rm -rf /var/lock/subsys/ipvsadm

  echo 'ipvsadm is stopped ...'
  ;;
 status)
  #判断/var/lock/subsys/ipvsadm文件是否存在
  if [ ! -e /var/lock/subsys/ipvsadm ]; then
   echo "ipvsadm is stopped..."
  else
   echo "ipvsadm is running..."
   ipvsadm -Ln
  fi
  ;;
 *)
  echo "Usage:$0 {start|stop|status}"
  ;;
 esac

RealServer配置脚本

#!/bin/bash
#
# 启动LVS DR real server
# chkconfig: - 90 10
#

. /etc/init.d/functions

VIP=192.168.0.100
HOST=`/bin/hostname`
case "$1" in
start)
  #启动当前机器LVS-DR real server
  /sbin/ifconfig lo down
  /sbin/ifconfig lo up
  echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
  echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
  echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
  echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

  /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
  /sbin/route add -host $VIP dev lo:0
  ;;
stop)
  /sbin/ifconfig lo down
  /sbin/ifconfig lo up
  echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
  echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
  echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
  echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

  /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
  /sbin/route add -host $VIP dev lo:0
  ;;
stop)
  #停止LVS-DR real server
  /sbin/ifconfig lo:0 down
  echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
  echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
  echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
  echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
  ;;
status)
  #LVS-DR real server的状态
  islothere=`/sbin/ifconfig lo:0 | grep $VIP`
  isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
  if [ ! "$islothere" -o ! "$isrothere" ];then
    echo "LVS-DR real server stopped."
  else
    echo "LVS-DR real server Running."
  fi
  ;;
*)
  echo "$0 : Usage: $0 {start|stop|status}"
  exit 1
  ;;
esac

以上是关于Linux学习-LVS服务器上Director和Real Server的配置脚本的主要内容,如果未能解决你的问题,请参考以下文章

Linux学习114 LVS实现企业负载均衡高级应用配置

lvs-nat模式

Linux CentOs集群LVS负载均衡的实现

Linux LVS+Keepalived+Httpd配置

LVS集群

LVS实现负载均衡安装配置详解