Ubuntu 22.04 安装HA-proxy
官网
资料
# 更新系统包列表:
sudo apt update
# 安装 HAproxy
sudo apt install haproxy -y
# 验证安装
haproxy -v # 如下图
配置 Haproxy
##### 基于IP的访问控制
acl ctrl_ip src 172.25.254.1 172.25.254.20 192.168.0.0/24 # 这里可以写具体的IP地址也可以写网段##### 拒绝IP访问acl ctrl_ip src 172.25.254.1 172.25.254.20 192.168.0.0/24http-request deny if ctrl_ip## # 基于浏览器类型的访问控制
acl badwebbrowers hdr_sub(User-Agent) -i curl wget
http-request deny if badwebbrowers##基于后缀名实现动静分离acl static path_end -i .html .jpg .png .css .js # 静态 acl php path_end -i .php # 动态use_backend webcluster-host if php use_backend webxx-host if static
路径:
vim /etc/haproxy/haproxy.cfg
# 全局配置
globallog /dev/log local0 info # 日志输出到syslog,定义日志输出方式,支持 local0-local7 级别info maxconn 4096 # 每个进程最大连接数 user haproxy # 运行用户group haproxy # 运行组daemon # 以守护进程方式运行 nbproc 1 # 进程数(建议单进程,多核可调整)stats socket /run/haproxy/admin.sock mode 660 level admin # 管理接口 tune.ssl.default-dh-param 2048 # SSL默认DH参数 ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256 # 加密套件
# 默认配置
defaultsmode http # 默认模式(http/tcp/health)http是7层 tcp 4层 设置默认工作类型,使用TCP更好,减少压力option httplog # 记录HTTP日志 option dontlognull # 不记录空连接(如健康检查)# IP 穿透option forwardfor # 添加X-Forwarded-For头 透传客户端RIP至后端web服务器option redispatch # 服务不可用后的操作,重定向到其它健康的服务器option abortonclose # 当服务器负载很高时,自动结束掉当前队列处理比较久的连接,针对业务情况选择开启option http-keep-alive # 开启与客户端的会话保持option http-server-close # 服务端关闭连接,客户端保持长连接 option http-pretend-keepalive # 伪装长连接(解决某些服务器兼容性问题)option forceclose # 强制关闭连接(兼容性差时使用) 确保连接立即释放,但增加开销timeout connect 10s # 连接后端超时 timeout client 30s # 客户端超时 timeout server 30s # 服务器响应超时 retries 3 # 失败重试次数 stats uri /haproxystats auth 用户名: 密码
# 前端配置
frontend http_in# 监听80端口bind *:80 # HTTPS配置 bind *:443 ssl crt /etc/haproxy/certs/example.com.pem log: global# httplog 日志模板options: httplog# ACL规则:路径匹配 ACL(Access Control List)# hdr(host):匹配域名# path_beg:匹配URL路径前缀 acl is_static path_beg -i /static use_backend static_servers if is_static # 根据ACL选择后端 default_backend dynamic_servers # 默认后端http-request set-header X-Real-IP %[src] # 设置真实IP头
# 后端配置
backend dynamic_servers# 模式mode http# 轮询算法(默认)balance roundrobin # 会话保持(基于Cookie) cookie SERVERID insert indirect nocache # 健康检查与最大连接 #》》》》》》》》》真实的服务器,真正提供服务的 # server 名称 域名|IP:Port server s1 192.168.1.10:80 check maxconn 100 weight 1 # 备用服务器 当上面一个不工作,这个才顶上server s2 192.168.1.11:80 check backup backend static_serversbalance leastconn # 最小连接数算法 server static1 192.168.1.20:80 check
# 健康检查
backend app_serversoption httpchk GET /health # HTTP健康检查 # inter:检查间隔 每隔5秒发送检查请求,连续2次成功标记为健康,3次失败标记为故障。server app1 192.168.1.30:8080 check inter 5s rise 2 fall 3# 监控界面
listen statsbind *:8404 # 监控端口stats enablestats uri /haproxy_stats # 访问路径stats auth admin:password # 认证 stats hide-version # 隐藏版本信息
》》》配置完之后 重启 Haproxy
sudo systemctl restart haproxy
补充几个常用命令
sudo systemctl enable haproxy 开机启动
sudo systemctl status haproxy 查看状态
sudo systemctl stop haproxy 停止
sudo systemctl start haproxy 开启
sudo systemctl reload haproxy 修改配置之后 可以重新加载
# docker-compose.yml
# docker compose up -d
services:tomcat1:image: tomcat:latestcontainer_name: tomcat1ports:- 8081:8080tomcate2:image: tomcat:latestcontainer_name: tomcat2ports:- 8082:8080
》》》检测8081、8082 直接访问 正常
通过Haproxy 负载均衡
globallog /dev/log local0log /dev/log local1 noticechroot /var/lib/haproxystats socket /run/haproxy/admin.sock mode 660 level adminstats timeout 30suser haproxygroup haproxydaemon# Default SSL material locationsca-base /etc/ssl/certscrt-base /etc/ssl/private# See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediatessl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-ticketsdefaultslog globalmode httpoption httplogoption dontlognulltimeout connect 5000timeout client 50000timeout server 50000errorfile 400 /etc/haproxy/errors/400.httperrorfile 403 /etc/haproxy/errors/403.httperrorfile 408 /etc/haproxy/errors/408.httperrorfile 500 /etc/haproxy/errors/500.httperrorfile 502 /etc/haproxy/errors/502.httperrorfile 503 /etc/haproxy/errors/503.httperrorfile 504 /etc/haproxy/errors/504.http
# 监控界面配置
listen statsbind *:8404stats enablestats uri /statsstats refresh 10sstats auth admin:password # 替换为你的用户名和密码stats admin if TRUE# 前端HTTP配置
frontend http_frontbind *:80acl is_http url_reg \.jsp$use_backend http_servers if is_httpdefault_backend http_servers# 后端服务器配置
backend http_serversbalance roundrobincookie SERVERID insert indirect nocacheserver server1 172.30.xxx.XX :8081 check cookie server1server server2 172.30.XX.XX:8082 check cookie server2