Nova API de plano de dados HAProxy: dois exemplos de programação

Use a API do HAProxy Data Plane para gerenciar dinamicamente sua configuração do balanceador de carga usando comandos HTTP.



Projetar para alta disponibilidade quase sempre significa ter um alto balanceador de proxy / carga. Um servidor proxy fornece serviços básicos, como:



  • detecção e remoção de servidores com defeito
  • fila de conexão
  • descarregando a criptografia TLS
  • compressão
  • cache


O desafio é manter suas configurações atualizadas, o que é especialmente difícil à medida que os serviços passam para os contêineres e esses se tornam efêmeros. Disponível desde o HAProxy 2.0, você pode usar a nova API do HAProxy Data Plane (tradução: https://habr.com/en/post/508132/ ), que é uma API REST moderna.



A API HAProxy Data Plane complementa a linguagem de configuração flexível HAProxy, que fornece blocos de construção para definir regras de roteamento simples e complexas. É também o complemento perfeito para a API Runtime existente , que permite iniciar, parar e transmitir tráfego de servidores, alterar pesos do servidor e gerenciar verificações de integridade.



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


.



Data Plane API OpenAPI, .



. . 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 .



Se você gostou deste artigo e deseja acompanhar os tópicos relacionados, assine este blog. Você também pode nos seguir no Twitter e participar da conversa no Slack . O HAProxy Enterprise facilita a introdução à API Data Plane, pois pode ser instalada como um pacote de sistema conveniente. Também inclui uma base de código avançada e confiável, um conjunto corporativo de complementos, suporte especializado e serviços profissionais. Quer saber mais? Entre em contato conosco hoje e faça o download da avaliação gratuita.



Chat de telegrama PS HAproxy https://t.me/haproxy_ru




All Articles