使用HAProxy Data Plane API通过HTTP命令动态管理您的负载均衡器配置。
设计高可用性几乎总是意味着拥有高级代理/负载平衡器。代理服务器提供以下基本服务:
- 检测并清除故障服务器
- 连接队列
- TLS加密卸载
- 压缩
- 快取
挑战在于保持您的配置最新,当服务移入容器且这些容器变得短暂时,这尤其困难。自HAProxy 2.0可用以来,您可以使用新的HAProxy数据平面API(翻译:https://habr.com/en/post/508132/),它是一种现代的REST API。
HAProxy Data Plane API是灵活的 HAProxy 配置语言的补充,该语言提供了用于定义简单和复杂路由规则的构建块。它也是现有Runtime API的完美补充,它允许您启动,停止和传递来自服务器的流量,更改服务器权重以及管理运行状况检查。
新的Data Plane API使您能够动态添加和配置前端,后端和流量路由逻辑。您还可以使用它来更新日志记录端点并创建SPOE筛选器。基本上,几乎整个负载均衡器都可以使用HTTP命令进行配置。在本文中,您将学习如何最好地使用它。
配置管理
HAProxy /etc/haproxy/haproxy.cfg
. . frontend
, IP-, , backend
, , . , , , , -, , ACL .
, , . , . , TLS. , , .
HTTP API . Data Plane API . HAProxy Data Plane API HAProxy . , API API , .
Data Plane API Go config-parser client-native HAProxy Runtime API . HAProxy.
HAProxy
Data Plane API . , backend
frontend
, . API.
, GET /v1/services/haproxy/configuration/backends, backend
, :
$ curl --get --user admin:mypassword \
http://localhost:5555/v1/services/haproxy/configuration/backends
backend
, endpoint POST. — . , .
endpoint /v1/services/haproxy/transactions . URL, . , POST, PUT DELETE, , HAProxy. , API, . , , , , . .
$ curl -X POST --user admin:mypassword \
-H "Content-Type: application/json" \
http://localhost:5555/v1/services/haproxy/transactions?version=1
JSON:
{"_version":5,"id":"9663c384-5052-4776-a968-abcef032aeef","status":"in_progress"}
endpoint /v1/services/haproxy/configuration/backends, , URL:
$ curl -X POST --user admin:mypassword \
-H "Content-Type: application/json" \
-d '{"name": "test_backend", "mode":"http", "balance": {"algorithm":"roundrobin"}, "httpchk": {"method": "HEAD", "uri": "/", "version": "HTTP/1.1"}}' \
http://localhost:5555/v1/services/haproxy/configuration/backends?transaction_id=9663c384-5052-4776-a968-abcef032aeef
endpoint /v1/services/haproxy/configuration/servers backend
:
$ curl -X POST --user admin:mypassword \
-H "Content-Type: application/json" \
-d '{"name": "server1", "address": "127.0.0.1", "port": 8080, "check": "enabled", "maxconn": 30, "weight": 100}' \
"http://localhost:5555/v1/services/haproxy/configuration/servers?backend=test_backend&transaction_id=9663c384-5052-4776-a968-abcef032aeef"
frontend
endpoint /v1/services/haproxy/configuration/frontends :
$ curl -X POST --user admin:mypassword \
-H "Content-Type: application/json" \
-d '{"name": "test_frontend", "mode": "http", "default_backend": "test_backend", "maxconn": 2000}' \
http://localhost:5555/v1/services/haproxy/configuration/frontends?transaction_id=9663c384-5052-4776-a968-abcef032aeef
frontend
bind
. , endpoint /v1/services/haproxy/configuration/binds, :
$ curl -X POST --user admin:mypassword \
-H "Content-Type: application/json" \
-d '{"name": "http", "address": "*", "port": 80}' \
"http://localhost:5555/v1/services/haproxy/configuration/binds?frontend=test_frontend&transaction_id=9663c384-5052-4776-a968-abcef032aeef"
, , endpoint /v1/services/haproxy/transactions/[transaction ID] PUT, :
$ curl -X PUT --user admin:mypassword \
-H "Content-Type: application/json" \
http://localhost:5555/v1/services/haproxy/transactions/9663c384-5052-4776-a968-abcef032aeef
:
frontend test_frontend
mode http
maxconn 2000
bind *:80 name http
default_backend test_backend
backend test_backend
mode http
balance roundrobin
option httpchk HEAD / HTTP/1.1
server server1 127.0.0.1:8080 check maxconn 30 weight 100
.
. . URL transaction_id
, .
HAProxy Data Plane API. HTTP- . . ACL, Host example.com. , use_backend example_servers. http-request deny, URL /admin.php, IP- 192.168.50.20/24.
endpoint /v1/services/haproxy/transactions :
$ curl -X POST --user admin:mypassword \
-H "Content-Type: application/json" \
http://localhost:5555/v1/services/haproxy/transactions?version=2
{"_version":2,"id":"7d0d6737-655e-4489-92eb-6d29cdd69827","status":"in_progress"}
endpoint /v1/services/haproxy/configuration/backends , backend
example_servers:
$ curl -X POST --user admin:mypassword \
-H "Content-Type: application/json" \
-d '{"name": "example_servers", "mode":"http", "balance": {"algorithm":"roundrobin"}}' \
http://localhost:5555/v1/services/haproxy/configuration/backends?transaction_id=7d0d6737-655e-4489-92eb-6d29cdd69827
endpoint /v1/services/haproxy/configuration/servers server backend:
$ curl -X POST --user admin:mypassword \
-H "Content-Type: application/json" \
-d '{"name": "server1", "address": "127.0.0.1", "port": 8081, "check": "enabled", "maxconn": 30, "weight": 100}' \
"http://localhost:5555/v1/services/haproxy/configuration/servers?backend=example_servers&transaction_id=7d0d6737-655e-4489-92eb-6d29cdd69827"
endpoint /v1/services/haproxy/configuration/acls, ACL is_example, , example.com:
$ curl -X POST --user admin:mypassword \
-H "Content-Type: application/json" \
-d '{"id": 0, "acl_name": "is_example", "criterion": "req.hdr(Host)", "value": "example.com"}' \
"http://localhost:5555/v1/services/haproxy/configuration/acls?parent_type=frontend&parent_name=test_frontend&transaction_id=7d0d6737-655e-4489-92eb-6d29cdd69827"
/v1/services/haproxy/configuration/backend_switching_rules, use_backend, ACL is_example:
$ curl -X POST --user admin:mypassword \
-H "Content-Type: application/json" \
-d '{"id": 0, "cond": "if", "cond_test": "is_example", "name": "example_servers"}' \
"http://localhost:5555/v1/services/haproxy/configuration/backend_switching_rules?frontend=test_frontend&transaction_id=7d0d6737-655e-4489-92eb-6d29cdd69827"
endpoint /v1/services/haproxy/configuration/http_request_rules, http-request deny, , /admin.php, IP- 192.168.50.20/24:
$ curl -X POST --user admin:mypassword \
-H "Content-Type: application/json" \
-d '{"id": 0, "cond": "if", "cond_test": "{ path /admin.php } !{ src 192.168.50.20/24 }", "type": "deny"}' \
"http://localhost:5555/v1/services/haproxy/configuration/http_request_rules?parent_type=frontend&parent_name=test_frontend&transaction_id=7d0d6737-655e-4489-92eb-6d29cdd69827"
, :
$ curl -X PUT --user admin:mypassword \
-H "Content-Type: application/json" \
http://localhost:5555/v1/services/haproxy/transactions/7d0d6737-655e-4489-92eb-6d29cdd69827
HAProxy :
frontend test_frontend
mode http
maxconn 2000
bind *:80 name http
acl is_example req.hdr(Host) example.com
http-request deny deny_status 0 if { path /admin.php } !{ src 192.168.50.20/24 }
use_backend example_servers if is_example
default_backend test_backend
backend example_servers
mode http
balance roundrobin
server server1 127.0.0.1:8081 check maxconn 30 weight 100
backend test_backend
mode http
balance roundrobin
option httpchk HEAD / HTTP/1.1
server server1 127.0.0.1:8080 check maxconn 30 weight 100
HAProxy Data Plane API, HAProxy REST API. (: https://habr.com/ru/post/508132/). , HAProxy API . Data Plane API , , HAProxy .
API . HAProxy .
如果您喜欢本文并希望紧跟相关主题,请订阅此博客。您也可以在Twitter上关注我们,并在Slack 上加入对话。HAProxy Enterprise可以作为方便的系统软件包安装,因此可以轻松开始使用Data Plane API。它还包括健壮和高级的代码库,企业附加套件,专家支持和专业服务。想知道更多?立即与我们联系并下载免费试用版。
PS电报聊天HAproxy https://t.me/haproxy_ru