利用haproxy作为端口转发并通过API管理配置

背景

服务作为端口转发的方式有很多,可以frp,可以iptables等等,还有个haproxy,这个我们一般是用来作为负载均衡服务器的,这个也可以实现
选择这个haproxy的原因是这个带了一个api的接口,这样如果需要进行远程批量配置的时候,就比直接上去改要方便

相关软件

haproxy的软件,这个版本要选3版本,这个因为配置工具dataplaneapi是有版本要求的,是版本2或者版本3,而centos7的版本比较低,存在兼容性问题,也就是最少系统建议是centos9的版本的
然后就按照haproxy
dataplaneapi这个是官方的软件我们版本都选3的版本

软件环境

1
2
3
4
[root@lab203 ~]# rpm -qa|grep haproxy
haproxy-awslc-3.3.10-1.el9_ha33.x86_64
[root@lab203 ~]# rpm -qa|grep dataplaneapi
dataplaneapi-3.3.5-1.x86_64

这个大版本匹配问题就不大

软件配置

1
2
3
4
5
6
# ---------------------------------------------------------------------
# Global settings
# ---------------------------------------------------------------------
global
# stats socket /var/lib/haproxy/stats
stats socket /var/run/haproxy.sock mode 660 level admin

这个默认的需要改下,这个是按官方推荐的修改,按官方的修改即可

1
2
userlist dataplaneapi
user admin insecure-password adminpwd

还要增加一个这个用户名称的接口的,上面也是用的官方的用户名密码,改好了后重启软件即可

dataplaneapi这个就正常安装,然后启动即可,默认是监听的5555端口,有需要可以去配置文件里面修改

接口相关的

注意通过api进行配置的时候,配置文件是有版本的,这个是避免并发操作的时候产生了错乱的问题,所以按要求来即可,拿到版本号,然后进行相关的请求

我的配置需求很简单

1
2
3
4
5
6
7
8
frontend ssh_in
mode tcp
bind *:8022
default_backend ssh_out

backend sshto-235
mode tcp
server s1 192.168.0.235:22

就是这样的一个需求,通过这台服务器的8022端口来访问到192.168.0.235的22端口的

配置顺序

配置的顺序有要求

  • 1、增加一个backends
  • 2、再backends里面增加server
  • 3、增加一个frontend并制定创建的backends
  • 4、给frontend增加绑定bind

按这个顺序操作的

backends相关的操作

查看当前的backends

1
2
3
curl --request GET \
--user admin:adminpwd \
"http://localhost:5555/v3/services/haproxy/configuration/backends"

增加一个backend

1
2
3
4
5
6
7
8
9
10
CFGVER=$(curl -s -u admin:adminpwd http://localhost:5555/v3/services/haproxy/configuration/version)
curl --request POST \
--user admin:adminpwd \
--header "Content-Type: application/json" \
-d '{
"name": "sshto-235",
"mode": "tcp"
}
}' \
"http://localhost:5555/v3/services/haproxy/configuration/backends?version=$CFGVER"

删除这个backend(清理步骤的)

1
2
3
4
CFGVER=$(curl -s -u admin:adminpwd http://localhost:5555/v3/services/haproxy/configuration/version)
curl --request DELETE \
--user admin:adminpwd \
"http://localhost:5555/v3/services/haproxy/configuration/backends/sshto-235?version=$CFGVER"

backend增加server

1
2
3
4
5
6
7
CFGVER=$(curl -s -u admin:adminpwd http://localhost:5555/v3/services/haproxy/configuration/version)
curl --request POST --user admin:adminpwd --header "Content-Type: application/json" -d '{
"address": "192.168.0.235",
"port": 22,
"name": "s1"
}' "http://localhost:5555/v3/services/haproxy/configuration/backends/sshto-235/servers?version=$CFGVER"
{"address":"192.168.0.235","name":"s1","port":22}

frontend相关操作

增加一个frontend

1
2
3
4
5
6
7
8
9
10
CFGVER=$(curl -s -u admin:adminpwd http://localhost:5555/v3/services/haproxy/configuration/version)
curl --request POST \
--user admin:adminpwd \
--header "Content-Type: application/json" \
-d '{
"name": "ssh_in",
"mode": "tcp",
"default_backend": "sshto-235"
}' \
"http://localhost:5555/v3/services/haproxy/configuration/frontends?version=$CFGVER"

增加bind

1
2
3
4
5
6
7
8
9
10
11
CFGVER=$(curl -s -u admin:adminpwd http://localhost:5555/v3/services/haproxy/configuration/version)
curl --request POST \
--user admin:adminpwd \
--header "Content-Type: application/json" \
-d '{
"name": "8022",
"address": "*",
"port": 8022
}' \
"http://localhost:5555/v3/services/haproxy/configuration/frontends/ssh_in/binds?version=$CFGVER"

删除backend(清理操作)

1
2
3
4
CFGVER=$(curl -s -u admin:adminpwd http://localhost:5555/v3/services/haproxy/configuration/version)
curl --request DELETE \
--user admin:adminpwd \
"http://localhost:5555/v3/services/haproxy/configuration/frontends/ssh_in?version=$CFGVER"

上面的操作完成后,相关的映射就做好了

清理步骤

清理步骤会少两步(上面配置过程有写清理的方法)

  • 1、清理frontend
  • 2、清理backend

总结

本篇是通过api来管理haproxy的相关步骤


利用haproxy作为端口转发并通过API管理配置
https://zphj1987.com/2026/06/24/利用haproxy作为端口转发并通过API管理配置/
作者
zphj1987
发布于
2026年6月24日
许可协议