haproxy+hearbeat实现高可用负载均衡

环境:
  haproxy-master,heartbeat-master: 192.168.0.36
  haproxy-backup,heartbeat-backup: 192.168.0.46
  VIP:192.168.0.111
  web server1: 192.168.0.35
  web server2: 192.168.0.97
 
第一步:部署测试页面,这里不多讲了,然后部署haproxy反向代理功能:
  软件安装参考上一节:配置文件如下:/etc/haproxy/haproxy.cfg
***************************************************************
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log 127.0.0.1   local0
        maxconn 4096
        chroot /usr/share/haproxy
        uid 555
        gid 555
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        option  redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
        balance roundrobin

listen  web_proxy  192.168.0.111:80
        option  httpchk /index.html
        option  persist
        balance roundrobin
        server  web1 192.168.0.35:80 check inter 2000 fall 3
        server  web2 192.168.0.97:80 check inter 2000 fall 3
        server  web3 127.0.0.1:8080 check inter 2000 fall 3 backup

listen stats :8000
        mode http
        stats uri /

***************************************************************
  注意的是,这里我直接复制做好的,VIP是heartbeat靠可用服务启动之后才有的
,为了后面的排错,是分别绑定在haproxy-master,haproxy-backup主机上ip进行
测试,确保代理服务的正常!


第二步:部署heartbeat高可用服务
   首先所有的依赖都在rhel6.0盘中,只需配置好yum源:/etc/yum.repos.d/re.repo
***************************************************************
[server]
name=server
baseurl=ftp://192.168.0.97/pub/linux/
gpgcheck=0


[server1]
name=server1
baseurl=ftp://192.168.0.97/pub/linux/HighAvailability
gpgcheck=0


[server2]
name=server2
baseurl=ftp://192.168.0.97/pub/linux/LoadBalancer
gpgcheck=0
***************************************************************
    软件依赖:
       yum install PyXML cluster-glue libltdl.so.7 libplumb.so.2 resource-agents ipvsadm perl-MailTools perl-Net-SSLeay perl-Socket6 perl-libwww-perl -y
    安装软件: heartbeat-3.0.4-1.el6.x86_64.rpm
              heartbeat-libs-3.0.4-1.el6.x86_64.rpm
              ldirectord-3.9.2-1.2.x86_64.rpm
              heartbeat-devel-3.0.4-1.el6.x86_64.rpm
     yum localinstall * --nogpgcheck 不然的话,无法安装成功

    修改配置:
        cd /etc/ha.d/
        cp /usr/share/doc/heartbeat-3.0.4/authkeys .
        cp /usr/share/doc/heartbeat-3.0.4/haresources .
        cp /usr/share/doc/heartbeat-3.0.4/ha.cf .
        vi authkeys
           auth 3
           #1 crc
           #2 sha1 HI!
           3 md5 Hello!
        vi haresources
           haproxy-master IPaddr::192.168.0.111/24/eth0  haproxy
        vi ha.cf
           debugfile /var/log/ha-debug
           logfile /var/log/ha-log
           logfacility     local0
           keepalive 2
           deadtime 30
           warntime 10
           initdead 120
           udpport 694
           bcast   eth0            # Linux
           auto_failback on
           watchdog /dev/watchdog
           node    haproxy-master
           node    haproxy-backup
           ping 192.168.0.97
           respawn hacluster /usr/lib64/heartbeat/ipfail
           apiauth ipfail gid=haclient uid=hacluster
        modprobe softdog
        vi /etc/rc.local
           modprobe softdog

第三步:启动服务并且测试其可用性
       在haproxy-master,haproxy-backup上:
          /etc/init.d/heartbeat start
       观察vip
*****************************************************************
[root@localhost ha.d]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether 52:54:00:e5:a0:9e brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.36/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.111/24 brd 192.168.0.255 scope global secondary eth0:0
    inet6 fe80::5054:ff:fee5:a09e/64 scope link
       valid_lft forever preferred_lft forever
*****************************************************************  
       访问:http://192.168.0.111 验证haproxy的反向代理功能

       关闭haproxy-master上的heartbeat服务,看haproxy是否正常访问,来验证heartbeat的高可用性

实施过程中应该注意的问题:
    1.在配置heartbeat服务时,ha.cf中node选项一定要是
        node  主机名
      主机名 = uname -n
      
    2.在测试阶段,注意的是:只需要启动heartbeat服务器即可,heartbeat会自动调用资源: VIP,haproxy
      建议heartbeat服务开机自启,以方便自动切回
         chkconfig heartbeat on

你可能感兴趣的:(Web,haproxy,heartbeat)