Keepalived + HAProxy 实现Apache负载均衡和高可用

  • 原创
  • Madman
  • /
  • /
  • 0
  • 1437 次阅读

Synopsis: HAProxy 实现 7 层(http)负载均衡,将客户端请求均衡分发到后端 Web 服务器上。为了防止 HAProxy 成为单点故障,需要扩展为两节点,并用 Keepalived 实现HA

环境说明:

节点 组件 备注
nkocs01 keepalived + haproxy + httpd Web服务IP:192.168.40.151,HAProxy绑定端口:8081,Keepalived VVIP:192.168.40.160
nkocs02 keepalived + haproxy + httpd Web服务IP:192.168.40.152,HAProxy绑定端口:8081,Keepalived VVIP:192.168.40.160

1. 提供 httpd 服务

本实验针对 Ceph S3 对象网关,已安装 httpd-2.4.6-40,并搭建好 radosgw(默认端口 80

2. 安装 HAProxy

两个节点都安装 HAProxy,并各自设置 后端服务器(backend) 为本节点和另一节点

2. 安装 rpm 包

# yum install -y haproxy

2.2 修改配置文件

(1)网关节点1

[root@nkocs01 ~]# vim /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 *:8081
    stats uri /haproxy
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  gateway1 127.0.0.1:80 check
    server  gateway2 192.168.40.152:80 check

(2)网关节点2

[root@nkocs02 ~]# vim /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
                                
                            
分类: 负载均衡
标签: HAProxy HA KeepAlived LB
未经允许不得转载: LIFE & SHARE - 王颜公子 » Keepalived + HAProxy 实现Apache负载均衡和高可用

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列