HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。————百度百科
安装:
[root@sherry ~]# yum install haproxy -y
基于session的具体配置:
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
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
frontend web
log 127.0.0.1 local3
bind :80,:443
default_backend webservers
backend webservers
option httpchk
cookie webcookie insert nocache
balance roundrobin
server s1 martin:80 check inter 3000 rise 2 fall 5 cookie s1
server s2 lucia:80 check inter 30000 rise 2 fall 5 cookie s2
listen stats
bind *:8009
stats enable
stats uri /haproxy?stats
stats auth admin:admin
stats hide-version
stats admin if TRUE
acl allow src 192.168.1.0/24
acl deny src 192.168.1.111
tcp-request content reject if deny
tcp-request content accept if allow
tcp-request content reject
开启日志记录:
[root@sherry ~]# vim /etc/rsyslog.conf
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514
local2.* /var/log/haproxy.log
[root@sherry ~]# /etc/init.d/rsyslog restart
测试结果: