haproxy源码编译安装

haproxy源码安装:

下载地址:http://www.haproxy.org/

tar xf haproxy-1.7.7.tar.gz
cd haproxy-1.7.7
make TARGET=linux26  && make install
cp /usr/local/sbin/haproxy /usr/sbin/haproxy
cp examples/haproxy.init /etc/init.d/haproxy
chmod +x /etc/init.d/haproxy
mkdir -p /etc/haproxy
cp examples/content-sw-sample.cfg /etc/haproxy/haproxy.cfg
chkconfig --add haproxy
service haproxy start

配置分段:

global
    配置参数:log,maxconn,...,应用程序,性能,调试
proxies
    defaults:定义frontend与backend常用属性
    frontend:定义前端,用来接受用户请求,根据用户请求调度至后端
    backend:定义后端,用来定义后端服务器属性,
    listen:关联了前后端,不用分开定义

balance调度算法:

roundrobin:基于权重进行轮叫,动态算法,运行时调整,每个后端服务器最多接收4128个连接。
static-rr:基于权重进行轮叫,静态算法,后端服务器连接数上无限制
leastconn:新的连接请求被派发至后端连接最少的服务器上,有这较长时间连接的应用场景使用,动态算法,运行时调整
source:将请求的源地址进行hash运算,除模取余,使得同一个客户端IP的请求始终发送至后端某特定服务器,
    hash-type:{map-based(除摸取余,静态)|consistent(一致性哈希法,动态)}

scheme://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>
uri:<path>;<params>?<query>#<frag>
uri左半部分:<path>;<params>
 uri:对URI的左半部分("问题"标记之前的部分)
     hash-type:{map-based|consistent}
url_param:根据url中指定参数的值,把值做hash计算,除以总权重
    hash-type:{map-based|consistent}
hdr(<name>):根据http首部的字段进行调度(UA,REFERER,HOSTNAME)
    hash-type:{map-based|consistent}
rdp-cookie:    rdp远程桌面协议
rdp-cookie(name):    rdp远程桌面协议

haproxy 1.5默认配置:

# /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 *:5000
    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

frontend test *:80
    default_backend     testapp

backend testapp
    balance   roundrobin
    server    node2 192.168.3.6:80   check
    server    node3 192.168.2.33:80  check 

#---------------------------------------------------------------------
# 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 127.0.0.1:5001 check
    server  app2 127.0.0.1:5002 check
    server  app3 127.0.0.1:5003 check
    server  app4 127.0.0.1:5004 check
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 参考文档: 1.haproxy:http://www.haproxy.org/ 本文涉及haproxy的安装,并做...
    Netonline阅读 2,449评论 1 51
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,009评论 19 139
  • 在大型系统设计中用代理在负载均衡是最常见的一种方式,而相对靠谱的解决方案中Nginx、HAProxy、LVS、F5...
    欢醉阅读 4,642评论 1 5
  • 今天太晚了 大概没空写weeks了 就用简书记一下 今早还是没早起来 但上了一堂大学生职业规划课 穿着皮衣和玉婷春...
    充满活力的元气少女阅读 179评论 0 0
  • 我多么希望, 有一个门口, 早晨, 阳光照在草上。 我们站着, 扶着自己的门窗, 门很低, 但太阳是明亮的。 草在...
    陈建民阅读 310评论 0 2