haproxy: how to prepare a server maintenance without kicking app sessions?

Solution 1:

If you use socat for communicate with your haproxy configurations, you can put a server in drain state of following way:

echo "set server backend/serv state drain" | sudo socat stdio /etc/haproxy/haproxysock

More commands here! For install socat in Ubuntu go to this answer

I test that with haproxy 1.6.3 version :)

Solution 2:

Put the server in drain mode using the web management interface. That provides the exact functionality you're looking for.

Solution 3:

For the future reader that wants to drain a backend from a HAProxy container...

docker exec $(docker ps | grep haproxy | awk '{print $1}') bash -c 'echo "set server backend_name/svc_name state drain" | nc -U /haproxy.sock'

The /haproxy.sock path is the one you have set in the haproxy.cfg file [eg stats socket /haproxy.sock mode 660 level admin]