keepalived+haproxy实现web服务的高可用和负载均衡

简介:

keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换.Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器.
 
Haproxy 反向代理服务器,支持双机热备支持虚拟主机,但其配置简单,拥有非常不错的服务器健康检查功能,当其代理的后端服务器出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入.新的1.3引入了frontend,backend,frontend根据任意HTTP请求头内容做规则匹配,然后把请求定向到相关的backend.
 
实验环境以及服务器信息:
 
OS:   RedHat  AS 5.1
 
软件列表:
      keepalived-1.2.2.tar.gz
      haproxy-1.4.13.tar.gz
 
服务器信息:
Master server  10.10.0.99   (调度主服务器)
Slave server    10.10.0.98   (从调度服务器)
       VIP: 10.10.0.97                   (调度服务器的虚拟IP)
 
Real server:
10.10.0.96
10.10.0.95

安装keepalived

#  tar zxvf  keepalived-1.2.2.tar.gz
#  cd keepalived-1.2.2
#  ./configure –-prefix=/usr/local/keepalived
#  make&&make install
#  cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
#  cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#  mkdir /etc/keepalived
#  cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
#  cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#  chkconfig –add keepalived
#  chkconfig  keepalived on
#  vi /etc/keepalived/keepalived.conf
 
global_defs {
notification_email {
xxxxxx@139.com
}
notification_email_from xxxxxx@139.com
smtp_server mail.139.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int
authentication {
auth_type PASS
auth_pass 1111
    }
virtual_ipaddress {
10.10.0.97
}
}
virtual_server 10.10.0.97 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
    real_server 10.10.0.95 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 10.10.0.96 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
关于keepalived配置文件的选项解释可以去网上搜下 由于选项参数居多这里就不说明了
#  service keepalived start
 
 
 

安装haproxy

#   tar zxvf  haproxy-1.4.13.tar.gz
#   mv  haproxy-1.4.13 haproxy
#   cd haproxy
#   make TARGET=linux26  
#   vi /usr/local/haproxy/conf/haproxy.conf
#   mkdir –p /var/chroot/haproxy
 
global
        chroot /var/chroot/haproxy
        daemon
        gid     0
        log 127.0.0.1 local3
        nbproc  2
        pidfile /var/run/haproxy-private.pid

        ulimit   -n        65535
        user    root
        maxconn         32000
        spread-checks           4
        tune.maxaccept          8
        tune.maxpollevents      100
defaults sxit
        log     global
        mode    http
        option  httplog
        option  dontlognull
        log 127.0.0.1 local3
        retries 3
        option redispatch
        maxconn     32000
        contimeout      5000
        clitimeout        50000
        srvtimeout       50000
listen  sxit 0.0.0.0:80
        appsession JSESSIONID len 52 timeout 3h
        cookie SRV insert indirect nocache
        mode http
        stats enable
        stats hide-version
        stats uri  /haproxy-stats
        stats realm Haproxy\ statistics
        stats auth sxit:sxit
        stats refresh 3s
        monitor-uri /haproxy_test
        balance roundrobin
        option httpclose
        option forwardfor
        option httpchk HEAD /index.html HTTP/1.0
        server s1 10.10.0.95:80 check inter 2000  
weight 3

        server s3 10.10.0.96:80 check inter 2000  
weight 3
        
# /usr/local/haproxy/sbin/haproxy –f  /usr/local/haproxy/config/haproxy.config
 
如果启动没有报什么错误的话,就在浏览器上输入如下地址
http://10.10.0.97/haproxy-stats  (查看服务器状态信息的页面,登录的时候输入上面设置的账号和密码sxit),页面状态如下:
 keepalived+haproxy实现web服务的高可用和负载均衡

测试负载均衡效果以及高可用性

负载均衡测试:

   启动真实服务器的web服务,2个真实服务器上创建2个首页文件,内容分别为test1test2,如果在浏览器*问web服务每次刷新既显示test1又显示tetst2就说明负载均衡已经生效了.

高可用性测试:

拔掉主调度服务器的网线或者关闭主调度服务器,看下VIP是否顺利的切换到从调度器,假如切换正常,那就说明keepalived已经成功生效了.

本文转自wiliiwin 51CTO博客,原文链接:http://blog.51cto.com/wiliiwin/648188

上一篇:美国“存储Twitter”,中国也要给博文“建档”


下一篇:JS关于全局变量跟局部变量的总结