来源:http://www.weiruoyu.cn/?p=671
一.网络环境
系统:centos5.6
Vip=192.168.138.158 real=192.168.138.134 real=192.168.138.154 master=192.168.138.155 slave =192.168.138.156 (此实验为虚拟机环境) 防火墙要开启udp694端口(或者关闭防火墙) |
从最简单的做起
二.分别先设置两个real(代码一样)
[root@localhost ~]# vi /etc/lvs.sh
#!/bin/bash
VIP="192.168.138.158"
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 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
[root@localhost ~]# chmod a+x /etc/lvs.sh
[root@localhost ~]# /etc/lvs.sh
[root@localhost ~]# echo “/etc/lvs.sh”>> /etc/rc.local
设置完后重启即可。
注意:重启后
关闭防火墙:[root@localhost ~]# service iptables stop
设置apache页面
vi /var/www/html/index.jsp
thin is 192.168.134 real-1
[root@localhost ~]# vi /etc/httpd/conf/httpd.conf
查找到下面,并添加下面红色
DirectoryIndex index.html index.html.varindex.jsp
开启apache:
[root@localhost ~]# service httpd start
三.master的安装
1.安装需要的软件:
[root@localhost ~]# yum -y install ipvsadm heartbeat heartbeat-ldirectord
[root@localhost ~]# yum -y install heartbeat (这个一定要再安装一遍,否则会少东西)
[root@localhost ha.d]# yum install arptables*
查看heartbeat服务,并开启他
[root@lvs-slave ~]# chkconfig --list heartbeat
heartbeat 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@lvs-slave ~]# chkconfig --add heartbeat
[root@lvs-slave ~]# chkconfig heartbeat on
并开启服务。
2.设置配置文件。
2.0 修改主机名称为lvs-master
[root@lvs-master ha.d]# vi /etc/sysconfig/network
HOSTNAME=lvs-master
最后设置完重启就会显示了。
2.1 ldirectord的设置
[root@lvs-master ha.d]# cat ldirectord.cf |grep -v ^#
checktimeout=3
checkinterval=1
fallback=127.0.0.1:80
autoreload=yes
logfile="/var/log/ldirectord.log"
emailalert="[email protected]"
emailalertfreq=3600
emailalertstatus=all
quiescent=no
virtual=192.168.138.158:80
real=192.168.138.134:80 gate
real=192.168.138.154:80 gate
scheduler=wrr
#persistent=600
#netmask=255.255.255.255
protocol=tcp
checktype=connect
checkport=80
与上面保持一致就就行。
2.2 修改配置文件ha.cf
[root@lvs-master ha.d]# cat ha.cf |grep -v ^#
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport 694
baud 19200
bcast eth0 # Linux
mcast eth0 225.0.0.1 694 1 0
ucast eth0 192.168.138.156
auto_failback on
node lvs-master
node lvs-slave
ping_group group1 192.168.138.134 192.168.138.154
respawn root /usr/lib/heartbeat/ipfail
apiauth ipfail gid=rootuid=root
2.3 修改authkeys
[root@lvs-master ha.d]# cat authkeys |grep -v ^#
auth 2
2 sha1 10jqka
2.4 修改haresources
[root@lvs-master ha.d]# cat haresources |grep -v ^#
lvs-master ipvs ldirectord
2.5 脚本文件ipvs
[root@lvs-master ha.d]# vi /etc/ha.d/resource.d/ipvs(ipvs一定要放在/etc/ha.d/resource.d/或者/etc/init.d里才会被启动)
#!/bin/sh
VIP=192.168.138.158
RIP1=192.168.138.134
RIP2=192.168.138.154
. /etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS "
# # set the Virtual IP Address
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:0
/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
;;
stop)
echo "close LVS Director"
/sbin/ipvsadm -C
/sbin/ifconfig eth0:0 down
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
四.slave主机上的安装
4.1 把master上面的配置文件拷贝到slave(156)主机上
[root@lvs-master ~]# cd /etc/ha.d
[root@lvs-master ha.d]# scp authkeys ha.cf haresources ldirectord.cf [email protected]:/etc/ha.d
同时进行相应修改:
更改ha.cf配置文件
ucast eth0 192.168.138.156 #eth0的IP,此处从为从的IP
master和slave启动:service heartbeat start
参考:
http://88fly.blog.163.com/blog/static/122680390201101042825766/
博客专题:https://blog.51cto.com/zt/11
http://linux008.blog.51cto.com/2837805/538369