Is there a way to add more backend server to haproxy without restarting haproxy?

Solution 1:

I haven't tested this specific use case but haproxy does support a "hot reload":

2.4.1) Hot reconfiguration
--------------------------
The '-st' and '-sf' command line options are used to inform previously running
processes that a configuration is being reloaded. They will receive the SIGTTOU
signal to ask them to temporarily stop listening to the ports so that the new
process can grab them. If anything wrong happens, the new process will send
them a SIGTTIN to tell them to re-listen to the ports and continue their normal
work. Otherwise, it will either ask them to finish (-sf) their work then softly
exit, or immediately terminate (-st), breaking existing sessions. A typical use
of this allows a configuration reload without service interruption :

 # haproxy -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)

If you have an init script to start and stop haproxy it likely supports the reload argument with a function like:

haproxy_reload()
{
    $HAPROXY -f "$CONFIG" -p $PIDFILE -D $EXTRAOPTS -sf $(cat $PIDFILE) \
        || return 2
    return 0
}

Solution 2:

From the manual:

> 1.6) Helping process management

Haproxy now supports the notion of pidfile. If the '-p' command line argument, or the 'pidfile' global option is followed with a file name, this file will be removed, then filled with all children's pids, one per line (only in daemon mode). This file is NOT within the chroot, which allows to work with a readonly chroot. It will be owned by the user starting the process, and will have permissions 0644.

Example :

global
    daemon
    quiet
    nbproc  2
    pidfile /var/run/haproxy-private.pid

# to stop only those processes among others :
# kill $(</var/run/haproxy-private.pid)

# to reload a new configuration with minimal service impact and without
# breaking existing sessions :
# haproxy -f haproxy.cfg -p /var/run/haproxy-private.pid -sf $(</var/run/haproxy-private.pid)

Solution 3:

Also depending on your HA-proxy version you might want to consider the HA-Proxy Dynamic API as described by haproxy.com in this page: https://www.haproxy.com/blog/dynamic-scaling-for-microservices-with-runtime-api/

The HA-Proxy Dynamic API comes with the Enterprise version.

You should consider the HA-Proxy Dynamic API if you want to add/remove servers on the fly as a usual practice or if your project implies such a use case.