haproxy简介
haproxy是一个提供高可用性,负载均衡的应用代理,支持的协议有TCP,HTTP,并且支持虚拟主机,特别适用于那些负载量大的web应用,这些应用通常需要会话保持或者七层处理,HAProxy实现了一种事件驱动、单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
haproxy的特性
haproxy目前最新的版本是1.4系列的,提供了很多比较1.3的新的特性:
1.支持客户端侧的长链接(client-side keep-alive)
2.支持TCP加速(TCP speedups)
3.支持响应池(response buffering)
4.支持RDP协议
5.支持基于源的粘性(source-based stickiness)
6.有更好的统计数据接口(a much better stats interfaces)
7.有更详细的健康状态检测机制(more verbose health checks)
8.有基于流量的健康评估机制(traffic-based health)
9.支持HTTP认证
10.服务器管理命令行接口(server management from the CLI)
11.基于ACL的持久性(ACL-based persistence)
haproxy的性能
1.haproxy使用单进程,事件驱动模型降低了上下午切换的开销及内存的占用
2.O(1)事件查看器(event checker)允许其在高并发连接中对任何连接的任何事件实现即时探测
3.单缓冲机制,不会复制任何数据,节约CPU时钟周期
4.可以实现零复制转发,在Linux kernel 3.5以上还支持零复制启动
5.MRU内存分配器在固定大小的内存池中可实现即时内存分配,这能够显著减少创建一个会话的时长
6.采用树型存储
7.大部分工作都在用户空间完成,如时间读取、缓冲聚合及文件描述符的启用和禁用等
haproxy的配置
安装依赖包
[root@localhost ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
//创建用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin haproxy
//解压软件包
[root@localhost ~]# tar xf haproxy-v2.3.0.tar.gz
//进到解压的目录里
[root@localhost ~]# cd haproxy-2.3.0/
[root@localhost haproxy-2.3.0]# ls
BRANCHES CONTRIBUTING include MAINTAINERS reg-tests src VERDATE
CHANGELOG doc INSTALL Makefile ROADMAP SUBVERS VERSION
contrib examples LICENSE README scripts tests
//编译安装
[root@localhost haproxy-2.3.0]# make -j $(grep 'processor' /proc/cpuinfo |wc -l) \
> TARGET=linux-glibc \
> USE_OPENSSL=1 \
> USE_ZLIB=1 \
> USE_PCRE=1 \
> USE_SYSTEMD=1
[root@localhost haproxy-2.3.0]# make install PREFIX=/usr/local/haproxy
//配置各个负载的内核参数
[root@localhost haproxy-2.3.0]# echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
[root@localhost haproxy-2.3.0]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@localhost haproxy-2.3.0]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
//做一个环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/haproxy/sbin:$PATH' > /etc/profile.d/haproxy.sh
[root@localhost ~]# source /etc/profile.d/haproxy.sh
[root@localhost ~]# which haproxy
/usr/local/haproxy/sbin/haproxy
//编写配置文件
[root@localhost ~]# mkdir /etc/haproxy
[root@localhost ~]# cat > /etc/haproxy/haproxy.cfg <<EOF
> #--------------全局配置----------------
> global
> log 127.0.0.1 local0 info
> #log loghost local0 info
> maxconn 20480
> #chroot /usr/local/haproxy
> pidfile /var/run/haproxy.pid
> #maxconn 4000
> user haproxy
> group haproxy
> daemon
> #---------------------------------------------------------------------
> #common defaults that all the 'listen' and 'backend' sections will
> #use if not designated in their block
> #---------------------------------------------------------------------
> defaults
> mode http
> log global
> option dontlognull
> option httpclose
> option httplog
> #option forwardfor
> option redispatch
> balance roundrobin
> timeout connect 10s
> timeout client 10s
> timeout server 10s
> timeout check 10s
> maxconn 60000
> retries 3
> #--------------统计页面配置------------------
> listen admin_stats
> bind 0.0.0.0:8189
> stats enable
> mode http
> log global
> stats uri /haproxy_stats
> stats realm Haproxy\ Statistics
> stats auth admin:admin
> #stats hide-version
> stats admin if TRUE
> stats refresh 30s
> #---------------web设置-----------------------
> listen webcluster
> bind 0.0.0.0:80
> mode http
> #option httpchk GET /index.html
> log global
> maxconn 3000
> balance roundrobin
> cookie SESSION_COOKIE insert indirect nocache
> server web01 172.16.103.130:80 check inter 2000 fall 5
> #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
> EOF
haproxy.service文件编写
[root@localhost ~]# cat > /usr/lib/systemd/system/haproxy.service <<EOF
> [Unit]
> Description=HAProxy Load Balancer
> After=syslog.target network.target
>
> [Service]
> ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
> ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
> ExecReload=/bin/kill -USR2 $MAINPID
>
> [Install]
> WantedBy=multi-user.target
> EOF
[root@localhost ~]# systemctl daemon-reload
启动日志
[root@localhost haproxy-2.3.0]# vim /etc/rsyslog.conf
local0.* /var/log/haproxy.log
重启服务
```cpp
[root@localhost ~]# systemctl enable --now haproxy
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
登录方式:
在浏览器界面输入:
192.168.98.123:8189/haproxy_stats