ǰλ: > CentOS > Ⱥ >

CentOS5.5HAProxy+Keepalived߿øؾ

ʱ:2014-07-31 01:08Դ:blog.51cto.com :ɶ@״ ٱ :
һϵͳ
ϵͳ汾CentOS5.5 x86_64
master_ip:172.20.27.40
backup_ip:172.20.27.50 
vip:172.20.27.200
web_1: 172.20.27.90
web_2:172.20.27.100
haproxyװ
1.172.20.27.40װϰװ
1.1װ
 tar zxvf haproxy-1.3.20.tar.gz
 cd haproxy-1.3.20
 make TARGET=linux26 PREFIX=/usr/local/haproxy                             
 make install PREFIX=/usr/local/haproxy
1.2.
 cd /usr/local/haproxy 
 mkdir etc
 cd etc
 vi haproxy.cfg


global
  log 127.0.0.1   local0
   maxconn 65535
   chroot /usr/local/haproxy
    uid 99
    gid 99
    daemon
    nbproc 12
   pidfile /usr/local/haproxy/haproxy.pid
defaults
   log     127.0.0.1       local3
   mode   http
   option httplog
   option httpclose
   option dontlognull
   option forwardfor
   option redispatch
   retries 2
   maxconn 65535
   balance   source
   stats   uri     /web-status
   stats   auth admin:admin@admin
   stats   refresh 60s
   contimeout      5000
   clitimeout      50000
   srvtimeout      50000
listen  172.20.27.200
  bind *:80
  mode http 
  option httplog 
  log global
  option httpchk HEAD /t.htm HTTP/1.0
  server web1  172.20.27.90:80 weight 5  check inter 2000 rise 2 fall 3
  server web2  172.20.27.100:80 weight 5  check inter 2000 rise 2 fall 3
Ȼhaproxyִ
 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg 
2.172.20.27.50װϰװ
2.1.װ
 tar zxvf haproxy-1.3.20.tar.gz
 cd haproxy-1.3.20
 make TARGET=linux26 PREFIX=/usr/local/haproxy                             
 make install PREFIX=/usr/local/haproxy
2.2.
 cd /usr/local/haproxy 
 mkdir etc
 cd etc
 vi haproxy.cfg


global
  log 127.0.0.1   local0
   maxconn 65535
   chroot /usr/local/haproxy
    uid 99
    gid 99
    daemon
    nbproc 12
   pidfile /usr/local/haproxy/haproxy.pid
defaults
   log     127.0.0.1       local3
   mode   http
   option httplog
   option httpclose
   option dontlognull
   option forwardfor
   option redispatch
   retries 2
   maxconn 65535
   balance   source
   stats   uri     /web-status
   stats   auth admin:admin@admin
   stats   refresh 60s
   contimeout      5000
   clitimeout      50000
   srvtimeout      50000
listen  172.20.27.200
  bind *:80
  mode http 
  option httplog 
  log global
  option httpchk HEAD /t.htm HTTP/1.0
  server web1  172.20.27.90:80 weight 5  check inter 2000 rise 2 fall 3
  server web2  172.20.27.100:80 weight 5  check inter 2000 rise 2 fall 3
Ȼhaproxyִ
 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg 
keepalivedװ
1.172.20.27.40װϰװ
1.1װ
tar zxvf keepalived-1.1.17.tar.gz
cd keepalived-1.1.17
./configure 
make
make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig
mkdir /etc/keepalived
cp /usr/local/sbin/keepalived /usr/sbin/
cd /etc/keepalived
1.2.
 vi keepalived.conf
! Configuration File for keepalived
global_defs {
 notification_email {
    331095659@qq.com
}
  notification_email_from 331095659@qq.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
  router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
   script "/usr/local/haproxy/shell/check_haproxy.sh"
   interval 2
   weight 2
}
  # VIP1
  vrrp_instance VI_1 {
   state BACKUP
    interface eth0
   lvs_sync_daemon_inteface eth0
    virtual_router_id 151
    priority 100
    advert_int 5
    nopreempt
    authentication {
       auth_typePASS
       auth_pass 2222
   }
    virtual_ipaddress {
        172.20.27.200
    }
    track_script {
   chk_haproxy
   }
}
mkdir -p /usr/local/haproxy/shell
cd /usr/local/haproxy/shell
#haproxyű
vi check_haproxy.sh
#!/bin/bash
#auto check haprox process
#2014-6-30
killall -0 haproxy
 if  [[ $? -ne 0 ]];then
 /etc/init.d/keepalived stop
 fi
chmod u+x check_haproxy.sh
service keepalived restart
2.172.20.27.50װϰװ
2.1װ
tar zxvf keepalived-1.1.17.tar.gz
cd keepalived-1.1.17
./configure
make
make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig
mkdir /etc/keepalived
cp /usr/local/sbin/keepalived /usr/sbin/
cd /etc/keepalived
2.2
 vi keepalived.conf
! Configuration File for keepalived
global_defs {
 notification_email {
     331095659@qq.com
}
   notification_email_from  331095659@qq.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
   script "/usr/local/haproxy/shell/check_haproxy.sh"
   interval 2
   weight 2
}
  # VIP1
  vrrp_instance VI_1 {
   state BACKUP
    interface eth0
   lvs_sync_daemon_inteface eth0
   virtual_router_id 151
    priority 90
    advert_int 5
    nopreempt
    authentication {
       auth_typePASS
       auth_pass 2222
   }
    virtual_ipaddress {
        172.20.27.200
    }
    track_script {
   chk_haproxy
   }
}
mkdir -p /usr/local/haproxy/shell
cd /usr/local/haproxy/shell
#haproxyű
vi check_haproxy.sh
#!/bin/bash
#auto check haprox process
#2014-6-30
killall -0 haproxy
 if  [[ $? -ne 0 ]];then
 /etc/init.d/keepalived stop
 fi
chmod u+x check_haproxy.sh
service keepalived restart

ġ
 vip  172.20.27.200󶨵в
 1.һֱping 172.20.27.200IPȻmaster_ip̨ģ崻״̬ʱpingӳ3Զлbackup_ip˵Գɹ
------ָ----------------------------
Ϊ˺ҹرҳ棬ҵQQȺサɣ348944156,Ҳӭעվ΢Źںţcentoscn