来源: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

 
  
  1. #!/bin/bash

  2. VIP="192.168.138.158"

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

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

  5. echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore

  6. echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce

  7. echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore

  8. echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce

  9. 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

 
  
  1. 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 ^#

 
  
  1. checktimeout=3

  2. checkinterval=1

  3. fallback=127.0.0.1:80

  4. autoreload=yes

  5. logfile="/var/log/ldirectord.log"

  6. emailalert="[email protected]"

  7. emailalertfreq=3600

  8. emailalertstatus=all

  9. quiescent=no

  10. virtual=192.168.138.158:80

  11. real=192.168.138.134:80 gate

  12. real=192.168.138.154:80 gate

  13. scheduler=wrr

  14. #persistent=600

  15. #netmask=255.255.255.255

  16. protocol=tcp

  17. checktype=connect

  18. checkport=80

与上面保持一致就就行。
2.2 修改配置文件ha.cf

[root@lvs-master ha.d]# cat ha.cf |grep -v ^#

 
  
  1. debugfile /var/log/ha-debug

  2. logfile /var/log/ha-log

  3. logfacility local0

  4. keepalive 2

  5. deadtime 30

  6. warntime 10

  7. initdead 120

  8. udpport 694

  9. baud 19200

  10. bcast eth0 # Linux

  11. mcast eth0 225.0.0.1 694 1 0

  12. ucast eth0 192.168.138.156

  13. auto_failback on

  14. node lvs-master

  15. node lvs-slave

  16. ping_group group1 192.168.138.134 192.168.138.154

  17. respawn root /usr/lib/heartbeat/ipfail

  18. apiauth ipfail gid=rootuid=root

2.3 修改authkeys

[root@lvs-master ha.d]# cat authkeys |grep -v ^#

 
  
  1. auth 2

  2. 2 sha1 10jqka

2.4 修改haresources

[root@lvs-master ha.d]# cat haresources |grep -v ^#

 
  
  1. 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里才会被启动)

 
  
  1. #!/bin/sh

  2. VIP=192.168.138.158

  3. RIP1=192.168.138.134

  4. RIP2=192.168.138.154

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

  6. case "$1" in

  7. start)

  8. echo " start LVS "

  9. # # set the Virtual IP Address

  10. /sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up

  11. /sbin/route add -host $VIP dev eth0:0

  12. /sbin/ipvsadm -C

  13. /sbin/ipvsadm -A -t $VIP:80 -s rr

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

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

  16. ;;

  17. stop)

  18. echo "close LVS Director"

  19. /sbin/ipvsadm -C

  20. /sbin/ifconfig eth0:0 down

  21. ;;

  22. *)

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

  24. exit 1

  25. 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 #eth0IP,此处从为从的IP


masterslave启动:service heartbeat start

参考:

http://88fly.blog.163.com/blog/static/122680390201101042825766/

博客专题:https://blog.51cto.com/zt/11

http://linux008.blog.51cto.com/2837805/538369