码迷,mamicode.com
首页 > 其他好文 > 详细

基于keepalived对HAproxy做高可用集群

时间:2014-05-05 12:13:18      阅读:487      评论:0      收藏:0      [点我收藏+]

标签:haproxy 高可用集群 keepalived 负载均衡集群 集群

一、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将把服务器从服务器群中剔除。

二、安装配置

1.拓扑图

bubuko.com,布布扣

   说明:1.两个代理服务器通过VIP向外提供数据

         2.两个代理服务器都可以代理后端的服务器

         3.为测试方便,后端服务器至提供静态页面

2.ip规划

功用ip地址安装软件
VIP192.168.1.99
反向代理1192.168.1.201keepalived、haproxy
反向代理2192.168.1.204keepalived、haproxy
web服务器1192.168.1.202httpd
web服务器2192.168.1.203httpd

3.安装配置haproxy

   关于haproxy的详细配置,请参照http://wangfeng7399.blog.51cto.com/3518031/1405758      

[root@node1 ~]# cat /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the ‘-r‘ option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the ‘listen‘ and ‘backend‘ sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:80
    default_backend static    
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      node2 192.168.1.202:80 check maxconn 2000
    server      node3 192.168.1.203:80 check maxconn 2000
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------

   说明:两个节点的HAproxy的配置文件应该保持一样

4.测试haproxy的配置

bubuko.com,布布扣

bubuko.com,布布扣

5.安装配置keepalived

       ①、安装keepalived

           keepalived的安装可以通过yum源来安装,也可以通过编译源码来安装,本处通过yum源赖安装

       ②、配置keepalived主节点

[root@node1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {  //全局参数
   notification_email { //邮件
     sysadmin@firewall.loc //收件人地址
   }
   notification_email_from Alexandre.Cassen@firewall.loc //发件人地址
   smtp_server 127.0.0.1  //邮件服务器的地址
   smtp_connect_timeout 30  //间隔时间
   router_id LVS_DEVEL  //邮件服务器的组的id
}
vrrp_script chk_haproxy {
    script "killall -0 haproxy"  //检查haproxy是否在线
    interval 1    //检查间隔时间
    weight -5     //如果检查失败,则权重-5
}
vrrp_instance VI_1 {   //定义第一个集群
    state MASTER      //初始状态为主节点,从节点应该为BACKUP
    interface eth0    //配置ip的端口
    virtual_router_id 51  //本组集群的id号,主从节点必须一样
    priority 100   //主节点的优先级,备用节点的优先级必须低于主节点
    advert_int 1   //心跳检查间隔时间
    authentication {
        auth_type PASS   //通信为明文密码通信
        auth_pass 1111   //通信的密码,主从节点必须一样
    }
    virtual_ipaddress {
        192.168.1.99    //定义一个VIP
    }
track_script {   //调用上面的命令
    chk_haproxy
}
}

   ③、配置keepalived从节点

[root@node4 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
    script "killall -0 haproxy"
    interval 1
    weight -5
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.99
    }
track_script {
    chk_haproxy
}
}

6.测试

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:56:78:cd brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.204/24 brd 192.168.1.255 scope global eth0
    inet 192.168.1.99/32 scope global eth0
    inet6 fe80::20c:29ff:fe56:78cd/64 scope link tentative dadfailed
       valid_lft forever preferred_lft forever

      可以看到ip地址已经配置上去了

bubuko.com,布布扣

   我们可以看到网页访问正常

   下面,我们模拟服务器损坏,将node1上的haproxy关掉,看看ip地址是否会转移到node4上

[root@node4 ~]# ip add
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:56:78:cd brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.204/24 brd 192.168.1.255 scope global eth0
    inet 192.168.1.99/32 scope global eth0
    inet6 fe80::20c:29ff:fe56:78cd/64 scope link tentative dadfailed
       valid_lft forever preferred_lft forever

   查看网页是否能够访问正常   bubuko.com,布布扣

    OK!!可以看到我们的页面访问正常,这就可以实现当前段的一个反向代理服务器宕机或者后端的一个web服务宕机,服务都可以正常对外提供

     7.扩展

         我们还可以自定义通知机制

#!/bin/bash
# Author: wangfeng7399<wangfeng17399@163.com>
# description: An example of notify script
#
vip=192.168.1.99
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
        exit 0
    ;;
    backup)
        notify backup
        exit 0
    ;;
    fault)
        notify fault
        exit 0
    ;;
    *)
        echo ‘Usage: `basename $0` {master|backup|fault}‘
        exit 1
    ;;
esac

       在keepalived的配置文件中通过notify来调用,如下所示

notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"

       大功告成,由于本人水平有限,可能有逻辑上的错误,请各位大神匹配指正

本文出自 “IT民工--小枫” 博客,请务必保留此出处http://wangfeng7399.blog.51cto.com/3518031/1405785

基于keepalived对HAproxy做高可用集群,布布扣,bubuko.com

基于keepalived对HAproxy做高可用集群

标签:haproxy 高可用集群 keepalived 负载均衡集群 集群

原文地址:http://wangfeng7399.blog.51cto.com/3518031/1405785

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