码迷,mamicode.com
首页 > Web开发 > 详细

HAProxy+KeepAlived实现web服务高可用、动静分离等

时间:2014-05-03 14:37:46      阅读:610      评论:0      收藏:0      [点我收藏+]

标签:haproxy   keepalived   高可用   动静分离   负载均衡   

大致规划:

主机IP描述
VIP192.168.0.222对外提供高可用IP
haproxy+keepalived (node1)192.168.0.111

haproxy为后端两台WEB服务的做动静分离;keepalived为haproxy做高可用。

haproxy+keepalived (node2)192.168.0.112
WEB                (node3)192.168.0.113提供静态请求响应
Apache+PHP+MySQL   (node4)192.168.0.114提供动态请求响应

bubuko.com,布布扣

一、KeepAlived的简介以原理

Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。

Layer3,4&7工作在IP/TCP协议栈的IP层,TCP层,及应用层,原理分别如下:

  • Layer3:Keepalived使用Layer3的方式工作式时,Keepalived会定期向服务器群中的服务器发送一个ICMP的数据包(既我们平时用的Ping程序),如果发现某台服务的IP地址没有激活,Keepalived便报告这台服务器失效,并将它从服务器群中剔除,这种情况的典型例子是某台服务器被非法关机。Layer3的方式是以服务器的IP地址是否有效作为服务器工作正常与否的标准。在本文中将采用这种方式。

  • Layer4:如果您理解了Layer3的方式,Layer4就容易了。Layer4主要以TCP端口的状态来决定服务器工作正常与否。如web server的服务端口一般是80,如果Keepalived检测到80端口没有启动,则Keepalived将把这台服务器从服务器群中剔除。

  • Layer7:Layer7就是工作在具体的应用层了,比Layer3,Layer4要复杂一点,在网络上占用的带宽也要大一些。Keepalived将根据用户的设定检查服务器程序的运行是否正常,如果与用户的设定不相符,则Keepalived将把服务器从服务器群中剔除。

--引用百度百科

二、安装相关软件

WEB和LAMP搭建这里不做赘述;安装完成后可以测试是否正常访问。

bubuko.com,布布扣

bubuko.com,布布扣

测试两台机器正常访问。

为node1和node2各自安装keepalived和haproxy;为了简便;yum安装即可

[root@node1 ~]# rpm -q keepalived haproxy
keepalived-1.2.7-3.el6.x86_64
haproxy-1.4.24-2.el6.x86_64
                                                                                                                                                                                                                                      
-----------------------------------------------
                                                                                                                                                                                                                                    
[root@node2 ~]# rpm -q keepalived haproxy
keepalived-1.2.7-3.el6.x86_64
haproxy-1.4.24-2.el6.x86_64
[root@node2 ~]#

三、配置haproxy实现动静分离

这里在node1上演示;node2同样的配置:

[root@node1 ~]# vim /etc/haproxy/haproxy.cfg
#这里对应global段和default段不做任何修改;但是日志需要在global段开启;
#其余的全部注释;任何重新添加以下内容;
#具体含义上一篇以做解释;这里就不做详细介绍
frontend  web
    bind *:80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js .html .htm
    acl url_dynamic      path_end       -i .php
                                                                                                                                                                                         
    use_backend static          if url_static
    use_backend dynamic         if url_dynamic
    default_backend             static
backend static
     balance    roundrobin
     server  node3 192.168.0.113:80 check
backend dynamic
     balance    roundrobin
     server  node4 192.168.0.114:80 check
listen stats
     mode http
     bind *:1234
     stats enable
     stats refresh 3s
     stats hide-version
     stats uri  /admin?stats
     stats realm HAProxy\ Statistics
     stats auth admin:haproxy
     stats admin if TRUE
                                                                                                                                                                                          
-------------------------------------------------------------------
#复制一份到node2即可;各自启动haproxy  
[root@node1 ~]# scp /etc/haproxy/haproxy.cfg node2:/etc/haproxy/
haproxy.cfg                                                          100% 3896     3.8KB/s   00:00  
[root@node1 ~]#

查看stats信息:

bubuko.com,布布扣

如后端有多台机器;则可以实现负载均衡;这里未做实例。

四、配置keepalived实现haproxy的高可用

同样在node1上配置;但是node2需要更改两个地方:

[root@node1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
                                                                                                                                              
global_defs {                 #全局配置
   notification_email {
        root@localhost        #接受邮件方
   }
   notification_email_from soul@localhost  #发件人
   smtp_server 127.0.0.1                   #邮件服务器
   smtp_connect_timeout 30                 #超时时长
   router_id LVS_DEVEL                     #ID;随意即可
}
                                                                                                                                              
vrrp_script chk_down {                     #添加监控规则
    script "[[ -f /root/down ]] && exit 1 || exit 0"
#上面说明如果在/roo/下有down文件;就失败;转移到从上;否则正常
    interval 1             #多久监控一次
    weight 2               #权重
}
                                                                                                                                              
vrrp_script chk_haproxy {  #监控haproxy服务的
    script "pidof haproxy &> /dev/null && exit 0 || exit 1"
    interval 1
    weight 2
}
                                                                                                                                              
vrrp_instance VI_1 {        #添加一个实例
    state MASTER            #定义主从    #注意node2从的上为BACKUP
    interface eth0          #网络接口
    virtual_router_id 222   #虚拟路由ID;根据该ID生成虚拟MAC;保证其唯一性
    priority 100            #优先级别;BACKUP要低于MASTER
    advert_int 1            #心跳广播间隔
    authentication {        #认证
        auth_type PASS      #明文认证
        auth_pass 1111      #password
    }
    virtual_ipaddress {     #虚拟IP
        192.168.0.222
    }
    track_script {          #脚本追踪;生效上述定义脚本
        chk_down
        chk_haproxy
    }
    notify_master "/etc/keepalived/notify.sh master"   #邮件通知
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}
#更改以上信息;其余配置文件中的信息全部注释即可
---------------------------------------------------------
#复制一份到node2;根据上述描述进行更改

提供haproxy脚本:

[root@node1 ~]# vim /etc/keepalived/notify.sh
#!/bin/bash
# Author: MageEdu <linuxedu@foxmail.com>
# description: An example of notify script
#
vip=192.168.0.222
contact=‘root@localhost‘
notify() {         #定义发送邮件格式等信息
    mailsubject="`hostname` to be $1: $vip floating"
    mailbody="`date ‘+%F %H:%M:%S‘`: vrrp transition, `hostname` changed to be $1"
    echo $mailbody | mail -s "$mailsubject" $contact
}
                                                                                                                                  
case "$1" in
    master)             #根据参数进行执行对应命令
        notify master  
        /etc/rc.d/init.d/haproxy start
        exit 0
    ;;
    backup)
        notify backup
        /etc/rc.d/init.d/haproxy stop
        exit 0
    ;;
    fault)
        notify fault
        /etc/rc.d/init.d/haproxy stop
        exit 0
    ;;
    *)
        echo ‘Usage: `basename $0` {master|backup|fault}‘
        exit 1
    ;;
esac
                                                                                                                                  
#完成后复制一份到node2即可
#启动keepalived即可

五、测试haproxy的高可用

[root@node1 ~]# ip a
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 00:0c:29:5e:1e:4f brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.111/16 brd 192.168.255.255 scope global eth0
    inet 192.168.0.222/32 scope global eth0
    inet6 fe80::20c:29ff:fe5e:1e4f/64 scope link
       valid_lft forever preferred_lft forever
                                                                                                                              
#查看虚拟IP目前在node1上;

bubuko.com,布布扣

测试访问正常;下面down掉node1看下;

#由于配置文件定义了一个down的脚本;直接建立文件即可
[root@node1 ~]# touch down
[root@node1 ~]# ip a
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 00:0c:29:5e:1e:4f brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.111/16 brd 192.168.255.255 scope global eth0
    inet6 fe80::20c:29ff:fe5e:1e4f/64 scope link
       valid_lft forever preferred_lft forever
You have new mail in /var/spool/mail/root    #这里也提示有邮件信息
                                                                                                           
------------------------------------------------------------------
                                                                                                           
[root@node2 ~]# ip a
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 UP qlen 1000
    link/ether 00:0c:29:df:70:b6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.112/16 brd 192.168.255.255 scope global eth0
    inet 192.168.0.222/32 scope global eth0
                                                                                                           
#测试已转移到node2上

同时测试页面也是正常的。

测试haproxy脚本是否能够执行

[root@node1 ~]# service haproxy stop
Stopping haproxy:                                          [  OK  ]
[root@node1 ~]# ip a
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 00:0c:29:5e:1e:4f brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.111/16 brd 192.168.255.255 scope global eth0
    inet6 fe80::20c:29ff:fe5e:1e4f/64 scope link
                                                                                  
----------------------------------------------------------------------
                                                                                  
[root@node2 ~]# ip a
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 UP qlen 1000
    link/ether 00:0c:29:df:70:b6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.112/16 brd 192.168.255.255 scope global eth0
    inet 192.168.0.222/32 scope global eth0
                                                                                  
---------------------------------------------------------------------
                                                                                  
[root@node1 ~]# tail -5 /var/log/messages
May  2 18:25:10 node1 Keepalived_vrrp[5331]: VRRP_Script(chk_haproxy) failed
May  2 18:25:10 node1 Keepalived_vrrp[5331]: VRRP_Instance(VI_1) Received higher prio advert
May  2 18:25:10 node1 Keepalived_vrrp[5331]: VRRP_Instance(VI_1) Entering BACKUP STATE
May  2 18:25:10 node1 Keepalived_vrrp[5331]: VRRP_Instance(VI_1) removing protocol VIPs.
May  2 18:25:10 node1 Keepalived_healthcheckers[5330]: Netlink reflector reports IP 192.168.0.222 removed
                                                                                  
#可以查看系统日志也记录详细信息
#测试启动haproxy后;虚拟IP会自动转回。

到此;HAProxy+KeepAlived配置以全部完成;相关功能也都已测试成功。




如有错误;恳请纠正。

本文出自 “Soul” 博客,请务必保留此出处http://chenpipi.blog.51cto.com/8563610/1405554

HAProxy+KeepAlived实现web服务高可用、动静分离等,布布扣,bubuko.com

HAProxy+KeepAlived实现web服务高可用、动静分离等

标签:haproxy   keepalived   高可用   动静分离   负载均衡   

原文地址:http://chenpipi.blog.51cto.com/8563610/1405554

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!