Centos系统下载大全 | Redhat系统下载大全 | Windows2012系统下载大全 | Windows2008系统下载大全| CMS教程 | 网站地图 51运维网-专注Linux/Unix系统安全运维!
当前位置:51运维网 > 系统 > Centos服务器 > 正文

CentOS5.5下HAProxy+Keepalived高可用负载均衡配置

时间:2015-09-16 23:59 来源:网络整理 作者:51ou.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.200这个IP,然后把master_ip这台服务器模拟宕机状态。这时候ping会延迟3秒后,自动切换到backup_ip。(说明测试成功)

感谢您对【51运维网 http://www.51ou.com/】的支持,我们为您免费提供《CentOS5.5下HAProxy+Keepalived高可用负载均衡配置》技术文章,《CentOS5.5下HAProxy+Keepalived高可用负载均衡配置》详细使用和说明,有时《CentOS5.5下HAProxy+Keepalived高可用负载均衡配置》可能不完善、敬请谅解!如果《CentOS5.5下HAProxy+Keepalived高可用负载均衡配置》有错误请给我们留言,我们将尽快修复文章错误,如果您觉得本站不错,请分享给周围的朋友!谢谢!

顶一下
(0)
0%
踩一下
(0)
0%
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
验证码:点击我更换图片