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

Solution 1:

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

For details on the web management interface - https://github.com/Aidaho12/haproxy-wi

Solution 2:

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 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'

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

Solution 4:

Other ways

synchronize session files between servers (needs a way to synchronize files between several servers, or a common single mountpoint)

If your backend servers use PHP for the application(s) you can use Memcache to sync the sessions between them.

Also Couchbase-Server can do memcache replication out of the box.
Of course it's kind of overkill to use couchbase-server just for sessions replication :)