HAproxy负载均衡部署

HAproxy负载均衡部署

1、安装HAproxy

yum -y install haproxy

2、编辑配置文件

#---------------------------------------------------------------------
# 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      #定义haproxy的家目录,
    pidfile     /var/run/haproxy.pid   #定义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


listen stats    #状态信息
    mode http   
    bind *:1080  #绑定端口
    stats enable #开启或者关闭
    stats hide-version #关闭版本信息
    stats uri   /haproxy?admin #web状态界面的url
    stats realm Haproxy Statistics #
    stats auth admin:admin  #认证的用户名:密码
    stats admin if TRUE     #如果认证成功开启admin
  
frontend http-in     #前端配置
   bind *:80        #前端绑定端口
   mode http        #http
   log global       #全局日志
   option httpclose #
   option logasap
   capture request header Host len 20
   capture request header Referer len 60
   default_backend http-server

backend http-server   #后端web服务集群
   balance roundrobin  #调度算法模式
   server web1 192.168.10.150:81  check maxconn 4000  #后端web列表 
   server web2 192.168.10.122:80  check maxconn 3000
原文地址:https://www.cnblogs.com/xu743876685/p/8596764.html