lvs 搭建笔记记录

1、LVS介绍

LVS集群有DR、TUN、NAT三种配置模式,可以对www服务、FTP服务、MAIL服务等做负载均衡,下面通过搭建www服务的负载均衡实例,讲述基于DR模式的LVS集群配置。

 

 

1 lvs

[root@gb_pre_lvs_180_54 lvs]# vimlvs-dr-client.sh

 

 

 

2、Real server 的配置

  在lvs的DR和TUn模式下,用户的访问请求到达真实服务器后,是直接返回给用户的,而不再经过前端的Director Server,

因此,就需要在每个Real server节点上增加虚拟的VIP地址,这样数据才能直接返回给用户,增加VIP地址的操作可以通过创建

脚本的方式来实现,创建文件/etc /init.d/lvsrs,脚本内容如下:

#!/bin/bash

VIP=192.168.180.55

case "$1" in

start)

# 通过ipvsadm命令行方式配置LVS

echo "start LVS real server"

# 安装IPVS后,就可以配置LVS集群了,首先在Director Server上绑定一个虚拟IP(也叫VIP),此IP用于对外提供服务,执行如下命令:

/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up

 

 

# 然后给设备eth0:0指定一条路由,执行如下指令:

/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

 

 

# 使前面的设置的参数生效

/sbin/sysctl -p

  

# 此操作是在回环设备上绑定了一个虚拟IP地址,并设定其子网掩码为255.255.255.255,与Director Server上的虚拟IP保持互通,然后禁止了本机的ARP请求。

 

 

;;

stop)

echo "stop LVS real server"

/sbin/ifconfig lo:0 down

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

;;

*)exit 1

esac

 

 

 

lvs是通过在keepalived里面启动来着的。

 

 

3、源码安装 ipvsadm

安装配置ipvsadm,编写启动脚本

# version版本号码

wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz

tar -xvf ipvsadm-1.26.tar.gz

cd  ipvsadm-1.26

make

make install

 

 

启动脚本:

[root@/root/nagios/check_lvs lvs]# pwd

/usr/local/lvs

[root@/root/nagios/check_lvs lvs]# cat lvs-dr-server.sh

#!/bin/bash

 VIP=10.254.3.17

 RIP1=10.254.3.27

 RIP2=10.254.3.28

 PORT=80

case "$1" in

start)

echo "start LVS of DirectorServer"

#Set the Virtual IP Address

/sbin/ifconfig em1:1 $VIP broadcast $VIP netmask 255.255.255.255 up

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

#Clear IPVS Table

/sbin/ipvsadm -C

#Set Lvs

/sbin/ipvsadm -A -t $VIP:$PORT -s wlc

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

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

#Run Lvs

#/sbin/service ipvsadm start

/sbin/ipvsadm

;;

 

 

stop)

echo "stop LVS Directorserver"

/sbin/ifconfig em1:1 down

/sbin/ipvsadm -C

#/sbin/service ipvsadm stop

;;

 

 

*)

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

exit 1

esac

[root@/root/nagios/check_lvs lvs]#

 

 

4、配置 keepalived 使其生效

安装keepalived:

[root@gb_pre_lvs_180_54 ~]#

 

 

 

 

 

 

 

 

wget http://www.keepalived.org/software/keepalived-1.2.8.tar.gz

tar -xvf keepalived-1.2.8.tar.gz

cd keepalived-1.2.8

./configure --prefix=/usr/local/keepalived

make

make install

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

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

mkdir /etc/keepalived

 

 

配置keepadlived参数:

cat /etc/keepalived/keepalived.conf

##############################################################

# global define #

##############################################################

 

 

global_defs {

router_id LVS_PRE_NET_MASTER # 设置lvs的id,在一个网络内应该是唯一的

}

 

 

vrrp_sync_group VGM {

group {

VI_PRE_NET # 设置vrrp组

}

}

 

 

################################################################

# vvrp_instance define #

################################################################

 

 

vrrp_instance VI_PRE_NET {

         state MASTER #设置lvs的状态,报错MASTER和BACKUP两种,必须大写

         interface eth0 #设置对外服务的接口

         lvs_sync_daemon_inteface eth0 #设置lvs监听的接口

         virtual_router_id 16 #设置虚拟路由表示

         priority 180  #设置优先级,数值越大,优先级越高

         advert_int 5 #设置同步时间间隔

         authentication { #设置验证类型和密码

                   auth_type PASS

                   auth_pass LVSPRENET

         }

         virtual_ipaddress { #设置lvs vip

                   192.168.180.55

         }

}

##############################################################

# virtual server setting #

##############################################################

 

 

virtual_server 192.168.180.55 80 {

         delay_loop 6 #健康检查时间间隔

         lb_algo wlc #负载均衡调度算法

         lb_kind DR #负载均衡转发规则

 

 

         protocol TCP #协议

         real_server 192.168.180.50 80 {

                   weight 100 #设置权重

                   TCP_CHECK {

                            connect_timeout 3

                            nb_get_retry 3

                            delay_before_retry 3

                            connect_port 80

                   }

         }

         real_server 192.168.180.53 80 {

                   weight 100

                   TCP_CHECK {

                            connect_timeout 3

                            nb_get_retry 3

                            delay_before_retry 3

                            connect_port 80

                   }

         }

}

 

 

#虚拟IP地址

virtual_server 192.168.180.55 443 {

         delay_loop 6

         lb_algo wlc

         lb_kind DR

 

 

         #2台真实IP地址

         protocol TCP

         real_server 192.168.180.50 443 {

                   weight 100

                   TCP_CHECK {

                            connect_timeout 3

                            nb_get_retry 3

                            delay_before_retry 3

                            connect_port 443

                   }

         }

         real_server 192.168.180.53 443 {

                   weight 100

                   TCP_CHECK {

                            connect_timeout 3

                            nb_get_retry 3

                            delay_before_retry 3

                            connect_port 443

                   }

         }

}

 

 

 

 

BTY:三台客户端的脚本都一样!

 

 

 

5、测试lVS+Keepalived

1)       首先测试各个readserver,确定各个readserver都能正常访问,测试realserver.sh脚本,看启动后能不能绑定lvs vip,停止后能不能去除绑定的vip

2)       测试lvs能否进行负载均衡转发,利用ipvsadm命令查看,如果想只当当前的请求转发到哪个服务器去了,可以用ipvsadm加选项来查看,其完整形式为:ipvsadm –lcn | grep 192.168.1.115

3)       停掉主lvs看lvs backup是否接管!

 

 

[root@gb_pre_lvs_180_54 software]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

 -> RemoteAddress:Port          Forward Weight ActiveConn InActConn

TCP 192.168.180.55:http wlc

 -> 192.168.180.50:http         Route   100    1         0        

 -> 192.168.180.53:http         Route   100    0         0        

TCP 192.168.180.55:https wlc

 -> 192.168.180.50:https        Route   100    0         0        

 -> 192.168.180.53:https        Route   100    0         0        

[root@gb_pre_lvs_180_54 software]#

你可能感兴趣的:(linux)