首页 > 其他分享 >HAProxy 入门实战(2)--简单使用

HAProxy 入门实战(2)--简单使用

时间:2023-11-17 11:32:02浏览次数:28  
标签:HAProxy 127.0 入门 -- 0.0 rise server 0.1 check

本文主要介绍 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

HAProxy 入门实战(2)--简单使用_bc

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



标签:HAProxy,127.0,入门,--,0.0,rise,server,0.1,check
From: https://blog.51cto.com/wuyongyin/8438630

相关文章

  • python-tkinter去除命令日志
    Python打包exe文件后,执行exe文件总会打开命令行窗口,通过查找相关解决的方法,经过亲测,介绍几种可行的方案。修改文件名后缀将.py文件改成.pyw文件(使用的是脚本解析程序pythonw.exe)修改打包命令pyinstaller-i添加图标        -w去除命令行解决报错AttributeError......
  • How To Delete Reservations Using Standard API INV_RESERVATION_PUB.Delete_Reserva
    SolutionSummary:ThereservationAPIINV_RESERVATION_PUB.Delete_Reservationwilldeletereservationsacceptingthereservationidandoptionallyserialnumberstolocateandremovereservations.Careshouldbetakentoensurerelatedobjectslikesaleso......
  • [XMAN2018排位赛]通行证
    打开txt文件得到一串base64编码的字符串a2FuYmJyZ2doamx7emJfX19ffXZ0bGFsbg==解码得到一串貌似是栅栏加密的字符串kanbbrgghjl{zb____}vtlaln这里我直接使用栅栏解密,栏目数设置多少都不对,看了其他师傅的WP后才知道,这里是先进行了栅栏解密,所以我们需要进行栅栏加密的操作才......
  • Solution - Partition Game
    Link.做vjudge的题有一种美丽的窒息的感觉。设\(f_{i,j}\)表示前\(i\)个选\(j\)段出来的最小代价,转移\(f_{i,j}=\min_{0\leqk<i}\{f_{k,j-1}+w_{k+1,i}\}\),\(w_{k+1,i}\)是\([k+1,i]\)这一段的代价,时间复杂度\(O(n^2k)\),然后就不会做了/l......
  • 35个超实用excel快捷键
    以下是一些常用的Excel快捷键,希望对你有所帮助。如果你想要了解更多快捷键,可以参考Excel的官方文档或者在网上搜索相关信息。Ctrl+C:复制选定的单元格或单元格范围。Ctrl+X:剪切选定的单元格或单元格范围。Ctrl+V:粘贴复制或剪切的内容。Ctrl+Z:撤销上一步操作。Ctrl+......
  • [MRCTF2020]千层套路
    压缩包需要密码,暴力破解得知为0573发现里面压缩包的密码也是文件名0114估计是套娃题,拿脚本解压importzipfilename='0573'whileTrue:fz=zipfile.ZipFile(name+'.zip','r')fz.extractall(pwd=bytes(name,'utf-8'))name=fz.filelist[0].file......
  • Lumen框架 之api用户认证
    一、配置1、在\app\bootstrap\app.php中取消注释$app->withFacades();$app->withEloquent();$app->routeMiddleware(['auth'=>App\Http\Middleware\Authenticate::class]);$app->register(App\Providers\AuthServiceProvider::class);2、创建用户......
  • Lumen框架 之数据库迁移
    一、基本操作1、/database/migrations/目录下生成一个php文件,这个文件主要包括两个函数,在up()函数中根据你的需求定义数据库字段phpartisanmake:migrationcreate_users_table--create=users<?phpuseIlluminate\Database\Migrations\Migration;useIlluminate\Database\Sch......
  • flask取消jsonify自动排序
    将此配置行添加到应用程序定义之后的代码中:app=Flask(__name__)app.config['JSON_SORT_KEYS']=False对于Flask2.3及更高版本,请使用以下命令:app.json.sort_keys=False......
  • ! (空引用忽略判断) 操作符 (C# reference)
    ref: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving主要是.Net6开始判断引用类型是否空,在项目文件中  PropertyGroup节点下  <Nullable>enable</Nullable,代表开启 ,在这个情况我想某个变量或者属性引用不要......