Skip to content

Instantly share code, notes, and snippets.

@haproxytechblog
Last active May 13, 2020 20:36
Show Gist options
  • Save haproxytechblog/0a199786f091cf5179219e723bce2e83 to your computer and use it in GitHub Desktop.
Save haproxytechblog/0a199786f091cf5179219e723bce2e83 to your computer and use it in GitHub Desktop.
HAProxy Data Plane API 2.0 Adds More Dynamic Features
$ cat urls.map
/api/ be_api
/documentation/ be_documentation
/blog/ be_blog
$ curl -X POST \
-F "fileUpload=@urls.map" \
-u dataplaneapi:password \
http://192.168.50.20:5555/v2/services/haproxy/runtime/maps
$ curl -X POST \
-H 'Content-Type: application/json' \
-u dataplaneapi:password \
-d '{"name":"%[path,lower,map_beg(/etc/haproxy/maps/urls.map,be_main)]","index":0}' \
"http://192.168.50.20:5555/v2/services/haproxy/configuration/backend_switching_rules?frontend=fe_main&version=1"
$ curl -X GET -u dataplaneapi:password 192.168.50.20:5555/v2/services/haproxy/runtime/maps
[
{
"description": "pattern loaded from file '/etc/haproxy/maps/urls.map' used by map at file '/etc/haproxy/haproxy.cfg' line 37",
"file": "/etc/haproxy/maps/urls.map",
"id": "1"
}
]
$ curl -X POST \
-H 'Content-Type: application/json' \
-u dataplaneapi:password \
-d '{"key":"/images/", "value": "be_static"}' \
"http://192.168.50.20:5555/v2/services/haproxy/runtime/maps_entries?map=urls.map"
$ curl -X DELETE \
-u dataplaneapi:password \
"192.168.50.20:5555/v2/services/haproxy/runtime/maps_entries/%2Fapi%2F?map=urls.map"
$ curl -X POST \
-H 'Content-Type: application/json' \
-u dataplaneapi:password \
-d '{"name": "login_requests", "stick_table": {"type": "ip", "size": 1000000, "expire": 600, "store": "http_req_rate(10m)"}}' \
'192.168.50.20:5555/v2/services/haproxy/configuration/backends?version=1'
$ curl -X GET -u dataplaneapi:password \
'192.168.50.20:5555/v2/services/haproxy/runtime/stick_tables'
$ curl -X GET -u dataplaneapi:password \
'192.168.50.20:5555/v2/services/haproxy/runtime/stick_tables/login_requests?process=1'
$ curl -X GET -u dataplaneapi:password \
'192.168.50.20:5555/v2/services/haproxy/runtime/stick_table_entries?process=1&stick_table=http_requests'
peers lb_cluster
peer host1 192.168.50.20:10000
peer host2 192.168.50.21:10000
backend http_requests
stick-table type ip size 1m expire 10m peers lb_cluster store http_req_rate(10m)
$ curl -X POST \
-H 'Content-Type: application/json' \
-u dataplaneapi:password \
'192.168.50.20:5555/v2/services/haproxy/configuration/peer_section?version=1' \
-d '{"name":"lb_cluster"}'
$ curl -X POST \
-H 'Content-Type: application/json' \
-u dataplaneapi:password \
'192.168.50.20:5555/v2/services/haproxy/configuration/peer_entries?version=2&peer_section=lb_cluster' \
-d '{"name":"host1", "address":"192.168.50.20", "port":10000}'
$ curl -X PUT \
-H 'Content-Type: application/json' \
-u dataplaneapi:password \
-d '{"name": "login_requests", "stick_table": {"type": "ip", "size": 1000000, "expire": 600, "store": "http_req_rate(10m)", "peers": "lb_cluster"}}' \
'192.168.50.20:5555/v2/services/haproxy/configuration/backends/login_requests?version=1'
$ curl -X POST \
-H 'Content-Type: application/json' \
-u dataplaneapi:password \
-d '{"name": "internal_dns", "parse_resolv-conf": false, "accepted_payload_size": 8192}' \
'192.168.50.20:5555/v2/services/haproxy/configuration/resolvers?version=1'
$ curl -X POST \
-H 'Content-Type: application/json' \
-u dataplaneapi:password \
-d '{"name": "ns1", "address": "10.0.0.1", "port": 53}' \
'192.168.50.20:5555/v2/services/haproxy/configuration/nameservers?version=2&resolver=internal_dns'
resolvers internal_dns
nameserver ns1 10.0.0.1:53
accepted_payload_size 8192
$ curl -X PUT \
-H 'Content-Type: application/json' \
-u dataplaneapi:password \
-d '{"name": "server1", "address": "server1.site.com", "port": 8080, "check": "enabled", "resolvers": "internal_dns"}' \
'192.168.50.20:5555/v2/services/haproxy/configuration/servers/server1?version=1&backend=be_main'
curl -X POST \
-H 'Content-Type: application/json' \
-u dataplaneapi:password \
-d '{"index": 0, "type": "track-sc0", "track-sc0-key": "src", "track-sc0-table": "login_requests", "cond": "if", "cond_test": "{ path_beg /login }"}' \
'192.168.50.20:5555/v2/services/haproxy/configuration/http_request_rules?version=1&parent_type=frontend&parent_name=fe_main'
http-request track-sc0 src table login_requests if { path_beg /login }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment