Chinaunix首页 | 论坛 | 博客
  • 博客访问: 910059
  • 博文数量: 91
  • 博客积分: 803
  • 博客等级: 准尉
  • 技术积分: 1051
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-24 13:42
文章分类

全部博文(91)

文章存档

2021年(1)

2020年(4)

2019年(4)

2018年(9)

2017年(11)

2016年(11)

2015年(6)

2014年(3)

2013年(28)

2012年(14)

分类: LINUX

2013-08-29 16:55:23

原文地址:centos5.3下lvs搭建笔记 作者:purcotton

测试环境

 

LVS 192.168.38.240    真实IP

LVS 192.168.38.241  真实IP backup

VIP 192.168.38.242    虚拟IP(访问地址)

RIP1  :192.168.38.201  负载WEB

RIP2  :192.168.38.9       负载WEB

 

一、在两台LVS上安装软件:keepalived-1.1.15.tar.gzipvsadm-1.24.tar.gz

并执行以下操作

mkdir /usr/local/src/lvs

mv keepalived-1.1.15.tar.gz /usr/local/src/lvs/

mv/usr/local/src/lvs/

cd /usr/local/src/lvs/

lsmod | grep ip_vs

#查看是否有ip_lvs模块

uname –r   

#查看版本号

ln -s /usr/src/kernels/2.6.18-53.el5-i686 /usr/src/linux

#否则提示一个错误

cd  /usr/local/src/lvs/

tar –zxvf  ipvsadm-1.24.tar.gz

cd  ipvsadm-1.24

make

make install

find -name ipvsadm

find / -name ipvsadm

cd ..

tar -zxvf keepalived-1.1.15.tar.gz

cd keepalived-1.1.15

./configure

#进行编译

make

make install

cp /usr/local/etc/rc.d/init.d/ /etc/rc.d/init.d/

cp /usr/local/etc/rc.d/init.d/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

service keepalived stop

chkconfig --add keepalived

chkconfig keepalived on

 

 

创建ipvsadm的启动文件(两台LVS上都创建)

vi /usr/local/sbin/lvsdr.sh

VIP=192.168.38.242     

#虚拟IP

RIP1=192.168.38.201

#第一台web备机

RIP2=192.168.38.9

#第二台web备机

/etc/rc.d/init.d/functions

#引用函数库

case "$1" in

start)

        echo "stat LVS of DirectorServer"

        /sbin/ipvsadm --set 900 120 300

                   #保持回话

        /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.0 up

                   建立eth0:1网卡设定为虚拟IP地址

        /sbin/route add -host $VIP dev eth0:1

        /sbin/ipvsadm –C

#清除内核虚拟服务器表中的所有记录

        /sbin/ipvsadm -A -t $VIP:80 -s wrr

                   #增加一台新的提供TCP服务的虚拟服务器列表

        /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 –g

                   #增加一台提供TCP服务的真实虚拟主机

        /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 –g

                   #同上

        /sbin/ipvsadm

;;

stop)

        echo "Close LVS Directorserver"

        /sbin/ifconfig eth0:1 down

                   #关闭虚拟IP

        /sbin/ipvsadm –C

                   #清除虚拟服务器列表

;;

*)

        echo "usage:$0 {start|stop}"

 

exit 1

esac

chmod 755 /usr/local/sbin/lvsdr.sh

#更改权限

/usr/local/sbin/lvsdr.sh start

#启动服务

#反馈如上采用Route路由直连

Ifconfig

#显示网卡信息

 会看到一个eth0:1  ip是刚刚在脚本中出现的192.168.38.242

ipvsadm -L –c

#查看当前进行TCP连接的用户

二、在两台web上执行以下操作

建立脚本

vi /usr/local/sbin/lvsadm.sh

#!/bin/bash

 VIP=192.168.38.242

#设定虚拟IP地址

. /etc/rc.d/init.d/functions

#调用函数库

case "$1" in

start)

       ifconfig lo:0 $ VIP netmask 255.255.255.255 broadcast $ VIP

                   ##建立eth0:1网卡设定为虚拟IP地址

       /sbin/route add -host $ VIP dev lo:0

       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

       sysctl -p >/dev/null 2>&1

       echo "RealServer Start OK"

                   #还没看,应该是和路由表有问题

 

       ;;

stop)

       ifconfig lo:0 down

       route del $ VIP >/dev/null 2>&1

                   #清除路由表中的虚拟IP路由信息

       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 "RealServer Stoped"

       ;;

status)

        # Status of LVS-DR real server.

        islothere=`/sbin/ifconfig lo:0 | grep $ VIP`

        isrothere=`netstat -rn | grep "lo:0" | grep $ VIP`

        if [ ! "$islothere" -o ! "isrothere" ];then

            # Either the route or the lo:0 device

            # not found.

            echo "LVS-DR real server Stopped."

        else

            echo "LVS-DR Running."

        fi

;;

*)

        # Invalid entry.

        echo "$0: Usage: $0 {start|status|stop}"

        exit 1

;;

esac

exit 0

 

启动脚本 /usr/local/sbin/lvsadm.sh

Keepalived使用

一、使用Keepalived

 

/etc/rc.d/init.d/keepalived start

#启动

Vi /etc/Keepalived/Keepalived.conf

! Configuration File for keepalived

 

global_defs {

   notification_email {

        test@test.com

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_instance VI_1 {

state MASTER

#LVSMASTER 备为BACKUP

    interface eth0

    virtual_router_id 51

priority 100

#备机权重小于100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.38.242

    }

}

 

virtual_server 192.168.38.242 80 {

    delay_loop 6

    lb_algo wrr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 600

    protocol TCP

 

    real_server 192.168.38.9 80 {

        weight 3

        TCP_CHECK {

           

            nb_get_retry 3

            delay_before_retry 3

            }

        }

    real_server 192.168.38.6 80 {

        weight 3

        TCP_CHECK {

connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            }

        }

}

 

 

 

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