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

环境:
  haproxy-master,keepalived-master: 192.168.0.36
  haproxy-backup,keepalived-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是keepalivedg高可用服务启动之后才有的
,为了后面的排错,是分别绑定在haproxy-master,haproxy-backup主机上ip进行
测试,确保代理服务的正常!


第二步.安装并部署keepalived: master与backup操作相同
   yum install gcc libnl-devel openssl-devel popt-devel ipvsadm kernel-devel -y
   tar zxf keepalived-1.2.2.tar.gz
   cd keepalived-1.2.2
   ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-71.el6.x86_64/
   make&&make install
   ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
   ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
   ln -s /usr/local/keepalived/etc/keepalived/ /etc/
   ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
   vi /etc/keepalived/keepalived.conf
****************************************************************
! Configuration File for keepalived 
 
global_defs {
   notification_email {
         root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_connect_timeout 3
   smtp_server 127.0.0.1
   router_id hhaproxy
 }

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 99
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
 virtual_ipaddress {
        192.168.0.111
    }
}
****************************************************************
 注意的是:keepalived不能像heartbeat一样将haproxy当作资源一样管理
在这里keepalived的作用只是提供高可用:VIP,haproxy再借助vip来启动!

第三步:在keepalived master backup上部署脚本
  vi haproxy.sh
****************************************************************
#!/bin/bash
# haproxypid == 0    ---> dead
# haproxypid == 1    ---> live
ha ()
{      while :
       do
        vippid=`ip addr show | grep 192.168.0.111 | wc -l`
        if [ $vippid -eq 0 ]
          then
           break;
        fi
        haproxypid=`ps -C haproxy | grep haproxy | wc -l`
         if [ $haproxypid -eq 0 ]
           then
         /etc/init.d/haproxy restart
         sleep 3
        haproxypid=`ps -C haproxy | grep haproxy | wc -l`
          if [ $haproxypid -eq 0 ]
            then
           /etc/init.d/keepalived stop
          fi
         fi
     sleep 3
   done
}


while :
do
vippid=`ip addr show | grep 192.168.0.111 | wc -l`
if [ $vippid -eq 1 ]
then
  ha
fi
done
**************************************************************
  sh haproxy.sh &

第四步:启动服务并测试
   /etc/init.d/keepalived start(master,backup)
  1.master 关闭haproxy 要连续几次(读脚本)
   /etc/init.d/haproxy stop
   继续访问http://192.168.0.111
  2.master上关闭keepalived服务再测试
   这样就可以做好基于服务,基于主机的高可用了!

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