负载均衡的高可用集群(5)-haproxy的介绍及负载均衡服务器部署

一、Haproxy介绍

HAProxy是一个特别适用于高可用性环境的TCP/HTTP开源的反向代理和负载均衡软件。实现了一种事件驱动,单一进程模型,支持非常大的并发连接,是因为事件驱动模型有更好的资源和时间管理的用户端(user-space)实现这些业务

  • 7层负载均衡方面的功能很强大(支持cookie track, header rewrite等等)
  • 支持双机热备
  • 支持虚拟主机
  • 支持健康检查
  • 同时还提供直观的监控页面,可以清晰实时的监控服务集群的运行状况。
  • 同时支持Linux 2.6内核中System Epoll,通过简化系统调用,大幅的提高了网络I/O性能。

二、HAProxy配置

HAProxy的配置过程分为3个主要部分:

  1. 命令行参数,这是最优先的;
  2. global(全局)段,设置进程级参数;
  3. 代理配置段,通常位于default,listen,backend这样的形式内。
  4. 配置文件的语法是由关键字后跟可选的一个或者多个参数(参数之间有空格)组成。如果字符串中包含空格,必须用’\’进行转义

三.haproxy负载均衡服务器部署

实验环境

server3(haproxy服务器,关掉http) 172.25.254.3
server4(httpd后台服务器) 172.25.254.4
server5(httpd后台服务器) 172.25.254.5

 

(1)在server3上安装haproxy,运用系统自带的haproxy安装包

yum install haproxy -y

(2)启动服务 配置haproxy.cfg

systemctl start haproxy
[root@server3 haproxy]# cat haproxy.cfg 

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
#    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  app1 172.25.254.4:80 check
    server  app2 172.25.254.5:80 check

  (3)修改完配置文件  重启服务 

(4)测试   在server1上(172.25.254.1)

curl 172.25.254.3

负载均衡的高可用集群(5)-haproxy的介绍及负载均衡服务器部署_第1张图片

你可能感兴趣的:(企业自动化运维架构)