haproxy源码安装:
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