Haproxy
文章目录
- Haproxy
- haproxy简介
- Haproxy核心功能
- Haproxy的关键特性
- 部署:
- Haproxy搭建http负载均衡
haproxy简介
haproxy是一个免费的负载均衡软件,可以运行于大部分主流的linux操作系统上
haproxy提供了L4(tcp)和L7(http)两种负载均衡能力,具备丰富的功能。HAproxy的社区非常活跃,版本更新比较快。
Haproxy核心功能
- 负载均衡:L4和L7两种模式,支持RR、静态RR、LC、IP Hash 、url Hash、URL_PARAM等丰富的负载均衡算法
- 健康检查:支持tcp和http两种健康检查模式
- 回话保持:对于未实现会话共享的应用集群,可通过insert cookie、rewrite coookie、prefix cookie 以及上述的多种hash方式实现会话保持
- ssl:Haproxy可以解析https协议,并能够将请求解密为http后想后端传输
- http请求重定向与重写
- 监控与统计:Haproxy提供了基于web的统计信息页面,展现健康状态流量数据。基于此功能,使用者可以开发监控程序来监控Haproxy的状态。
Haproxy的关键特性
性能
- 采用单线程、事件驱动、非阻塞模型,减少上下文切换的消耗,能在1ms内处理数百个请求。并且每个会话只占用数kb的内存
- 大量精细的性能优化,入01复杂度的事件检查器、延迟更新技术、single-buffereing、zaro-copy forwarding等等,这些技术使得Haproxy在中等负载下只占用极低的cpu资源。
- HAProxy大量利用操作系统本身的功能特性,使得其在处理请求时能发挥极高的性能,通常情况下,HAProxy自身只占用15%的处理时间,剩余的85%都是在系统内核层完成的。
- HAProxy作者在8年前(2009)年使用1.4版本进行了一次测试,单个HAProxy进程的处理能力突破了10万请求/秒,并轻松占满了10Gbps的网络带宽。
稳定性
作为建议以单进程模式运行的程序,HAProxy对稳定性的要求是十分严苛的。按照作者的说法,HAProxy在13年间从未出现过一个会导致其崩溃的BUG,HAProxy一旦成功启动,除非操作系统或硬件故障,否则就不会崩溃(我觉得可能多少还是有夸大的成分)。
在上文中提到过,HAProxy的大部分工作都是在操作系统内核完成的,所以HAProxy的稳定性主要依赖于操作系统,作者建议使用2.6或3.x的Linux内核,对sysctls参数进行精细的优化,并且确保主机有足够的内存。这样HAProxy就能够持续满负载稳定运行数年之久
部署:
1源码安装
haproxy官方帮助文档:https://cbonte.github.io/haproxy-dconv/
下载安装包
haproxy源码包下载网站地址:https://src.fedoraproject.org/repo/pkgs/haproxy/
[root@localhost ~]# ls
haproxy-2.6.0.tar.gz
[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-2.6.0.tar.gz
[root@localhost ~]# ls
haproxy-2.6.0 haproxy-2.6.0.tar.gz
[root@localhost ~]# cd haproxy-2.6.0/
[root@tangyuxuan haproxy-2.6.0]# make clean
[root@tangyuxuan haproxy-2.6.0]# make -j $(grep 'processor' /proc/cpuinfo |wc -l) \
> TARGET=linux-glibc \
> USE_OPENSSL=1 \
> USE_ZLIB=1 \
> USE_PCRE=1 \
> USE_SYSTEMD=1
CC src/ev_poll.o
CC src/ev_epoll.o
CC src/cpuset.o
CC src/ssl_sample.o
-------
[root@tangyuxuan haproxy-2.6.0]# make install PREFIX=/usr/local/haproxy
install: creating directory '/usr/local/haproxy'
-------------------
[root@tangyuxuan haproxy-2.6.0]# cp haproxy /usr/sbin/
设置inux内核参数
[root@tangyuxuan haproxy-2.6.0]# vim /etc/sysctl.conf
[root@tangyuxuan haproxy-2.6.0]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
配置haproxy服务
[root@tangyuxuan haproxy-2.6.0]# vim /etc/haproxy/haproxy.cfg
[root@tangyuxuan haproxy-2.6.0]# cat /etc/haproxy/haproxy.cfg
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 你的ip:80 check inter 2000 fall 5
启动haproxy,配置haproxy.service服务单元文件
[root@tangyuxuan haproxy-2.6.0]# vim /usr/lib/systemd//system/haproxy.service
[root@tangyuxuan haproxy-2.6.0]# cat /usr/lib/systemd//system/haproxy.service
[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
配置日志信息
[root@tangyuxuan haproxy-2.6.0]# vim /etc/rsyslog.conf
#加上
local0.* /var/log/haproxy.log
[root@tangyuxuan haproxy-2.6.0]# systemctl restart rsyslog.service
[root@tangyuxuan haproxy-2.6.0]# systemctl restart haproxy.service
[root@tangyuxuan haproxy-2.6.0]# ss -atl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:kerberos 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
Haproxy搭建http负载均衡
环境
主机 | IP | 需要安装的应用 | 系统 |
centos8-stream8 | 192.168.245.128 | haproxy | 全部centos8-stream |
stream1 | 192.168.245.131 | 无 | |
stream2 | 192.168.245.129 | httpd | |
stream3 | 192.168.245.130 | httpd |
关闭所有防火墙
stream2/3准备
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo RS1 > /var/www/html/index.html
[root@localhost etc]# systemctl restart httpd
访问测试