HAProxy 入门实战(2)--简单使用
  BCkntiJu7RTL 2023年11月02日 52 0

本文主要介绍 HAProxy 的实际使用,文中所使用到的软件版本:Centos 7.9.2009、HAProxy 2.8.2。

1、全局配置

全局配置位于 global 部分,该部分的参数是进程范围的,通常特定于操作系统。它们通常仅设置一次,并且在设置正确后不需要更改。其中一些参数具有命令行等效项。

global
  log 127.0.0.1 local0 info
  maxconn 20480 #最大连接数
  daemon  #后台运行
  pidfile /home/mongo/soft/haproxy-2.8.2/logs/haproxy.pid

HAProxy 通过 Syslog 记录日志,要使日志能正常记录并方便查看需要对 Syslog 进行适当配置,编辑 /etc/rsyslog.conf 文件:

#取消以下注释,开启Syslog UDP端口
$ModLoad imudp
$UDPServerRun 514

#新增一行,配置单独日志文件
local0.*     /home/mongo/soft/haproxy-2.8.2/logs/haproxy.log

然后重启 Syslog:

systemctl restart syslog

2、代理配置

代理配置包含如下部分:

- defaults [<name>] [ from <defaults_name> ]
- frontend <name>   [ from <defaults_name> ]
- backend  <name>   [ from <defaults_name> ]
- listen   <name>   [ from <defaults_name> ]

default: 代理默认配置参数,frontend,backend,Listen 继承该部分参数;该部分名称是可选的
frontend: 该部分描述了一组监听套接字,用于接受客户端连接
backend: 该部分部分描述了一组服务器,代理客户端连接转发到这些服务器
listen: 该部分定义了一个完整的代理,将前端和后端部分组合在一起;通常适用于仅有 TCP 流量的场景

2.1、default 配置

defaults
  mode http
  log global #使用全局日志配置
  option httplog #启用HTTP请求、会话状态和计时器的日志记录
  timeout connect 10s #连接后端服务器的超时时间
  timeout client  30s #客户端的最大非活动时间
  timeout server  30s #服务端的最大非活动时间
  timeout check   5s  #检测超时时间

2.2、http 代理配置

A、使用 front 和 backend 配置

frontend http-9090
  bind 0.0.0.0:9090
  default_backend tomcat-cluster
backend tomcat-cluster
  balance source
  option httpchk GET /
  http-check expect status 200
  server tomcat1 127.0.0.1:7070 weight 3 check inter 2000 rise 2 fall 3
  server tomcat2 127.0.0.1:8080 weight 3 check inter 2000 rise 2 fall 3

B、使用 listen 配置

listen http-9091
  bind 0.0.0.0:9091
  balance source
  option httpchk GET /
  http-check expect status 200
  server tomcat1 127.0.0.1:7070 weight 3 check inter 2000 rise 2 fall 3
  server tomcat2 127.0.0.1:8080 weight 3 check inter 2000 rise 2 fall 3

C、后端为 https

listen http-9092
  bind 0.0.0.0:9092
  balance roundrobin
  option httpchk GET /
  http-check expect status 200
  server baidu www.baidu.com:443 ssl verify none check

HAProxy 支持多种负载均衡算法:

roundrobin: 根据权重轮询(动态)
static-rr: 根据权重轮询(静态)
leastconn: 请求发送到连接数最少的服务器(动态)
first: 请求发送到第一台可用服务器
hash: 根据配置的表达式计算hash以选择服务器(静态)
source: 根据客户端ip计算hash以选择服务器(默认静态),类似Nginx的ip_hash
uri: 根据uri计算hash以选择服务器(默认静态)
url_param: 根据请求参数计算hash以选择服务器(默认静态)
hdr(<name>): 根据请求头计算hash以选择服务器(默认静态)
random/random(<draws>):根据权重随机选择服务器(动态)
rdp-cookie/rdp-cookie(<name>): 根据rdp(远程桌面)协议的cookie计算hash以选择服务器(静态)

2.3、https 代理配置

2.3.1、生成证书

启用 Https,需要使用 OpenSSL 创建证书。

A、生成根证书

openssl genrsa -out ca.key
openssl req -new -key ca.key -out ca.csr
openssl x509 -req -days 3650 -in ca.csr -signkey ca.key -out ca.pem

B、生成服务端证书

openssl genrsa -out server.key
openssl req -new -key server.key -out server.csr
openssl ca -days 3650 -in server.csr -cert ca.pem -keyfile ca.key -out server.pem
cp server.key server.pem.key #HAproxy没有单独配置key的参数,是在同目录下用 证书名称+".key" 来查找key文件

C、生成客户端证书

openssl genrsa -des3 -out client.key 1024
openssl req -new -key client.key -out client.csr
openssl ca -days 1500 -in client.csr -cert ca.pem -keyfile ca.key -out client.pem
openssl pkcs12 -export -clcerts -in client.pem -inkey client.key -out client.p12

使用 OpenSSL 创建证书的详细说明可参考:OpenSSL 介绍(5)--数字证书;这里生成的证书假设都存放在 /home/mongo/soft/haproxy-2.8.2/ssl 目录下。

2.3.2、HAProxy 中配置 https

listen https-6060
  bind 0.0.0.0:6060 ssl crt /home/mongo/soft/haproxy-2.8.2/ssl/server.pem
  #bind 0.0.0.0:6060 ssl crt /home/mongo/soft/haproxy-2.8.2/ssl/server.pem ca-file /home/mongo/soft/haproxy-2.8.2/ssl/ca.pem verify required
  balance roundrobin
  server tomcat1 127.0.0.1:7070 weight 3 check inter 2000 rise 2 fall 3
  server tomcat2 127.0.0.1:8080 weight 3 check inter 2000 rise 2 fall 3

如果需要开启客户端验证,则注释掉第二行的配置,取消注释第三行的配置即可。开启了客户端验证,客户端访问时需要使用客户端证书(client.p12)来访问;浏览器访问导入该证书即可,Java 客户端的访问可参考:Java调用Http/Https接口(2)--HttpURLConnection/HttpsURLConnection调用Http/Https接口

2.3.3、不同 URL 转发到不同的后端服务

frontend http-9096
  bind 0.0.0.0:9096
  acl abc path_beg /abc
  acl xyz path_beg /xyz
  use_backend abcapp if abc #以/abc开头的请求转发到abcapp后端服务
  use_backend xyzapp if xyz #以/xyz开头的请求转发到xyzapp后端服务
  default_backend abcapp

backend abcapp
  balance source
  server web1 127.0.0.1:7070 weight 3 check inter 2000 rise 2 fall 3

backend xyzapp
  balance source
  server web1 127.0.0.1:8080 weight 3 check inter 2000 rise 2 fall 3

2.3.4、会话保持

A、cookie 方式

listen http-9093
  bind 0.0.0.0:9093
  cookie server-id insert nocache #第一次访问时设置该cookie(值为:server1 或 server2),以后访问带上该cookie,就可以转发到相同的后端服务器上
  server tomcat1 127.0.0.1:7070 cookie server1 weight 3 check inter 2000 rise 2 fall 3
  server tomcat2 127.0.0.1:8080 cookie server2 weight 3 check inter 2000 rise 2 fall 3

B、strict-table 方式

listen http-9094
  bind 0.0.0.0:9094
  balance roundrobin
  stick-table type ip size 1m expire 5m
  stick on src
  server tomcat1 127.0.0.1:7070 weight 3 check inter 2000 rise 2 fall 3
  server tomcat2 127.0.0.1:8080 weight 3 check inter 2000 rise 2 fall 3

C、源地址 hash 方式

listen http-9095
  bind 0.0.0.0:9095
  balance source
  server tomcat1 127.0.0.1:7070 weight 3 check inter 2000 rise 2 fall 3
  server tomcat2 127.0.0.1:8080 weight 3 check inter 2000 rise 2 fall 3

2.3.5、配置状态统计页面

listen admin_stats
  bind 0.0.0.0:4001
  stats refresh 10 #自动刷新间隔
  stats uri /stats #统计页面url
  stats realm HAProxy_Statistics
  stats auth admin:123456 #统计页面用户名密码

访问地址为:http://10.49.196.33:4001/stats

2.3.6、tcp 代理配置

listen mysql
  bind 0.0.0.0:4306
  mode tcp
  balance roundrobin
  option tcplog
  option tcpka #向客户端和服务端发送TCP keepalive数据包,以保持连接为活动状态
  server mysql_1 127.0.0.1:3306 check

3、完整配置文件

global
  log 127.0.0.1 local0 info
  maxconn 20480
  daemon
  pidfile /home/mongo/soft/haproxy-2.8.2/logs/haproxy.pid

defaults
  mode http
  log global
  option httplog
  timeout connect 10s
  timeout client  30s
  timeout server  30s
  timeout check   5s

frontend http-9090
  bind 0.0.0.0:9090
  default_backend tomcat-cluster
backend tomcat-cluster
  balance source
  option httpchk GET /
  http-check expect status 200
  server tomcat1 127.0.0.1:7070 weight 3 check inter 2000 rise 2 fall 3
  server tomcat2 127.0.0.1:8080 weight 3 check inter 2000 rise 2 fall 3

listen http-9091
  bind 0.0.0.0:9091
  balance source
  option httpchk GET /
  http-check expect status 200
  server tomcat1 127.0.0.1:7070 weight 3 check inter 2000 rise 2 fall 3
  server tomcat2 127.0.0.1:8080 weight 3 check inter 2000 rise 2 fall 3

listen http-9092
  bind 0.0.0.0:9092
  balance roundrobin
  option httpchk GET /
  http-check expect status 200
  server baidu www.baidu.com:443 ssl verify none check

listen https-6060
  bind 0.0.0.0:6060 ssl crt /home/mongo/soft/haproxy-2.8.2/ssl/server.pem
  #bind 0.0.0.0:6060 ssl crt /home/mongo/soft/haproxy-2.8.2/ssl/server.pem ca-file /home/mongo/soft/haproxy-2.8.2/ssl/ca.pem veri
fy required
  balance roundrobin
  server tomcat1 127.0.0.1:7070 weight 3 check inter 2000 rise 2 fall 3
  server tomcat2 127.0.0.1:8080 weight 3 check inter 2000 rise 2 fall 3

listen http-9093
  bind 0.0.0.0:9093
  cookie server-id insert nocache
  server tomcat1 127.0.0.1:7070 cookie server1 weight 3 check inter 2000 rise 2 fall 3
  server tomcat2 127.0.0.1:8080 cookie server2 weight 3 check inter 2000 rise 2 fall 3

listen http-9094
  bind 0.0.0.0:9094
  balance roundrobin
  stick-table type ip size 1m expire 5m
  stick on src
  server tomcat1 127.0.0.1:7070 weight 3 check inter 2000 rise 2 fall 3
  server tomcat2 127.0.0.1:8080 weight 3 check inter 2000 rise 2 fall 3

listen http-9095
  bind 0.0.0.0:9095
  balance source
  server tomcat1 127.0.0.1:7070 weight 3 check inter 2000 rise 2 fall 3
  server tomcat2 127.0.0.1:8080 weight 3 check inter 2000 rise 2 fall 3

frontend http-9096
  bind 0.0.0.0:9096
  acl abc path_beg /abc
  acl xyz path_beg /xyz
  use_backend abcapp if abc
  use_backend xyzapp if xyz
  default_backend abcapp

backend abcapp
  balance source
  server web1 127.0.0.1:7070 weight 3 check inter 2000 rise 2 fall 3

backend xyzapp
  balance source
  server web1 127.0.0.1:8080 weight 3 check inter 2000 rise 2 fall 3
  
listen admin_stats
  bind 0.0.0.0:4001
  stats refresh 10
  stats uri /stats
  stats realm HAProxy_Statistics
  stats auth admin:123456

listen mysql
  bind 0.0.0.0:4306
  mode tcp
  balance roundrobin
  option tcplog
  option tcpka
  server mysql_1 127.0.0.1:3306 check
haproxy.cfg

 

 

参考:http://docs.haproxy.org/2.8/configuration.html

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
BCkntiJu7RTL