Skip to content

Instantly share code, notes, and snippets.

@haproxytechblog
Last active May 9, 2021 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save haproxytechblog/15479bfa566b67e97ab7abfcd7dd91b4 to your computer and use it in GitHub Desktop.
Save haproxytechblog/15479bfa566b67e97ab7abfcd7dd91b4 to your computer and use it in GitHub Desktop.
Programmatic HAProxy Configuration Using the Data Plane API
userlist dataplane-api
user dataplaneapi password $5$oui1y1Q7o$VXoOr7Ns9WLyDFkWH0LTu9oysn/zyYhF4mSQHe9Ba5D
program api
command /opt/hapee-extras/sbin/hapee-dataplane-api --host 192.168.122.14 --port 5555 -c /etc/hapee-2.0/hapee-lb.cfg -u dataplane-api -b /opt/hapee-2.0/sbin/hapee-lb -r "service hapee-2.0-lb reload" --reload-delay 5 -m /var/run/hapee-2.0/hapee-lb.sock --log-to=file --log-file=/var/log/hapee-runtime-api.log
$ curl -X GET \
-s \
--user dataplaneapi:example \
192.168.122.14:5555/v1/services/haproxy/configuration/frontends | python -m json.tool
{
"_version": 1,
"data": [
{
"default_backend": "be_app",
"name": "fe_main"
}
]
}
$ curl -X POST \
-s \
--user dataplaneapi:example \
192.168.122.14:5555/v1/services/haproxy/transactions?version=1 | python -m json.tool
{
"_version": 1,
"id": "633e5ed4-8407-4e8f-9197-270df4c41f67",
"status": "in_progress"
}
$ curl -X POST \
-s \
-H "Content-Type: application/json" \
--user dataplaneapi:example \
-d'{"name": "be_site", "mode": "http", "httpchk": { "method": "HEAD", "uri": "/", "version": "HTTP/1.0" }}' \
192.168.122.14:5555/v1/services/haproxy/configuration/backends?transaction_id=633e5ed4-8407-4e8f-9197-270df4c41f67 | python -m json.tool
{
"httpchk": {
"method": "HEAD",
"uri": "/",
"version": "HTTP/1.0"
},
"mode": "http",
"name": "be_site"
}
$ curl -X POST \
-s \
-H "Content-Type: application/json" \
--user dataplaneapi:example \
-d'{"name": "srv1", "address": "192.168.122.1", "port": 80, "check": "enabled", "max-connections": 100}' \
'192.168.122.14:5555/v1/services/haproxy/configuration/servers?backend=be_site&transaction_id=633e5ed4-8407-4e8f-9197-270df4c41f67' | python -m json.tool
{
"address": "192.168.122.1",
"check": "enabled",
"name": "srv1",
"port": 80
}
$ curl -X POST \
-s -H "Content-Type: application/json" \
--user dataplaneapi:example \
-d'{"name": "fe_site", "default_backend": "be_site", "mode": "http", "httplog": true}' \
'192.168.122.14:5555/v1/services/haproxy/configuration/frontends?transaction_id=633e5ed4-8407-4e8f-9197-270df4c41f67' | python -m json.tool
{
"default_backend": "be_site",
"httplog": true,
"mode": "http",
"name": "fe_site"
}
$ curl -X POST \
-s \
-H "Content-Type: application/json" \
--user dataplaneapi:example \
-d'{"name": "ssl", "address": "192.168.122.14", "port": 443, "ssl": true, "ssl_certificate": "/etc/hapee-2.0/certs/ssl.pem"}' \
'192.168.122.14:5555/v1/services/haproxy/configuration/binds?frontend=fe_site&transaction_id=633e5ed4-8407-4e8f-9197-270df4c41f67' | python -m json.tool
{
"address": "192.168.122.14",
"name": "ssl",
"port": 443,
"ssl": true,
"ssl_certificate": "/etc/hapee-2.0/certs/ssl.pem"
}
$ curl -X PUT \
-H "Content-Type: application/json" \
--user dataplaneapi:example \
"192.168.122.64:5555/v1/services/haproxy/transactions/633e5ed4-8407-4e8f-9197-270df4c41f67"| python -m json.tool
{
"_version": 1,
"id": "633e5ed4-8407-4e8f-9197-270df4c41f67",
"status": "success"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment