LVS+Keepalived

拓扑描述:

LVS1(master)IP:10.58.156.82

LVS2(backup)IP:10.58.156.111

VIP:10.58.156.100

RS1 IP:10.58.156.73

RS2 IP:10.58.156.117

LVS安装:

检查内核模块,看一下ip_vs是否被加载

# lsmod |grep ip_vs

如果没有显示,则说明没有加载,执行命令

# modprobe ip_vs

查询内核版本

# uname –r

下载:

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

安装

# tar –zxvf ipvsadm-1.26.tar.gz

# cd ipvsadm-1.26

#make && make install

如果这里报错,一般显示缺少头文件,

ip_vs.h:15:29: 错误:netlink/netlink.h:没有那个文件或目录

# ls -s /usr/src/kernels/2.6.32-431.el6.i686/ /usr/src/linux/

# yum install -y libnl* popt*

 

安装keepalived

下载:http://www.keepalived.org/software/keepalived-1.2.15.tar.gz

解压安装:

#./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

建立负载均衡服务器启动脚本

#vi /sbin/lvsdr.sh

#!/bin/bash

VIP=10.58.156.100

RIP1=10.58.156.73

RIP2=10.58.156.117

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

case “$1” in

start)

echo “start LVS of DirectorServer”

#Set the Virtual IP Address

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

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

#Clear IPVS Table

/sbin/ipvsadm -C

#Set Lvs

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

/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”

/sbin/ifconfig eth0:1 down

/sbin/ipvsadm -C

;;

*)

echo “Usage0{start|stop}”

exit 1

esac

分配权限

#chmod 755 /sbin/lvsdr.sh

执行测试

# /sbin/lvsdr.sh start

查看ifconfig是否有ifcfg-eth0:1   (有就对了)

查看route -n 路由表是否多了eth0:1路由(有就对了)

# /sbin/lvsdr.sh stop

查看ifconfig是否有ifcfg-eth0:1   (无就对了)

查看route -n 路由表是否多了eth0:1路由(无就对了)

# /sbin/lvsdr.sh  adsa

是否提示参数错误,只能使用{start|stop}。

配置后端服务器

# vi /sbin/realdr.sh

#!/bin/bash

# description: Config realserver lo and apply noarp

VIP=10.58.156.100

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

case “$1” in

start)

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

/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

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”

;;

*)

echo “Usage: $0 {start|stop}”

exit 1

esac

exit 0

配置权限

#  chmod 755 /sbin/realdr.sh

配置keepalived.conf配置文件 //master和backup

#vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state MASTER  //备份服务器设置为backup

interface eth0

virtual_router_id 51

priority 100  ////备份服务器设置小于100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.58.156.100  //VIP

}

}

virtual_server 10.58.156.100 80 {

delay_loop 6  //隔6秒查询

lb_algo wrr   //lvs算法

lb_kind DR    //(Direct Route)

persistence_timeout 60  //同一IP的连接60秒内被分配到同一台realserver

inhibit_on_failure  //当web挂掉的时候,前面请求的用户,可以继续打开网页,但是后面的请求不会调度到挂掉的web上面。

protocol TCP   //用TCP协议检查realserver状态

real_server 10.58.156.73 80 {

weight 3    //权重

TCP_CHECK {

connect_timeout 10   //10秒无响应超时

nb_get_retry 3

delay_before_retry 3

}

}

real_server 10.58.156.117 80 {

weight 1

TCP_CHECK {

connect_timeout 10

nb_get_retry 3

delay_before_retry 3

}

}

}

启动keepalived

#service keepalived start

设置成自启动

#vi /etc/rc.local //里面添加

/etc/init.d/keepalived restart

/sbin/lvsdr.sh start

发表评论

电子邮件地址不会被公开。 必填项已用*标注