واجهة برمجة تطبيقات HAProxy Data Plane الجديدة: مثالان على البرمجة

استخدم واجهة برمجة تطبيقات HAProxy Data Plane API للتحكم ديناميكيًا في تكوين موازن التحميل باستخدام أوامر HTTP.



يعني التصميم من أجل التوافر العالي دائمًا تقريبًا وجود موازن وكيل / تحميل عالي المستوى. يوفر الخادم الوكيل خدمات أساسية ، مثل:



  • الكشف عن الخوادم المعيبة وإزالتها
  • طابور الاتصال
  • تفريغ تشفير TLS
  • ضغط
  • التخزين المؤقت


يكمن التحدي في إبقاء تكويناتك محدثة ، وهو أمر صعب بشكل خاص حيث تنتقل الخدمات إلى حاويات وتصبح هذه الحاويات سريعة الزوال. متوفر منذ HAProxy 2.0 ، يمكنك استخدام واجهة برمجة تطبيقات HAProxy Data Plane الجديدة (الترجمة: https://habr.com/en/post/508132/ ) وهي واجهة برمجة تطبيقات REST حديثة.



تكمل واجهة برمجة تطبيقات HAProxy Data Plane API لغة التكوين HAProxy المرنة ، والتي توفر لبنات بناء لتحديد قواعد التوجيه البسيطة والمعقدة. وهي أيضًا الإضافة المثالية إلى Runtime API الحالية ، والتي تتيح لك بدء حركة المرور من الخوادم وإيقافها وتمريرها وتغيير أوزان الخادم وإدارة الفحوصات الصحية.



تمنحك واجهة برمجة تطبيقات Data Plane الجديدة القدرة على إضافة وتكوين الواجهات الخارجية والواجهات الداخلية ومنطق توجيه حركة المرور ديناميكيًا. يمكنك أيضًا استخدامه لتحديث نقاط نهاية التسجيل وإنشاء فلاتر 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 .



إذا كنت تحب هذه المقالة وتريد أن تبقى على اطلاع على مواضيع مماثلة ، اشترك في هذه المدونة. يمكنك أيضًا متابعتنا على Twitter والانضمام إلى المحادثة على Slack . يعمل HAProxy Enterprise على تسهيل بدء استخدام واجهة برمجة تطبيقات Data Plane ، حيث يمكن تثبيته كحزمة نظام ملائمة. ويتضمن أيضًا قاعدة أكواد موثوقة ومتقدمة ومجموعة شركات من الوظائف الإضافية ودعم الخبراء والخدمات المهنية. اريد معرفة المزيد؟ اتصل بنا اليوم وقم بتنزيل نسخة تجريبية مجانية.



PS Telegram chat HAproxy https://t.me/haproxy_ru




All Articles