User Spotlight

HAProxy Process Management

HAProxy Technologies Logo
William Lallemand
HAProxy Technologies

In his HAProxyConf 2019 presentation, William Lallemand (Senior HAProxy Developer) shows how process management in HAProxy has evolved since the beginning of the project; With the advent of systemd, new techniques had to be developed so that users could reload HAProxy safely. The Master-Worker mode simplifies the management of HAProxy processes and introduces interesting features. This talk briefly reviews the history of process management in HAProxy and the CLI commands introduced through the evolution of the architecture.

Watch William’s presentation video or read the transcript below.

Explore more HAProxyConf 2019 talks in our User Spotlight Series.

Transcript

Thank you, Daniel! So, this talk will be a quick introduction to the process management in HAProxy or more precisely the Master/Worker mode. I won’t go into details, so if you want the details you can talk to me after the talk.

1.-haproxy-process-management-history-1675687619

Little bit of history first. I won’t talk about everything, but the important thing here is the systemd wrapper, which was the ancestor of the Master/Worker mode. Basically it was an external binary, which was launching HAProxy and handling signals. It was made to be compatible which systemd because the daemon mode of HAProxy wasn’t compatible with it. In version 1.8, we wanted to reintegrate this feature within HAProxy, so we have only one binary to do it. And after that we enhanced it with the Master CLI and the program section.

To understand why we needed the Master/Worker Mode, I have to introduce the daemon mode and what are the flaws in it.

2.-daemon-mode-1675687648

The daemon mode is the original mode used before systemd, so when you add a distribution with system file, we used this mode. It’s a simple design, basically it only launches as a process. It can be complex to monitor, because you have to monitor every single process. You can have many, and when you reload you can have many more. There is no Master process, so you cannot monitor only one and this is a problem with systemd.

3.-daemon-mode_reload-1-1675687669

This is the architecture of the daemon mode. For example, you have two processes in your configuration, so when you launch it, there are two processes.

4.-daemon-mode_reload-2-1675687692

And if you want to reload it, you have to launch a new process. This is interesting because with this method you can update the binary because you will launch a new process. The new process will load the configuration and it will send the soft stop signal, which is the -USR1 signal. The argument is -sf followed by the PID of the previous processes.

5.-daemon-mode_reload-3-1675687714

And then the answer it sends, it sends a signal. The new process will fork to a new process and you will probably still have the older processes because they still have jobs to finish.

6.-daemon-mode_reload-4-1675688554

After they finish their jobs, they will disappear. This is the problem with systemd, because systemd will consider that HAProxy left because of some processes left. Because with systemd, we need a main PID, and to have a main PID, you need main processes, a master process, and this is not the case in this mode.

7.-master-worker-mode-(1)

So, we went on to work with systemd and we introduced the Master/Worker. It was inspired by the systemd wrapper, but simpler because it’s not an external binary anymore. You just have to launch HAProxy with an argument. It’s only one process to monitor, so you have your main PID for systemd. And there is a feature which is a Master CLI, which is basically a stats socket which is connected to the master process.

8.-master-worker_reload-1-1675690699

It’s the same configuration with nbproc 2. In this architecture you can see that there is a Master and two Workers.The Workers are still independent, they still don’t communicate with each other, but the Master is able to communicate with the Worker.

9.-master-worker_reload-2-1675690722

If you want to reload, you have to send the -USR2 signal, this is what is done in the systemd unit file.

10.-master-worker_reload-3-1675690747

Once you send this signal, the Master will reload itself.

11.-master-worker_reload-4-1675690769

We still need the ability to update the binary, so it will re-exec itself. It will load the new binary and then load the new configuration.

12.-master-worker_reload-5-1675691021
13.-master-worker_reload-6-1675691030

And after that all is the same as the daemon mode: it will send the soft stop signal, it will fork and then the previous process will disappear once they finish their job.

14.-master-worker_start-1675691051

To launch in Master/Worker mode you simply put the -W argument on your command line. On the first line, you can see that it will be launched on the foreground, on the second line combined with -D argument it will be sent in the background. The last line is a -Ws argument which is a Master mode with systemd compatibility. It’s the ability to connect with sd_notify API to say to systemd: “Okay, I finished to load the configuration”, so you can have your systemctl status. To use it you need to modify the system unit file and put the Type=notify in the unit file.

15.-master-worker_optional-configration-1675691073

Okay, so here is some optional configuration. For example, if you don’t want to use the -W argument you can use the master-worker keyword in the global section.

There is a no-exit-on-failure keyword, which is the opposite of the exit-on-failure ability of the Master/Worker. In fact, if there is a problem with one of the Workers, for example, if it quits for whatever reason, the Master will kill every other Worker. So, it guarantees you to have a complete HAProxy instance which is working. If you want to debug on either one of the Workers, which can be still there, if another one segfaults or if there is an Out of Memory, you can use this keyword.

The second keyword is the mworker-max-reloads. It will say to the Master to kill a Worker after a certain amount of reloads. For example, if you try to reload a Worker three times and if it’s still there because there is some stuff to do, it would be killed under force-reload. And you can achieve the same thing, but this time with hard-stop-after. So, after a certain amount of time it will be killed.

16.-daemon-mode_cli-1-1675691095

There is a new feature which is called the Master CLI. To understand why we made this, we need to understand what the flaws of the previous mode were. So, basically when you add two processes, you need it to have one CLI on each process, which is not really convenient.

17.-daemon-mode_cli-2-1675691443

The problem is that when you reload the processes, the CLI is bound on the new process and you won’t be able to access the previous processes.

18.-master-cli-1-1675691462

The Master CLI is connected directly to all processes, so you can connect to any process: the new one and the old one.

19.-master-cli-2-1675691480

The Master CLI is configurable with an HAProxy argument, so you don’t need to modify your configuration file.

20.-master-cli_configuration-1675691498

You just have to put a -S with a bind line, which can be a path or an IP or you can use any keywords that are useful. They are the same keywords as the bind line in the HAProxy configuration. For example, you can see, there’s a uid, a gid, you can even use SSL, if you want. You can also use several Master CLIs. For example, you can use one CLI with a level,operator, one CLI with a level,user, which would be basically read-only.

21.-master-cli_available-commands-1675691551

There are a few new commands. The important ones are: the @ symbol and the show proc command and reload which does the same as kill -USR2.

22.-master-cli_show-proc-command-1675691572

The show proc command will show you every process. So you will have the <PID>, the <type> of process, the <relative PID> and the number of <reloads>. Also, there’s <uptime>. Something which can be important if you will try to update the binary – the <version>. For example, you can see that on the last line the version is not the same, because we reloaded it with a new version.

23.-master-cli_command-prefixes-1675691593

To enter in a process with the CLI, you need to use the @ prefix. For example, you can enter @1, which will enter in the process whose relative PID is 1. There you can type anything, like if you were on a normal stats socket. To kill it, you just have to type @ without anything. And if you want to enter in an old process, you can’t use the relative PID, so you have to use the real PID.

24.-programs-1675691615

The last feature which was made in the Master/Worker are Programs. It’s the ability to launch an external binary at HAProxy startup. It’s simple. This is not systemd,  so it will just launch binary and send a signal on the reload. But don’t expect dependencies and the complicated stuff. If you need that, use systemd.

25.-programs_configuration-1675691636

The configuration is simple. This is an example where I will launch an SPOA agent. You can use any argument on the program. And the option start-on-reload will permit you to relaunch the binary upon an HAProxy reload. The different behavior is to send a -USR1 signal and probably your program will kill the -USR1 signal, so you want to probably restart it. This option will achieve this.

WL
William Lallemand , HAProxy Technologies

Organizations rapidly deploy HAProxy products to deliver websites and applications with the utmost performance, observability and security at any scale and in any environment. Looking for more stories?

Explore All User Spotlights