Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370212
  • 博文数量: 180
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 32
  • 用 户 组: 普通用户
  • 注册时间: 2014-07-01 09:50
个人简介

那啥

文章分类

全部博文(180)

分类: 架构设计与优化

2014-07-01 11:27:01

LVS简介可以参考:http://blog.csdn.net/harbor1981/article/details/1358925
架构图:


所需软件:


ipvsadm-1.24-10.x86_64.rpm
heartbeat-2.1.3-3.el5.centos.x86_64.rpm
heartbeat-pils-2.1.3-3.el5.centos.x86_64.rpm
heartbeat-stonith-2.1.3-3.el5.centos.x86_64.rpm
PyXML-0.8.4-4.x86_64.rpm

系统环境:CentOS 5.9-64bit
192.168.137.14 虚拟ip
192.168.137.134 lvsmaster lvs主
192.168.137.135 lvsbackup lvs从
192.168.137.130 realserver1 web服务器
192.168.137.131 realserver2 web服务器

1、lvs安装

yum -y install ipvsadmyum -y install libnetyum -y install e2fsprogsyum -y install heartbeat
yum -y install heartbeat-devel
yum -y install heartbeat-ldirectord
echo "192.168.137.135 lvsmaster">>/etc/hosts
echo "192.168.137.135 lvsbackup">>/etc/hosts

2、配置步骤主

lvs配置
(1)lvsdr-basic脚本配置
[root@lvsmaster ~]#cat /etc/init.d/lvsdr-basic#!/bin/sh
VIP=192.168.137.14
RIP1=192.168.137.130
RIP2=192.168.137.131
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "start [OK]"
/sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:1
echo "1" > /proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/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
/sbin/ipvsadm
;;
stop)
echo "stop [OK]"
/sbin/ipvsadm -C
;;
*)
echo "Usage:$0 {start|stop}"
exit 1
esac
[root@lvsmaster ~]#chmod u+x /etc/init.d/lvsdr-basic
(2)ha.cf配置
[root@lvsmaster ~]#cat /etc/ha.d/ha.cf
debugfile /var/log/ha-debu
logfile /var/log/ha-log
logfacility local0
keepalive 2
warntime 10
deadtime 30
initdead 120
hopfudge 1
udpport 694
bcast eth0
ucast eth0 192.168.137.135
IPauto_failback on
node lvsmaster
node lvsbackup
ping 192.168.137.1
respawn root /usr/lib/heartbeat/ipfail
apiauth ipfail gid=root uid=root
(3)authkeys认证文件的配置
[root@lvsmaster ~]# cat /etc/ha.d/authkeys
auth 3
3 md5 9bf2f23aae3a63c16ea681295ffd7666[root@lvsmaster ~]#chmod u+x 600 authkeys
(4)ldirectord.cf文件配置
[root@lvsmaster ~]# cat /etc/ha.d/ldirectord.cf
checktimeout=3
checkinterval=1
autoreload=no
logfile="/var/log/ldirectord.log"
quiescent=no
virtual=192.168.137.14:80
real=192.168.137.130:80 gate
real=192.168.137.131:80 gate
service=http
request="test.html"
receive="Test"
scheduler=sh
protocol=tcp
(5)haresources文件配置[root@lvsmaster ~]# cat /etc/ha.d/haresources
lvsmaster IPaddr::192.168.137.14/24/eth0 ldirectord  lvsdr-basic
(6)关闭ldirectord服务开启heartbeat服务。
[root@lvsmaster ~]# chkconfig --del ldirectord[root@lvsmaster ~]# chkconfig --level 2345 ldirectord off[root@lvsmaster ~]# chkconfig --level 2345 heartbeat on
备份lvs配置(1)switchdr配置
[root@lvsbackup ~]#cat /etc/init.d/switchdr#!/bin/sh
# description: close lo0 and arp_ignore
VIP=192.168.137.14
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "************* start director server and close tunl ***********"
/sbin/ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
echo "start Real Server"
/sbin/ifconfig eth0:0 down
/sbin/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
;;
*)
echo "Usage: switchdr {start|stop}"
exit 1
esac
[root@lvsbackup ~]#chmod u+x /etc/init.d/switchdr
(2)ha.cf配置
[root@lvsbackup ~]#cat /etc/ha.d/ha.cf
debugfile /var/log/ha-debu
logfile /var/log/ha-log
logfacility local0
keepalive 2
warntime 10
deadtime 30
initdead 120
hopfudge 1
udpport 694
bcast eth0
ucast eth0 192.168.137.134
auto_failback on
node lvsmaster
node lvsbackup
ping 192.168.137.1
respawn root /usr/lib/heartbeat/ipfail
apiauth ipfail gid=root uid=root
(3)authkeys 配置
[root@lvsbackup ~]# cat /etc/ha.d/authkeys
auth 3
3 md5 9bf2f23aae3a63c16ea681295ffd7666[root@lvsbackup ~]#chmod 600 authkeys
(4) ldirectord.cf配置
[root@lvsbackup ~]# cat /etc/ha.d/ldirectord.cf
checktimeout=3
checkinterval=1
autoreload=no
logfile="/var/log/ldirectord.log"
quiescent=no
virtual=192.168.137.14:80
real=192.168.137.130:80 gate
real=192.168.137.131:80 gate
service=http
request="test.html"
receive="Test"
scheduler=sh
protocol=tcp
(5)配置haresources
[root@lvsbackup ~]# cat /etc/ha.d/haresources
lvsmaster IPaddr::192.168.137.14/24/eth0 ldirectord switchdr
(6)关闭ldirectord服务开启heartbeat服务。
[root@lvsbackup ~]# chkconfig --del ldirectord[root@lvsbackup ~]# chkconfig --level 2345 ldirectord off[root@lvsbackup ~]# chkconfig --level 2345 heartbeat on

3、客户端配置ip 192.168.137.131 192.168.137.130
#cat  /etc/init.d/realserver#!/bin/bash
#description : start realserver
VIP=192.168.137.14
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0echo "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#chmod u+x /etc/init.d/realserver执行
# /etc/init.d/realserver
查看VIP虚拟ip是否已经绑定:
# ifconfig  lo:0
lo:0      Link encap:Local Loopback
inet addr:192.168.137.14  Mask:255.255.255.255
UP LOOPBACK RUNNING  MTU:16436  Metric:1

4、启动服务

lvs主
/etc/init.d/heartbeat start
lvs备份
/etc/ini.d/switchdr stop
/etc/init.d/heartbeat start
测试(1)停掉主备份会接替主,当主恢复时备份自动转为备份状态。
(2)停止一台apache真实服务器。ipvsadm将会自动把这一台机器剔除。
得出结论:不断刷新192.168.137.14,会轮流显示realserver1 web和realserver2 web页面

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