To run:
docker compose up # or docker-compose up if not using the docker plugin
In another terminal the run the following:
curl -v -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST localhost:4080/test
You will then see in that the container called main-backend-1
handles the request, something like:
ha-proxy-failover-main-backend-1 | 172.27.0.4 - - [20/Mar/2023:15:11:23 +0000] "POST /test HTTP/1.1" 200 0 "-" "curl/7.81.0" "-"
To simulate the main backend going down just run:
docker compose stop main-backend
Then the next time you make a curl
request it will block and might also timeout.
Eventually HA proxy (after about 5 seconds or so) will register the main backend as down and route all requests to the backup backend.
In the logs you will then see the request being handled by ``.
You might need to make the curl
request again.
ha-proxy-failover-main-backend-1 exited with code 0
ha-proxy-failover-proxy-1 | [WARNING] (7) : Server bk_main/s1 is DOWN, reason: Layer4 timeout, check duration: 2001ms. 0 active and 0 backup servers left. 1 sessions active, 0 requeued, 0 remaining in queue.
ha-proxy-failover-proxy-1 | [ALERT] (7) : backend 'bk_main' has no server available!
ha-proxy-failover-backup-backend-1 | 172.27.0.4 - - [20/Mar/2023:15:17:42 +0000] "POST /test HTTP/1.1" 200 0 "-" "curl/7.81.0" "-"
The HA config can be improved to not use a network check and instead an actual health check. So it could look like something like:
backend bk_main
mode http
option httpchk
http-check send meth GET uri /health
http-request set-header X-MY-HEADER "some_value/bk_main"
server s1 main-backend:80 check