HAProxy’s Data Plane API is a neat tool to manage its configuration and it has some great docs, too. But in case we only need to change backend’s server state (ready/drain/maint) it might be overkill using all of those transactions, version numbers and configuration dumps.

Luckily, there’s actually a runtime API function that’s not mentioned in the documentation, but according to this code, it’s easy to use. Here’s how:

Check backend server’s state

curl --user dataplaneapi:mypassword \
  --header "Content-Type: application/json" \
  --request GET "http://localhost:5555/v2/services/haproxy/runtime/servers/$SERVER_NAME?backend=$BACKEND_NAME" | jq .

Output:

{
  "address": "10.0.0.11",
  "admin_state": "ready",
  "id": "1",
  "name": "$SERVER_NAME",
  "operational_state": "up",
  "port": 80
}

Change backend server’s state

Prepare payload JSON file:

{
  "admin_state":"maint",
  "operational_state":"up"
}

PUT JSON file to HAProxy Data Plane API:

curl --data @payload.json \
  --user dataplaneapi:mypassword \
  --header "Content-Type: application/json" \
  --request PUT "http://localhost:5555/v2/services/haproxy/runtime/servers/$SERVER_NAME?backend=$BACKEN_NAME" | jq '.'

Output:

{
  "address": "10.0.0.11",
  "admin_state": "maint",
  "id": "1",
  "name": "$SERVER_NAME",
  "operational_state": "down",
  "port": 80
}