首页 > 代码库 > lvs 负载均衡环境搭建

lvs 负载均衡环境搭建

记录一下搭建lvs环境的步骤

当中master 10.0.0.11,backup 10.0.0.15。realserver1 10.0.0.119。realserver210.0.0.11

1.mkdir /usr/local/src/lvs

2.cd /usr/local/src/lvs
3.上传keepalived-1.1.20.tar.gz ipvsadm-1.24.tar.gz到指定目录
4.lsmod |grep ip_vs
5.uname -r
结果:2.6.32-431.el6.x86_64
6.ln -s /usr/src/kernels/2.6.32-431.el6.x86_64/  /usr/src/linux
7  tar zxvf ipvsadm-1.24.tar.gz
    cd ipvsadm-1.24
    make && make install
然后用 ipvsadm  进行測试

8. tar zxvf keepalived-1.1.20.tar.gz
cd keepalived-1.1.20
 ./configure  && make && make install
    find / -name keepalived  # 查看keepalived位置      
9. cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
    cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
    mkdir /etc/keepalived
    cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
    cp /usr/local/sbin/keepalived /usr/sbin/
    service keepalived start|stop     #做成系统启动服务方便管理.  

10
 vim /etc/init.d/lvsdr
    
    #!/bin/bash
    RIP1=10.0.0.19
    RIP2=10.0.0.23
    
    VIP=10.0.0.11

    . /etc/rc.d/init.d/functions
    case "$1" in
        start)
            echo " start LVS of DirectorServer"

            # set the Virtual IP Address and sysctl parameter
            /sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
            /sbin/route add -host $VIP dev eth0:0
            echo "1" >/proc/sys/net/ipv4/ip_forward

            #Clear IPVS table
            /sbin/ipvsadm -C
            
            #set LVS
            #Web Apache
             /sbin/ipvsadm -A -t $VIP:80 -s rr
             /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
             /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
              
            #Run LVS
             /sbin/ipvsadm
          ;;
        stop)
            echo "close LVS Directorserver"
            echo "0" >/proc/sys/net/ipv4/ip_forward
            /sbin/ipvsadm -C
            /sbin/ifconfig eth0:0 down
          ;;
        *)
            echo "Usage: $0 {start|stop}"
            exit 1
    esac
    
 11.   chmod a+x /etc/init.d/lvsdr
    service lvsdr start|stop
12.查看结果
  ipvsadm -ln

13配置Realserver脚本
    vim /etc/init.d/realserver
    #!/bin/bash

    VIP=10.0.0.11

    . /etc/rc.d/init.d/functions
    case "$1" in
        start)
            echo " start LVS of REALServer"
            /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
            echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
            echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
            echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
            echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
            ;;
        stop)
            /sbin/ifconfig lo:0 down
            echo "close LVS Directorserver"
            echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
            echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
            echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
            echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
            ;;
        *)
            echo "Usage: $0 {start|stop}"
            exit 1
    esac
    chmod a+x /etc/init.d/realserver
    service realserver start|stop
    
    能够通过ifconfig或者ip add 命令查看是否绑定
    

    备注:此脚本在两台realserver上均须运行。
   

lvs 负载均衡环境搭建